我试图在单击ImageView
时使用AnimationDrawable显示帧到帧动画。但当我单击 ImageView 时,我的动画仅发生一次。我使用 xml 来制作动画。
动画 XML:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true">
<item android:drawable="@drawable/scorepopup1" android:duration="100" />
<item android:drawable="@drawable/scorepopup2" android:duration="100" />
<item android:drawable="@drawable/scorepopup3" android:duration="100" />
<item android:drawable="@drawable/scorepopup4" android:duration="100" />
<item android:drawable="@drawable/invipopup" android:duration="100" />
</animation-list>
这是我的按钮和动画图像容器的 xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="70dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_margin="5dp">
<ImageView
android:layout_width="35dp"
android:layout_height="35dp"
android:background="@drawable/bd"
android:layout_margin="10dp"
android:onClick="checkGem"
android:contentDescription="bd"/>
<ImageView
android:id="@+id/scorePopup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp" />
</LinearLayout>
检查 gem 方法:
ImageView popup1 = (ImageView) findViewById(R.id.scorePopup);
popup1.setBackgroundResource(R.drawable.popup1_animation);
AnimationDrawable frameAnimation2 = (AnimationDrawable)
popup1.getBackground();
frameAnimation2.start();
我的问题是动画仅显示一次,但我希望每次单击时它都会显示动画。有什么想法吗?
最佳答案
在 start()
之前的 ImageView
的 AnimationDrawable
背景上调用 stop()
调用:
//...
AnimationDrawable frameAnimation2 = (AnimationDrawable) popup1.getBackground();
frameAnimation2.stop();
frameAnimation2.start();
当您第一次单击ImageView
时,动画将会运行,但最后它会发现自己处于它认为仍在运行的状态。在这种状态下,调用 start()
将被忽略。 stop()
方法“重置”,表明 AnimationDrawable
将再次从头开始运行。
关于android - 在每个 onClick 调用上运行相同的帧动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14627677/