From 100a2d1fc67d79d19afa0c00830350a5d4ab0c59 Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Fri, 27 Jun 2014 12:35:42 -0700 Subject: [PATCH] Clean up AnimationDrawable.setVisible() state management BUG: 15823683 Change-Id: Ied0d476956c246bdfda66219aaa896f2ef58f0bd --- .../graphics/drawable/AnimationDrawable.java | 37 ++++++++++++++++--- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/graphics/java/android/graphics/drawable/AnimationDrawable.java b/graphics/java/android/graphics/drawable/AnimationDrawable.java index 074076151d564..cef3377a8fac2 100644 --- a/graphics/java/android/graphics/drawable/AnimationDrawable.java +++ b/graphics/java/android/graphics/drawable/AnimationDrawable.java @@ -80,20 +80,41 @@ import android.util.AttributeSet; */ public class AnimationDrawable extends DrawableContainer implements Runnable, Animatable { private final AnimationState mAnimationState; + + /** The current frame, may be -1 when not animating. */ private int mCurFrame = -1; + + /** Whether the drawable has an animation callback posted. */ + private boolean mRunning; + + /** Whether the drawable should animate when visible. */ private boolean mAnimating; + private boolean mMutated; public AnimationDrawable() { this(null, null); } + /** + * 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. + * + * @param visible true if visible, false otherwise + * @param restart when visible, true to force the animation to restart + * from the first frame + * @return true if the new visibility is different than its previous state + */ @Override public boolean setVisible(boolean visible, boolean restart) { final boolean changed = super.setVisible(visible, restart); if (visible) { - if (changed || restart) { - setFrame(0, true, mAnimating); + if (restart || changed) { + setFrame(restart ? 0 : mCurFrame, true, mAnimating); } } else { unscheduleSelf(this); @@ -115,6 +136,8 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An */ @Override public void start() { + mAnimating = true; + if (!isRunning()) { run(); } @@ -129,6 +152,8 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An */ @Override public void stop() { + mAnimating = false; + if (isRunning()) { unscheduleSelf(this); } @@ -141,7 +166,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An */ @Override public boolean isRunning() { - return mAnimating; + return mRunning; } /** @@ -158,7 +183,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An @Override public void unscheduleSelf(Runnable what) { mCurFrame = -1; - mAnimating = false; + mRunning = false; super.unscheduleSelf(what); } @@ -219,6 +244,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An if (next >= N) { next = 0; } + setFrame(next, unschedule, !mAnimationState.mOneShot || next < (N - 1)); } @@ -226,6 +252,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An if (frame >= mAnimationState.getChildCount()) { return; } + mAnimating = animate; mCurFrame = frame; selectDrawable(frame); if (unschedule || animate) { @@ -234,7 +261,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An if (animate) { // Unscheduling may have clobbered these values; restore them mCurFrame = frame; - mAnimating = true; + mRunning = true; scheduleSelf(this, SystemClock.uptimeMillis() + mAnimationState.mDurations[frame]); } }