public class AnimationDrawable extends DrawableContainer implements Runnable, Animatable
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();
For more information about animating with AnimationDrawable
, read the
Drawable Animation
developer guide.
DrawableContainer.DrawableContainerState
Drawable.Callback, Drawable.ConstantState
Constructor and Description |
---|
AnimationDrawable() |
Modifier and Type | Method and Description |
---|---|
void |
addFrame(Drawable frame,
int duration)
Adds a frame to the animation
|
void |
clearMutated()
Clears the mutated state, allowing this drawable to be cached and
mutated again.
|
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.
|
protected void |
setConstantState(DrawableContainer.DrawableContainerState state) |
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. |
applyTheme, canApplyTheme, draw, getAlpha, getChangingConfigurations, getConstantState, getCurrent, getCurrentIndex, getHotspotBounds, getIntrinsicHeight, getIntrinsicWidth, getMinimumHeight, getMinimumWidth, getOpacity, getOpticalInsets, getOutline, getPadding, invalidateDrawable, isAutoMirrored, isStateful, jumpToCurrentState, onBoundsChange, onLayoutDirectionChanged, onLevelChange, onStateChange, scheduleDrawable, selectDrawable, setAlpha, setAutoMirrored, setColorFilter, setCurrentIndex, setDither, setEnterFadeDuration, setExitFadeDuration, setHotspot, setHotspotBounds, setTintList, setTintMode, unscheduleDrawable, updateDensity
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getColorFilter, getDirtyBounds, getLayoutDirection, getLevel, getState, getTransparentRegion, inflate, invalidateSelf, isFilterBitmap, isProjected, isVisible, obtainAttributes, parseTintMode, resolveOpacity, scheduleSelf, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setLayoutDirection, setLevel, setState, setTint, setXfermode
public boolean setVisible(boolean visible, boolean restart)
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.
setVisible
in class DrawableContainer
visible
- true if visible, false otherwiserestart
- when visible, true to force the animation to restart
from the first framepublic void start()
Note: Do not call this in the
Activity.onCreate(android.os.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
Activity.onWindowFocusChanged(boolean)
method in your
activity, which will get called when Android brings your window into
focus.
start
in interface Animatable
isRunning()
,
stop()
public void stop()
stop
in interface Animatable
isRunning()
,
start()
public boolean isRunning()
isRunning
in interface Animatable
public void run()
start()
instead.public void unscheduleSelf(Runnable what)
Drawable
Drawable.Callback
implementation to have this Drawable
unscheduled. Does nothing if there is no Callback attached to the
Drawable.unscheduleSelf
in class Drawable
what
- The runnable that you no longer want called.Drawable.Callback.unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable)
public int getNumberOfFrames()
public Drawable getFrame(int index)
public int getDuration(int i)
public boolean isOneShot()
public void setOneShot(boolean oneShot)
oneShot
- Pass true if the animation should only play oncepublic void addFrame(Drawable frame, int duration)
frame
- The frame to addduration
- How long in milliseconds the frame should appearpublic void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
r
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
public Drawable mutate()
Drawable
mutate
in class DrawableContainer
Drawable.ConstantState
,
Drawable.getConstantState()
public void clearMutated()
Drawable
This is hidden because only framework drawables can be cached, so custom drawables don't need to support constant state, mutate(), or clearMutated().
clearMutated
in class DrawableContainer
protected void setConstantState(DrawableContainer.DrawableContainerState state)
setConstantState
in class DrawableContainer