1page.title=Drawable Animation 2parent.title=Animation 3parent.link=animation.html 4@jd:body 5 6 <p>Drawable animation lets you load a series of Drawable resources one after 7 another to create an animation. This is a traditional animation in the sense that it is created with a sequence of different 8 images, played in order, like a roll of film. The {@link 9 android.graphics.drawable.AnimationDrawable} class is the basis for Drawable animations.</p> 10 11 <p>While you can define the frames of an animation in your code, using the {@link 12 android.graphics.drawable.AnimationDrawable} class API, it's more simply accomplished with a 13 single XML file that lists the frames that compose the animation. The XML file for this kind 14 of animation belongs in the <code>res/drawable/</code> directory of 15 your Android project. In this case, the instructions are the order and duration for each frame of 16 the animation.</p> 17 18 <p>The XML file consists of an <code><animation-list></code> element as the root node and a 19 series of child <code><item></code> nodes that each define a frame: a drawable resource for 20 the frame and the frame duration. Here's an example XML file for a Drawable animation:</p> 21 <pre> 22<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 23 android:oneshot="true"> 24 <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 25 <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 26 <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 27</animation-list> 28</pre> 29 30 <p>This animation runs for just three frames. By setting the <code>android:oneshot</code> 31 attribute of the list to <var>true</var>, it will cycle just once then stop and hold on the last 32 frame. If it is set <var>false</var> then the animation will loop. With this XML saved as 33 <code>rocket_thrust.xml</code> in the <code>res/drawable/</code> directory of the project, it can 34 be added as the background image to a View and then called to play. Here's an example Activity, 35 in which the animation is added to an {@link android.widget.ImageView} and then animated when the 36 screen is touched:</p> 37 <pre> 38AnimationDrawable rocketAnimation; 39 40public void onCreate(Bundle savedInstanceState) { 41 super.onCreate(savedInstanceState); 42 setContentView(R.layout.main); 43 44 ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); 45 rocketImage.setBackgroundResource(R.drawable.rocket_thrust); 46 rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 47} 48 49public boolean onTouchEvent(MotionEvent event) { 50 if (event.getAction() == MotionEvent.ACTION_DOWN) { 51 rocketAnimation.start(); 52 return true; 53 } 54 return super.onTouchEvent(event); 55} 56</pre> 57 58 <p>It's important to note that the <code>start()</code> method called on the AnimationDrawable 59 cannot be called during the <code>onCreate()</code> method of your Activity, because the 60 AnimationDrawable is not yet fully attached to the window. If you want to play the animation 61 immediately, without requiring interaction, then you might want to call it from the <code>{@link 62 android.app.Activity#onWindowFocusChanged(boolean) onWindowFocusChanged()}</code> method in your 63 Activity, which will get called when Android brings your window into focus.</p> 64 65 <p>For more information on the XML syntax, available tags and attributes, see <a href= 66 "{@docRoot}guide/topics/resources/animation-resource.html">Animation Resources</a>.</p> 67