In the previous article we said that Android tween animation, this one we say frame animation, frame animation is to achieve a similar to the effect of GIF animation, is a number of images in a predetermined time sequential display sequentially.
Create a new Android project, named Frametest, create a new folder under the Res folder called Anim, the XML file of our frame animation is placed here.
Create a new Frame.xml file in Anim with the following contents:
<?xml version= "1.0" encoding= "Utf-8"?><animation-list xmlns:android="Http://schemas.android.com/apk/res/android" android:oneshot="false"> <!--True to play only once, false to play an infinite loop-- <item android:drawable="@drawable/girl_1" android:duration="100" /> <item android:drawable= "@drawable/girl_2" android:duration=" " /> <item android:drawable="@drawable/girl_3" android:duration= "/>" <item android:drawable="@drawable/girl_4" android:duration="100" /> <item android:drawable= "@drawable/girl_5" android:duration=" " /> <item android:drawable="@drawable/girl_6" android:duration= "/>" <item android:drawable="@drawable/girl_7" android:duration="400" /> <item android:drawable= "@drawable/girl_8" android:duration=" " /> <item android:drawable="@drawable/girl_9" android:duration= "/>" <item android:drawable="@drawable/girl_10" android:duration="100" /> <item android:drawable= "@drawable/girl_11" android:duration=" " /></animation-list>
Here is the 11 picture, the previous Android:oneshot property indicates the number of times the animation was executed, false means that the animation repeats itself repeatedly, True indicates that the animation value plays once, and duration indicates the time, in milliseconds, that each picture is displayed.
Then look at the code in Mainactivity:
Public class mainactivity extends Activity { PrivateImageView IV;PrivateAnimationdrawable AD;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main); IV = (ImageView) This. Findviewbyid (R.ID.IV);//Iv.setbackgroundresource (r.anim.frame);//AD = (animationdrawable) iv.getbackground (); //Above two sentences, can be replaced by the following two sentences, the effect is the sameIv.setimageresource (R.anim.frame); AD = (animationdrawable) iv.getdrawable (); } Public void Start(View v) {//If AD is running, let it stop first if(Ad.isrunning ()) ad.stop (); Ad.start (); }}
First get a imageview, and then set the frame animation to its background, and finally get the background of the picture and strong to animationdrawable, when clicked on the picture, if the animation is already running, let it stop first, and then rerun, otherwise directly run can.
Android Frame Animation detailed