Frame Animation, that is, playing images prepared in advance in sequence, is similar to a movie.
The following example shows how to click the button to play the animation and click "stop" to stop the animation!
1 ,:
2. The main. xml file is simple:
<Button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/button" Android: text = ""/> <button Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: Id = "@ + ID/Stop" Android: text = "stop"/> <imageview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_gravity = "center_vertical" Android: Id = "@ + ID/image"/>
3. Define a frame. xml file under the drawable folder to define all the images in the XML file. It is an animation list and stores all the images used !!
<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android" ><item android:drawable="@drawable/girl_1" android:duration="100" /><item android:drawable="@drawable/girl_2" android:duration="100" /><item android:drawable="@drawable/girl_3" android:duration="100" /><item android:drawable="@drawable/girl_4" android:duration="100" /><item android:drawable="@drawable/girl_5" android:duration="200" /><item android:drawable="@drawable/girl_6" android:duration="500" /><item android:drawable="@drawable/girl_7" android:duration="500" /><item android:drawable="@drawable/girl_8" android:duration="200" /><item android:drawable="@drawable/girl_9" android:duration="100" /><item android:drawable="@drawable/girl_10" android:duration="100" /><item android:drawable="@drawable/girl_11" android:duration="100" /></animation-list>
Each item has a Duration Attribute. This attribute is used to determine the duration of the image !!
4. Java code:
Package CN. csdn. anim; import android. app. activity; import android. graphics. drawable. animationdrawable; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imageview; public class frameactivity extends activity implements onclicklistener {private button, stop; private imageview image; private animationdrawable attackanimation; // define the animation object @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {image = (imageview) findviewbyid (R. id. image); // display the animation's imageviewbutton = (button) findviewbyid (R. id. button); // start animation stop = (button) findviewbyid (R. id. stop); // stop the Animation button. setonclicklistener (this); stop. setonclicklistener (this); image. setbackgroundresource (R. drawable. frame); // set the background resource parameter of the image that displays the animation to be int, which is the frame you write yourself. XML, which contains all related images attackanimation = (animationdrawable) image. getbackground () ;}@ overridepublic void onclick (view v) {Switch (v. GETID () {case R. id. button: attackanimation. start (); // start animation break; case R. id. stop: attackanimation. stop (); // stop the animation break ;}}}
The animationdrawable class is:
An object used to create frame-by-frame animations, defined by a series of drawable objects, which can be used as a view object's background.
The simplest way to create a frame-by-frame animation is to define the animation in an XML file, placed in the Res/drawable/folder, and set it as the background to a view object. then, callstart()To
Run the animation.
An animationdrawable defined in XML consists of a single<animation-list>Element, and a series of nested<item>Tags. Each item defines a frame
Of the animation.
An object is used to create a frame-by-frame animation. It is defined as a series of printable objects and can be used as the background of a view object.
The easiest way to create a frame-by-frame animation is to define an animation in an XML file, place it in a magazine/printable/folder, and set it as a background, a view object. Then, it is called an animation to start () running.
Define an animationdrawable in XML to form a single <animation-List> element, a series of nested <project> labels. Each item defines a frame animation.