java.lang.Object | ||||
android.graphics.drawable.Drawable | ||||
android.graphics.drawable.DrawableContainer | Drawable.Callback | |||
android.graphics.drawable.AnimationDrawable | Runnable |
An object used to define frame-by-frame animations that can be used as a View object's background.
Each frame in a frame-by-frame animation is a drawable resource. The simplest way to create a frame-by-frame animation is to define the animation in an XML file in the drawable/ folder, set it as the background to a View object, then call AnimationDrawable.run() to start the animation, as shown here. More details about the format of the animation XML file are given in Frame by Frame Animation. spin_animation.xml file in res/drawable/ folder:
<!-- Animation frames are wheel0.png -- wheel5.png files inside the res/drawable/ folder --> <animation-list android:id="selected" android:oneshot="false"> <item android:drawable="@drawable/wheel0" android:duration="50" /> <item android:drawable="@drawable/wheel1" android:duration="50" /> <item android:drawable="@drawable/wheel2" android:duration="50" /> <item android:drawable="@drawable/wheel3" android:duration="50" /> <item android:drawable="@drawable/wheel4" android:duration="50" /> <item android:drawable="@drawable/wheel5" android:duration="50" /> </animation-list>
Here is the Java code to load and play this animation.
// Load the ImageView that will host the animation and // set its background to our AnimationDrawable XML resource. ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image); img.setBackgroundResource(R.drawable.spin_animation); // Get the background, which has been compiled to an AnimationDrawable object. AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); // Start the animation (looped playback by default). frameAnimation.start()
AnimationDrawable() |
void | addFrame(Drawable frame, int duration) | |||||
Add a frame to the animation | ||||||
int | getDuration(int i) | |||||
Drawable | getFrame(int index) | |||||
int | getNumberOfFrames() | |||||
void | inflate(Resources r, XmlPullParser parser, AttributeSet attrs) | |||||
boolean | isOneShot() | |||||
boolean | isRunning() | |||||
Indicates whether the animation is currently running or not. |
||||||
void | run() | |||||
This method exists for implementation purpose only and should not be called directly. |
||||||
void | setOneShot(boolean oneShot) | |||||
Sets whether the animation should play once or repeat. | ||||||
boolean | setVisible(boolean visible, boolean restart) | |||||
Set whether this Drawable is visible. | ||||||
void | start() | |||||
Starts the animation, looping if necessary. |
||||||
void | stop() | |||||
Stops the animation. |
||||||
void | unscheduleSelf(Runnable what) | |||||
Use the current Drawable.Callback implementation to have this Drawable unscheduled. |
frame | The frame to add |
---|---|
duration | How long in milliseconds the frame should appear |
XmlPullParserException | |
---|---|
IOException |
Indicates whether the animation is currently running or not.
oneShot | Pass true if the animation should only play once |
---|
visible | Set to true if visible, false if not. |
---|---|
restart | You can supply true here to force the drawable to behave as if it has just become visible, even if it had last been set visible. Used for example to force animations to restart. |
Starts the animation, looping if necessary. This method has no effect if the animation is running.
Stops the animation. This method has no effect if the animation is not running.
what | The runnable that you no longer want called. |
---|
Copyright 2007 Google Inc. | Build 0.9_r1-98467 - 14 Aug 2008 18:48 |