3

I have created an AnimationDrawable and i want to repeat that animation for infinite number and stop that animation on a button click, i am sharing my code

anim.xml file

<?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/download_anim1" android:duration="100" />
    <item android:drawable="@drawable/download_anim2" android:duration="100" />
    <item android:drawable="@drawable/download_anim3" android:duration="100" />
    <item android:drawable="@drawable/download_anim4" android:duration="100" />
    <item android:drawable="@drawable/download_anim5" android:duration="100" />
</animation-list>

in my layout file

<ImageView
    android:id="@+id/imageViewAnimation"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:padding="3dp"
    android:layout_marginBottom="10dp"
    android:layout_weight="2"
    android:background="@color/colorOrange"
    android:src="@drawable/anim" />

and in my activity where i have started my animation

ImageView imageViewAnimationr = (ImageView) headerView.findViewById(R.id.imageViewHeaderDownloadAnimation);
AnimationDrawable animationDrawable = (AnimationDrawable) imageViewAnimationr.getDrawable();
animationDrawable.start();
Zubair Akber
  • 2,760
  • 13
  • 30

2 Answers2

2

I just find setOneShot for repeating the animation and find Stop to stop the animation at any instance.

Zubair Akber
  • 2,760
  • 13
  • 30
2

you can change oneshot=false for repeating infinite times and stop on any condition you can use animationDrawable.stop() like it :-

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/img_one" android:duration="200" />
    <item android:drawable="@drawable/img_two" android:duration="200" />
    <item android:drawable="@drawable/img_three" android:duration="200" />
</animation-list>
Geet Thakur
  • 1,966
  • 1
  • 16
  • 23