Android

android.graphics.drawable.AnimationDrawable

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()
 

Summary

Public Constructors

            AnimationDrawable()

Public Methods

          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
Methods inherited from class android.graphics.drawable.Drawable
Methods inherited from class java.lang.Object
Methods inherited from interface android.graphics.drawable.Drawable.Callback
Methods inherited from interface java.lang.Runnable

Details

Public Constructors

public AnimationDrawable()

Public Methods

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

public int getDuration(int i)

Returns

  • The duration in milliseconds of the frame at the specified index

public Drawable getFrame(int index)

Returns

  • The Drawable at the specified frame index

public int getNumberOfFrames()

Returns

  • The number of frames in the animation

public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs)

Throws

XmlPullParserException
IOException

public boolean isOneShot()

Returns

  • True of the animation will play once, false otherwise

public boolean isRunning()

Indicates whether the animation is currently running or not.

Returns

  • true if the animation is running, false otherwise

public void run()

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

See Also

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

public boolean setVisible(boolean visible, boolean restart)

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.

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.

public void start()

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

See Also

public void stop()

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

See Also

public 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 The runnable that you no longer want called.
Copyright 2007 Google Inc. Build 0.9_r1-98467 - 14 Aug 2008 18:56