AnimationDrawablepublic class AnimationDrawable extends DrawableContainer implements Runnable, AnimatableAn 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
{@link #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 -- 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 {@code AnimationDrawable}, read the
Drawable Animation
developer guide.
|
Fields Summary |
---|
private AnimationState | mAnimationState | private int | mCurFrameThe current frame, may be -1 when not animating. | private boolean | mRunningWhether the drawable has an animation callback posted. | private boolean | mAnimatingWhether the drawable should animate when visible. | private boolean | mMutated |
Constructors Summary |
---|
public AnimationDrawable()
this(null, null);
| private AnimationDrawable(AnimationState state, android.content.res.Resources res)
final AnimationState as = new AnimationState(state, this, res);
setConstantState(as);
if (state != null) {
setFrame(0, true, false);
}
|
Methods Summary |
---|
public void | addFrame(Drawable frame, int duration)Add a frame to the animation
mAnimationState.addFrame(frame, duration);
if (mCurFrame < 0) {
setFrame(0, true, false);
}
| public void | clearMutated()
super.clearMutated();
mMutated = false;
| android.graphics.drawable.AnimationDrawable$AnimationState | cloneConstantState()
return new AnimationState(mAnimationState, this, null);
| public int | getDuration(int i)
return mAnimationState.mDurations[i];
| public Drawable | getFrame(int index)
return mAnimationState.getChild(index);
| public int | getNumberOfFrames()
return mAnimationState.getChildCount();
| public void | inflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.content.res.Resources.Theme theme)
final TypedArray a = obtainAttributes(r, theme, attrs, R.styleable.AnimationDrawable);
super.inflateWithAttributes(r, parser, a, R.styleable.AnimationDrawable_visible);
updateStateFromTypedArray(a);
a.recycle();
inflateChildElements(r, parser, attrs, theme);
setFrame(0, true, false);
| private void | inflateChildElements(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs, android.content.res.Resources.Theme theme)
int type;
final int innerDepth = parser.getDepth()+1;
int depth;
while ((type=parser.next()) != XmlPullParser.END_DOCUMENT
&& ((depth = parser.getDepth()) >= innerDepth || type != XmlPullParser.END_TAG)) {
if (type != XmlPullParser.START_TAG) {
continue;
}
if (depth > innerDepth || !parser.getName().equals("item")) {
continue;
}
final TypedArray a = obtainAttributes(r, theme, attrs,
R.styleable.AnimationDrawableItem);
final int duration = a.getInt(R.styleable.AnimationDrawableItem_duration, -1);
if (duration < 0) {
throw new XmlPullParserException(parser.getPositionDescription()
+ ": <item> tag requires a 'duration' attribute");
}
Drawable dr = a.getDrawable(R.styleable.AnimationDrawableItem_drawable);
a.recycle();
if (dr == null) {
while ((type=parser.next()) == XmlPullParser.TEXT) {
// Empty
}
if (type != XmlPullParser.START_TAG) {
throw new XmlPullParserException(parser.getPositionDescription()
+ ": <item> tag requires a 'drawable' attribute or child tag"
+ " defining a drawable");
}
dr = Drawable.createFromXmlInner(r, parser, attrs, theme);
}
mAnimationState.addFrame(dr, duration);
if (dr != null) {
dr.setCallback(this);
}
}
| public boolean | isOneShot()
return mAnimationState.mOneShot;
| public boolean | isRunning()Indicates whether the animation is currently running or not.
return mRunning;
| public Drawable | mutate()
if (!mMutated && super.mutate() == this) {
mAnimationState.mutate();
mMutated = true;
}
return this;
| private void | nextFrame(boolean unschedule)
int next = mCurFrame+1;
final int N = mAnimationState.getChildCount();
if (next >= N) {
next = 0;
}
setFrame(next, unschedule, !mAnimationState.mOneShot || next < (N - 1));
| public void | run()This method exists for implementation purpose only and should not be
called directly. Invoke {@link #start()} instead.
nextFrame(false);
| protected void | setConstantState(DrawableContainerState state)
super.setConstantState(state);
if (state instanceof AnimationState) {
mAnimationState = (AnimationState) state;
}
| private void | setFrame(int frame, boolean unschedule, boolean animate)
if (frame >= mAnimationState.getChildCount()) {
return;
}
mAnimating = animate;
mCurFrame = frame;
selectDrawable(frame);
if (unschedule || animate) {
unscheduleSelf(this);
}
if (animate) {
// Unscheduling may have clobbered these values; restore them
mCurFrame = frame;
mRunning = true;
scheduleSelf(this, SystemClock.uptimeMillis() + mAnimationState.mDurations[frame]);
}
| public void | setOneShot(boolean oneShot)Sets whether the animation should play once or repeat.
mAnimationState.mOneShot = oneShot;
| public 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 animation will resume from the most recent frame.
final boolean changed = super.setVisible(visible, restart);
if (visible) {
if (restart || changed) {
boolean startFromZero = restart || mCurFrame < 0 ||
mCurFrame >= mAnimationState.getChildCount();
setFrame(startFromZero ? 0 : mCurFrame, true, mAnimating);
}
} else {
unscheduleSelf(this);
}
return changed;
| public void | start()Starts the animation, looping if necessary. This method has no effect
if the animation is running. Do not call this in the {@link android.app.Activity#onCreate}
method of your activity, because the {@link android.graphics.drawable.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 {@link android.app.Activity#onWindowFocusChanged} method in your activity,
which will get called when Android brings your window into focus.
mAnimating = true;
if (!isRunning()) {
run();
}
| public void | stop()Stops the animation. This method has no effect if the animation is
not running.
mAnimating = false;
if (isRunning()) {
unscheduleSelf(this);
}
| public void | unscheduleSelf(java.lang.Runnable what)
mCurFrame = -1;
mRunning = false;
super.unscheduleSelf(what);
| private void | updateStateFromTypedArray(android.content.res.TypedArray a)
mAnimationState.mVariablePadding = a.getBoolean(
R.styleable.AnimationDrawable_variablePadding, mAnimationState.mVariablePadding);
mAnimationState.mOneShot = a.getBoolean(
R.styleable.AnimationDrawable_oneshot, mAnimationState.mOneShot);
|
|