Frame animation is to put some column pictures. Play it in turn.
Using the principle of "visual persistence" of the naked eye, the illusion of animation is given to the user, and the principle of frames-by-frame animation is the same as that of the early film.
A: You need to define a frame-wise animation that can be defined by code. can also be defined by an XML file. General XML file definition is more intuitive
<?xml version= "1.0" encoding= "Utf-8"? ><animation-list xmlns:android= "http://schemas.android.com/apk/res/ Android "android:oneshot=" false "> <!--false is looping play--<!--drawables is every picture. Duration is the duration of the picture--<item android:drawable= "@drawable/g1" android:duration= "$"/> <item Android:drawa ble= "@drawable/g2" android:duration= "/> <item android:drawable=" @drawable/g3 "android:duration=" 200 "/ > <item android:drawable= "@drawable/g4" android:duration= "$"/> <item android:drawable= "@drawable/g5" android:duration= "/> <item android:drawable=" @drawable/g6 "android:duration="/> <item android: drawable= "@drawable/g7" android:duration= "/> <item android:drawable=" @drawable/g8 "android:duration=" 200 "/> <item android:drawable=" @drawable/g9 "android:duration=" "/> <item android:drawable=" @drawable/g "android:duration=" "/> <item android:drawable=" @drawabLe/g11 "android:duration="/></animation-list>
Where oneshot represents whether the loop plays, false is looping, true is only played once
B: Set the resource defined above XMLD to the background of ImageView
Find ImageView ImageView IV = (ImageView) Findviewbyid (R.ID.IV); Set the resource file of the frame animation to ImageView background iv.setbackgroundresource (r.drawable.frameanimation);
C: Get Animationdrawable Object
Gets the Animationdrawable object animationdrawable ad = (animationdrawable) iv.getbackground ();
D: Start playing the animation on OK
Start playing animation Ad.start ();
Activity entire code:
public class Mainactivity extends Activity { @Override protected void onCreate (Bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Find ImageView ImageView IV = (ImageView) Findviewbyid (R.ID.IV); Set the resource file of the frame animation to ImageView background iv.setbackgroundresource (r.drawable.frameanimation); Gets the Animationdrawable object animationdrawable ad = (animationdrawable) iv.getbackground (); Start playing the animation Ad.start (); }}
Demo Effect:
Step by step animation of Android learning (frame)