drawable animation is a frame-by-pixel animation, you must first define each frame before using it. We can either define it by code or use the XML file definition, which is generally used. As follows:
<?XML version= "1.0" encoding= "Utf-8"?><animation-listxmlns:android= "Http://schemas.android.com/apk/res/android"Android:oneshot= "false"> <Itemandroid:drawable= "@drawable/icon_aboutus"android:duration= "$" /> <Itemandroid:drawable= "@drawable/icon_join_menu"android:duration= "$" /> <Itemandroid:drawable= "@drawable/icon_join_opinion"android:duration= "$" /> </animation-list>
where android:oneshot= "true" indicates that the animation plays only once and is equal to false when looping. The <item/> tab defines the picture that is displayed for each frame. The display order is in accordance with the <item/> definition order.
Then look at the activity of the main interface:
Public classMainactivityextendsActivity { PublicImageView Iv_start; PublicImageView Iv_stop; PublicImageView iv_animation; Publicanimationdrawable drawableanimation; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Iv_start=(ImageView) Findviewbyid (R.id.iv_start); Iv_stop=(ImageView) Findviewbyid (r.id.iv_stop); Iv_animation=(ImageView) Findviewbyid (r.id.iv_animation); Iv_start.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {startanimation (); } }); Iv_stop.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {stopanimation (); } }); Iv_animation.setbackgroundresource (r.drawable.animations); Drawableanimation=(animationdrawable) iv_animation.getbackground (); } Public voidstartanimation () {Drawableanimation.start (); } Public voidstopanimation () {drawableanimation.stop (); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {intID =Item.getitemid (); if(id = =r.id.action_settings) { return true; } return Super. onoptionsitemselected (item); }}
Android Animation drawable Animation