Build 1.1_r1 (from source)

android.graphics.drawable
Class AnimationDrawable

java.lang.Object
  extended by android.graphics.drawable.Drawable
      extended by android.graphics.drawable.DrawableContainer
          extended by android.graphics.drawable.AnimationDrawable
All Implemented Interfaces:
Drawable.Callback, Runnable

public class AnimationDrawable
extends DrawableContainer
implements Runnable

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 run() to start 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 -- 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 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()
 


Nested Class Summary
 
Nested classes/interfaces inherited from class android.graphics.drawable.DrawableContainer
DrawableContainer.DrawableContainerState
 
Nested classes/interfaces inherited from class android.graphics.drawable.Drawable
Drawable.Callback, Drawable.ConstantState
 
Field Summary
 
Fields inherited from class android.graphics.drawable.Drawable
mCallback
 
Constructor Summary
AnimationDrawable()
           
 
Method Summary
 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.
 
Methods inherited from class android.graphics.drawable.DrawableContainer
draw, getChangingConfigurations, getConstantState, getCurrent, getIntrinsicHeight, getIntrinsicWidth, getMinimumHeight, getMinimumWidth, getOpacity, getPadding, invalidateDrawable, isStateful, onBoundsChange, onLevelChange, onStateChange, scheduleDrawable, selectDrawable, setAlpha, setColorFilter, setConstantState, setDither, unscheduleDrawable
 
Methods inherited from class android.graphics.drawable.Drawable
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromStream, createFromXml, createFromXmlInner, getBounds, getLevel, getState, getTransparentRegion, inflateWithAttributes, invalidateSelf, isVisible, resolveOpacity, scheduleSelf, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setFilterBitmap, setLevel, setState
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AnimationDrawable

public AnimationDrawable()
Method Detail

setVisible

public boolean setVisible(boolean visible,
                          boolean restart)
Description copied from class: Drawable
Set whether this Drawable is visible. This generally does not impact the Drawable's behavior, but is a hint that can be used by some Drawables, for example, to decide whether run animations.

Overrides:
setVisible in class DrawableContainer
Parameters:
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.
Returns:
boolean Returns true if the new visibility is different than its previous state.

start

public void start()

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

See Also:
isRunning(), stop()

stop

public void stop()

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

See Also:
isRunning(), start()

isRunning

public boolean isRunning()

Indicates whether the animation is currently running or not.

Returns:
true if the animation is running, false otherwise

run

public void run()

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

Specified by:
run in interface Runnable
See Also:
start()

unscheduleSelf

public void unscheduleSelf(Runnable what)
Description copied from class: Drawable
Use the current Drawable.Callback implementation to have this Drawable unscheduled. Does nothing if there is no Callback attached to the Drawable.

Overrides:
unscheduleSelf in class Drawable
Parameters:
what - The runnable that you no longer want called.
See Also:
Drawable.Callback.unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable)

getNumberOfFrames

public int getNumberOfFrames()
Returns:
The number of frames in the animation

getFrame

public Drawable getFrame(int index)
Returns:
The Drawable at the specified frame index

getDuration

public int getDuration(int i)
Returns:
The duration in milliseconds of the frame at the specified index

isOneShot

public boolean isOneShot()
Returns:
True of the animation will play once, false otherwise

setOneShot

public void setOneShot(boolean oneShot)
Sets whether the animation should play once or repeat.

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

addFrame

public void addFrame(Drawable frame,
                     int duration)
Add a frame to the animation

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

inflate

public void inflate(Resources r,
                    XmlPullParser parser,
                    AttributeSet attrs)
             throws XmlPullParserException,
                    IOException
Overrides:
inflate in class Drawable
Throws:
XmlPullParserException
IOException

Build 1.1_r1 (from source)

Please submit a feedback, bug or feature