Today to introduce the frame animation in Android, frame animation is actually a picture, in a certain order to play, and then form an animation.
First, let's take a picture:
Two buttons in the diagram, start to play the animation, stop the animation, to see the code:
1. Res/drawable/frame_animation.xml:
<?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/ma1 "android:duration=" 100 " ></item> <item android:drawable= "@drawable/ma2" android:duration= "></item> < Item android:drawable= "@drawable/ma3" android:duration= "></item> <item android:drawable=" @ Drawable/ma4 "android:duration=" ></item> <item android:drawable= "@drawable/ma5" Android: duration= "></item> <item android:drawable=" @drawable/ma6 "android:duration=" "></" item> <item android:drawable= "@drawable/ma7" android:duration= "></item> <item" android:drawable= "@drawable/ma8" android:duration= "></item></animation-list>
This is the XML configuration frame animation, each item is a frame, the oneshot parameter means that you want to play the end of a time, false means to continue to loop
2. Res/layout/activity_main.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " android:orientation= "vertical" tools:context= ". Mainactivity "> <button android:id=" @+id/btn_start " android:layout_width=" Wrap_content " android:layout_height= "Wrap_content" android:text= "Start"/> <button android:id= "@+id/btn_ Stop " android:layout_width=" wrap_content " android:layout_height=" wrap_content " android:text=" Stop "/> <imageview android:id=" @+id/ma_iv " android:layout_width= " Wrap_content " android:layout_height= "Wrap_content" android:layout_centerinparent= "true"/> </LinearLayout>
3. Mainactivity.java:
Package Com.example.demo5_04_frameanimation;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.graphics.drawable.animationdrawable;import Android.view.ContextMenu; Import Android.view.menu;import Android.view.view;import Android.view.contextmenu.contextmenuinfo;import Android.widget.button;import Android.widget.imageview;public class Mainactivity extends Activity {private Animationdrawable ad = Null;imageview IV; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (r.layout.activity_main); iv = (ImageView) Findviewbyid (R.ID.MA_IV); Iv.setimageresource (r.drawable.frame_animation); ad = (animationdrawable) iv.getdrawable (); Button Btn_start = (button) Findviewbyid (R.id.btn_start); Button Btn_stop = (button) Findviewbyid (r.id.btn_stop); Btn_start.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {Ad.start ();}}); Btn_stop.setonclicklistener (New View.onclicklisteNER () {@Overridepublic void OnClick (View v) {ad.stop ();}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}
The code in Mainactivity is simpler, mainly how to initialize an animation. The ImageView is used here, the resources of IV are animated, and the animation resources are obtained through iv.getdrawable (), and then the ad is used to control the playback of the animation to stop. Very simple, we can see the understanding.
Android Getting Started note-Interface development-frame animation