English | Site Directory

Android - An Open Handset Alliance Project

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 -->
 <frames id="selected" oneshot="false">
    <frame drawable="@drawable/wheel0" duration="50" />
    <frame drawable="@drawable/wheel1" duration="50" />
    <frame drawable="@drawable/wheel2" duration="50" />
    <frame drawable="@drawable/wheel3" duration="50" />
    <frame drawable="@drawable/wheel4" duration="50" />
    <frame drawable="@drawable/wheel5" duration="50" />
 </frames>

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.setBackground(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  inflate(Resources r, XmlPullParser parser, AttributeSet attrs)
        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.

        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 inflate(Resources r, XmlPullParser parser, AttributeSet attrs)

Throws

XmlPullParserException
IOException

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 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.
Build m5-rc15g - 14 May 2008 12:50