ImageButton 顺序动画的无限循环

Infinite loop of sequential animation of ImageButton

我是 android studio 的新手,我想使用顺序动画集为 imageButton 制作动画。动画集(animation_boutons.xml)在res/anim中。 我已经尝试在 java 中使用 animationSet,但每次启动模拟器时应用程序都会崩溃。 我花了很长时间寻找解决方案。我希望有一个人可以帮助我 ! 如果这很明显,我深表歉意。

java代码:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        configureCodeurBouton();
    }
    private void configureCodeurBouton() {
        ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
        Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
        animBoutons.setRepeatCount(Animation.INFINITE);
        boutonCodeur.setAnimation(animBoutons);
        boutonCodeur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, codeur.class));
            }
        });
    }
}

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true"
    android:fillAfter="true">

    <rotate

        android:fromDegrees="0"
        android:toDegrees="20"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="0"
        android:duration="1000"
        />

    <rotate
        android:startOffset="1000"
        android:fromDegrees="20"
        android:toDegrees="-20"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        />

    <rotate

        android:fromDegrees="-20"
        android:toDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="3000"
        android:duration="1000"
        />

</set>

此外,Vedprakash Wagh 给了我尝试 animBoutons.setRepeatCount(Animation.INFINITE) 的建议,但没有效果。

您的应用程序每次都会崩溃,因为您试图在 class 首先创建时而不是在设置布局之后找到您的 ImageButton。

您正在获取 NullPointerException,因为当您尝试查找时,您的视图层次结构中没有 ID 为 R.id.boutoncodeur 的 ImageButton。

您需要找到您的 ImageView AFTER 它在您的视图层次结构中可用,即在您的 setContentView();

之后

您可以这样做:

  1. 删除第二行

    ImageButton boutonCodeur = findViewById(R.id.boutoncodeur);
    

    因为您已经在 configureCodeurButton() 函数中找到了 ImageView

  2. 或者,您可以保留 ImageView 的一个 class 变量,并在 setContentView 之后调用 findViewById,如下所示。

    public class MainActivity extends AppCompatActivity {
    ImageButton boutonCodeur;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        boutonCodeur = findViewById(R.id.boutoncodeur);
        configureCodeurBouton();
    }
    private void configureCodeurBouton() {
        Animation animBoutons = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.anim_boutons);
        boutonCodeur.setAnimation(animBoutons);
        boutonCodeur.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, codeur.class));
            }
        });
    }
    }
    

您可以了解有关 NullPointerException here 的更多信息。此外,学习如何阅读可用教程中的错误。或者,只需在发生错误时打开 Android Studio 中的 logcat 选项卡即可了解您遇到的错误。

  1. 要使您的动画 运行 无限,您可以在代码中添加它。

animation.setRepeatCount(Animation.INFINITE)

我只需要更改整个 xml anim_boutons 文件,所以我只有一个动画而不是三个旋转动画。 repeatMode 行表示在每次重复时向后重复动画。这给出了预期的效果。

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="true"
    android:fillAfter="true">

    <rotate

        android:fromDegrees="-20"
        android:toDegrees="20"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="0"
        android:duration="1000"
        android:repeatCount="infinite"
        android:repeatMode="reverse"
    />

</set>