Android and frame-by-frame animation:
:
When we click the button, the image will not stop rotating, and when we click the button again, it will stop in the current status.
Activity Code:
Package cn.com. chenzheng_java.animation; </P> <p> Import android. app. activity; <br/> Import android. graphics. drawable. animationdrawable; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. widget. imageview; <br/>/** <br/> * @ description frame-by-frame animation in Android. <br/> * the principle of frame-by-frame animation is very simple. Like playing a movie, a picture similar to one another is continuously switched. When the switching speed reaches a certain value, <br/> * our vision will show a shadow. The appearance of the Shadow ensures the continuity of visual changes. At this time, the switching of images will be the same in our eyes. <Br/> * frame-by-frame animation: <br/> * Step 1: Create an XML file in the Res/drawable folder, this file defines in detail the pictures used for animation playback, the time used to switch each picture <br/> *, and whether the video is played continuously. (Some articles say that placing the file in the Res/anim folder turns out to be wrong.) <br/> * Step 2: layout the file in the code, assign a value to a specific image display control, such as the imageview in this example. <Br/> * Step 3: Use imageview. getbackground () gets the corresponding animationdrawable object, then control the animation using this object method <br/> * @ author chenzheng_java <br/> */<br/> public class animation1activity extends activity {<br /> imageview; <br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. animation1); </P> <p> imageview = (imageview) findviewbyid (R. id. imageview_animation1); <br/> imageview. setbackgroundresource (R. drawable. animationappsdrawable); </P> <p >}</P> <p> Public void myclickhandler (view targetbutton) {<br/> // obtain the animationdrawable object <br/> animationdrawable = (animationdrawable) imageview. getbackground (); </P> <p> // whether the animation is running <br/> If (animationdrawable. isrunning () {<br/> // stop playing the animation <br/> animationdrawable. stop (); <br/>}< br/> else {<br/> // start or resume animation playback <br/> animationdrawable. start (); <br/>}</P> <p >}< br/>}
Animation1.xml file:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "match_parent" Android: layout_height = "match_parent" <br/> Android: orientation = "vertical"> <br/> <button Android: Id = "@ + ID/button_animation1" Android: text = "animation start" <br/> Android: layout_gravity = "center_horizontal" Android: layout_width = "wrap_content" <br/> Android: layout_height = "wrap_content" Android: onclick = "myclickhandler"> </button> <br/> <imageview Android: Id = "@ + ID/imageview_animation1" <br/> Android: layout_width = "fill_parent" <br/> Android: layout_height = "wrap_content" Android: layout_weight = "1"> </imageview> <br/> </linearlayout> <br/>
XML file for storing animation files:
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! -- <Br/> the root tag is animation-list. oneshot indicates whether to display the animation only once. If it is set to false, the animation is played continuously. <br/> under the root tag, declare each image in the animation using the item tag <br/> Android: duration indicates the duration of the image to be displayed. <br/> --> <br/> <animation-list <br/> xmlns: android = "http://schemas.android.com/apk/res/android" <br/> Android: oneshot = "false" <br/> <item Android: drawable = "@ drawable/A1" Android: duration = "50"> </item> <br/> <item Android: drawable = "@ drawable/A2" Android: duration = "50"> </item> <br/> <item Android: drawable = "@ drawable/A3" Android: duration = "50"> </item> <br/> <item Android: drawable = "@ drawable/A4" Android: duration = "50"> </item> <br/> <item Android: drawable = "@ drawable/A5" Android: duration = "50"> </item> <br/> <item Android: drawable = "@ drawable/a6" Android: duration = "50"> </item> <br/> </animation-List> <br/>
In addition: In animationdrawable, we can also see the following important methods:
Setoneshot (Boolean flag) is the same as configuring in the configuration file. You can set whether to play the animation once. If it is set to false, the animation is played continuously;
Addframe (drawable frame, int duration) dynamically adds an image to the animation.