【问题标题】:Infinite loop of sequential animation of ImageButtonImageButton顺序动画的无限循环
【发布时间】:2019-06-09 20:29:14
【问题描述】:

我是 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) 但它没有效果。

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    您的应用程序每次都会崩溃,因为您在第一次创建类时尝试查找 ImageButton,而不是在设置布局之后。

    你得到NullPointerException,因为当你试图找到它时,你的视图层次结构中没有ID为R.id.boutoncodeur的ImageButton。

    你需要找到你的ImageViewAFTER它在你的视图层次结构中可用,即在你的setContentView();之后

    您可以这样做:

    1. 删除你的第二行

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

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

    2. 或者,您可以保留ImageView 的一个类变量,并在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));
              }
          });
      }
      }
      

    您可以了解更多关于NullPointerExceptionhere的信息。此外,了解如何从可用的教程中阅读错误。或者,只需在发生错误时打开 Android Studio 中的 logcat 选项卡即可知道您遇到了什么错误。

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

    animation.setRepeatCount(Animation.INFINITE)

    【讨论】:

    • 感谢您的回答。我在帖子上解决了这个问题,我学到了一些东西。但是,您对我的问题有想法吗? (使动画无限)
    • 我已经更新了我的答案,请查看。如果对您有帮助,请不要忘记将其标记为已接受
    • 不,它不起作用。动画启动一次然后停止。我也尝试使用除 Animation.INFINITE 之外的另一个值(如 15)来查看参数是否是问题所在,但它没有效果。我认为它是一个动画集的事实阻止了这种方法。
    • 将 android:repeatCount="infinite" 添加到所有旋转动画并从代码中删除 setrepeat count
    • 我试过了,但它没有按预期工作。每个旋转动画都单独重新启动,并且完整的动画被窃听。至少它是无限的。还有其他想法吗?此外,当我打开一个新活动然后返回时,错误的动画不起作用。
    【解决方案2】:

    我只需要更改整个 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>
    

    【讨论】:

      猜你喜欢
      • 2014-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      相关资源
      最近更新 更多