Just like in a movie, play a ready-to-prepare picture to achieve the animated effect.
Frames-by-frame animations need to use the Animationdrawable class, which is used primarily to create a frame-by-frames animation, and then we set the animation to the background of the view.
Android offers two ways to add frames for animationdrawable: XML definition and Java code creation.
Xml
Because the animation frame resource needs to be a drawable object, it needs to be placed in the drawable directory. Use <item> to add a frame in <animation-list>
Anima.xml
<?XML version= "1.0" encoding= "Utf-8"?><animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "false" > <Itemandroid:drawable= "@drawable /A"android:duration= "+"/> <Itemandroid:drawable= "@drawable/b"android:duration= "+"/> <Itemandroid:drawable= "@drawable/C"android:duration= "+"/> <Itemandroid:drawable= "@drawable/d"android:duration= "+"/> <Itemandroid:drawable= "@drawable/E"android:duration= "+"/></animation-list>
Mainactivity.java
PackageCn.lixyz.animator;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;Importandroid.graphics.drawable.AnimationDrawable;ImportAndroid.os.Bundle;ImportAndroid.widget.ImageView; @SuppressLint ("Newapi") Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ImageView IV=(ImageView) Findviewbyid (r.id.image); Animationdrawable AD=Newanimationdrawable (); Ad=(animationdrawable) getresources (). getdrawable (R.drawable.anima); Iv.setbackground (AD); Ad.start (); }}
Java
In Android, you can add animation frames to animationdrawable with the Animationdrawable.addframe () method, in addition to the ability to define a per-frame animation through an XML file.
PackageCn.lixyz.animator;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;Importandroid.graphics.drawable.AnimationDrawable;ImportAndroid.os.Bundle;ImportAndroid.widget.ImageView; @SuppressLint ("Newapi") Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); ImageView IV=(ImageView) Findviewbyid (r.id.image); Animationdrawable AD=Newanimationdrawable (); Ad.addframe (Getresources (). Getdrawable (R.DRAWABLE.A),500); Ad.addframe (Getresources (). Getdrawable (r.drawable.b),500); Ad.addframe (Getresources (). Getdrawable (r.drawable.c),500); Ad.addframe (Getresources (). Getdrawable (R.DRAWABLE.D),500); Ad.addframe (Getresources (). Getdrawable (R.DRAWABLE.E),500); Ad.setoneshot (false); Iv.setbackground (AD); Ad.start (); }}
Common methods
Animationdrawable also has a few other methods:
void Start (): Starts playback of frame-wise animations.
void Stop (): Stops the frame-wise animation.
void Addframe (drawable frame,int duration): Adds a frame to animationdrawable and sets the duration.
int getduration (int i): Gets the duration of the frame for the specified index.
drawable getframe (int index): Gets the frame drawable of the specified index.
int Getnumberofframes (): Gets the number of all frames in the current animationdrawable.
Boolean isoneshot (): Whether the current animationdrawable executes once, returns true once, and false loops.
Boolean isrunning (): Whether the current animationdrawable is playing.
void Setoneshot (Boolean oneShot): Sets whether animationdrawable executes once, true once, and false loop plays
Android Notes (63) animation in Android-Frame by box animation (Frame-by-frame animation)