Most visited

Recently visited

Added in API level 1

AnimationDrawable

public class AnimationDrawable
extends DrawableContainer implements Runnable, Animatable

java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.graphics.drawable.DrawableContainer
       ↳ android.graphics.drawable.AnimationDrawable


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, call start() 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. See the example below.

spin_animation.xml file in res/drawable/ folder:

 <!-- Animation frames are wheel0.png through wheel5.png
     files inside the res/drawable/ folder -->
 <animation-list android:id="@+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 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();
 

Developer Guides

For more information about animating with AnimationDrawable, read the Drawable Animation developer guide.

Summary

XML attributes

android:drawable Reference to a drawable resource to use for the frame. 
android:duration Amount of time (in milliseconds) to display this frame. 
android:oneshot If true, the animation will only run a single time and then stop. 
android:variablePadding If true, allows the drawable's padding to change based on the current state that is selected. 
android:visible Provides initial visibility state of the drawable; the default value is false. 

Public constructors

AnimationDrawable()

Public methods

void addFrame(Drawable frame, int duration)

Adds a frame to the animation

int getDuration(int i)
Drawable getFrame(int index)
int getNumberOfFrames()
void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme.

boolean isOneShot()
boolean isRunning()

Indicates whether the animation is currently running or not.

Drawable mutate()

Make this drawable mutable.

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)

Sets whether this AnimationDrawable is visible.

void start()

Starts the animation from the first frame, looping if necessary.

void stop()

Stops the animation at the current frame.

void unscheduleSelf(Runnable what)

Use the current Drawable.Callback implementation to have this Drawable unscheduled.

Protected methods

void setConstantState(DrawableContainer.DrawableContainerState state)

Inherited methods

From class android.graphics.drawable.DrawableContainer
From class android.graphics.drawable.Drawable
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface java.lang.Runnable
From interface android.graphics.drawable.Animatable

XML attributes

android:drawable

Reference to a drawable resource to use for the frame. If not given, the drawable must be defined by the first child tag.

Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name".

This corresponds to the global attribute resource symbol drawable.

android:duration

Amount of time (in milliseconds) to display this frame.

Must be an integer value, such as "100".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol duration.

android:oneshot

If true, the animation will only run a single time and then stop. If false (the default), it will continually run, restarting at the first frame after the last has finished.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol oneshot.

android:variablePadding

If true, allows the drawable's padding to change based on the current state that is selected. If false, the padding will stay the same (based on the maximum padding of all the states). Enabling this feature requires that the owner of the drawable deal with performing layout when the state changes, which is often not supported.

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol variablePadding.

android:visible

Provides initial visibility state of the drawable; the default value is false. See setVisible(boolean, boolean).

Must be a boolean value, either "true" or "false".

This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:][type:]name") containing a value of this type.

This corresponds to the global attribute resource symbol visible.

Public constructors

AnimationDrawable

Added in API level 1
AnimationDrawable ()

Public methods

addFrame

Added in API level 1
void addFrame (Drawable frame, 
                int duration)

Adds a frame to the animation

Parameters
frame Drawable: The frame to add
duration int: How long in milliseconds the frame should appear

getDuration

Added in API level 1
int getDuration (int i)

Parameters
i int
Returns
int The duration in milliseconds of the frame at the specified index

getFrame

Added in API level 1
Drawable getFrame (int index)

Parameters
index int
Returns
Drawable The Drawable at the specified frame index

getNumberOfFrames

Added in API level 1
int getNumberOfFrames ()

Returns
int The number of frames in the animation

inflate

Added in API level 21
void inflate (Resources r, 
                XmlPullParser parser, 
                AttributeSet attrs, 
                Resources.Theme theme)

Inflate this Drawable from an XML resource optionally styled by a theme. This can't be called more than once for each Drawable. Note that framework may have called this once to create the Drawable instance from XML resource.

Parameters
r Resources: Resources used to resolve attribute values
parser XmlPullParser: XML parser from which to inflate this Drawable
attrs AttributeSet: Base set of attribute values
theme Resources.Theme: Theme to apply, may be null
Throws
XmlPullParserException
IOException

isOneShot

Added in API level 1
boolean isOneShot ()

Returns
boolean True of the animation will play once, false otherwise

isRunning

Added in API level 1
boolean isRunning ()

Indicates whether the animation is currently running or not.

Returns
boolean true if the animation is running, false otherwise

mutate

Added in API level 3
Drawable mutate ()

Make this drawable mutable. This operation cannot be reversed. A mutable drawable is guaranteed to not share its state with any other drawable. This is especially useful when you need to modify properties of drawables loaded from resources. By default, all drawables instances loaded from the same resource share a common state; if you modify the state of one instance, all the other instances will receive the same modification. Calling this method on a mutable Drawable will have no effect.

Returns
Drawable This drawable.

run

Added in API level 1
void run ()

This method exists for implementation purpose only and should not be called directly. Invoke start() instead.

See also:

setOneShot

Added in API level 1
void setOneShot (boolean oneShot)

Sets whether the animation should play once or repeat.

Parameters
oneShot boolean: Pass true if the animation should only play once

setVisible

Added in API level 1
boolean setVisible (boolean visible, 
                boolean restart)

Sets whether this AnimationDrawable is visible.

When the drawable becomes invisible, it will pause its animation. A subsequent change to visible with restart set to true will restart the animation from the first frame. If restart is false, the drawable will resume from the most recent frame. If the drawable has already reached the last frame, it will then loop back to the first frame, unless it's a one shot drawable (set through setOneShot(boolean)), in which case, it will stay on the last frame.

Parameters
visible boolean: true if visible, false otherwise
restart boolean: when visible, true to force the animation to restart from the first frame
Returns
boolean true if the new visibility is different than its previous state

start

Added in API level 1
void start ()

Starts the animation from the first frame, looping if necessary. This method has no effect if the animation is running.

Note: Do not call this in the onCreate(Bundle) method of your activity, because the AnimationDrawable is not yet fully attached to the window. If you want to play the animation immediately without requiring interaction, then you might want to call it from the onWindowFocusChanged(boolean) method in your activity, which will get called when Android brings your window into focus.

See also:

stop

Added in API level 1
void stop ()

Stops the animation at the current frame. This method has no effect if the animation is not running.

See also:

unscheduleSelf

Added in API level 1
void unscheduleSelf (Runnable what)

Use the current Drawable.Callback implementation to have this Drawable unscheduled. Does nothing if there is no Callback attached to the Drawable.

Parameters
what Runnable: The runnable that you no longer want called.

Protected methods

setConstantState

Added in API level 1
void setConstantState (DrawableContainer.DrawableContainerState state)

Parameters
state DrawableContainer.DrawableContainerState

Hooray!