I’m trying to display a frame to frame animations using AnimationDrawable when clicking a ImageView. But my animation happens only once when I click the ImageView. I’m using xml for the animation.
Animation 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>
Here’s my xml for the button and the container of the animation image:
<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>
checkGem Method:
ImageView popup1 = (ImageView) findViewById(R.id.scorePopup);
popup1.setBackgroundResource(R.drawable.popup1_animation);
AnimationDrawable frameAnimation2 = (AnimationDrawable)
popup1.getBackground();
frameAnimation2.start();
My problem is the animation displays only once, But I want it to animate everytime I click. Any ideas?
Place a
stop()call on theImageView‘sAnimationDrawablebackground right before thestart()call:When you first click the
ImageViewthe animation will run but at the end it will find itself in a state where it thinks is still running. In that state, callingstart()will simply be ignored. Thestop()method “resets” that state and theAnimationDrawablewill run from the start again.