A simple instance used by AnimationDrawable in Android

  • 2020-05-17 06:31:50
  • OfStack

First, you can define an xml file under the drawable folder. As follows:


<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/compass_1" android:duration="70" />
    <item android:drawable="@drawable/compass_2" android:duration="70" />
    <item android:drawable="@drawable/compass_3" android:duration="70" />
    <item android:drawable="@drawable/compass_4" android:duration="70" />
    <item android:drawable="@drawable/compass_5" android:duration="70" />
</animation-list>

The root tag is animation-list, where oneshot represents whether to show only once, and false will play the animation in a continuous loop. Among them, each item is 1 frame, android:duration="400" means that each frame lasts for 400ms, android:drawable is the picture to be displayed for each frame.
Next, the code USES:


AnimationDrawable ad = (AnimationDrawable) getResources().getDrawable(R.drawable.bootanimation);

Get the AnimationDrawable object. Set the object to backgroud of the current view.


mView.setBackgroundDrawable(ad);

You can then call the ad.start () method and the ad.stop () method to start and stop the animation.
Note: this animation must be set to the current background of view, otherwise start and stop will not work.


Related articles: