OSDN Git Service

Check whether AnimationDrawable is one shot only when visibility changes
authorDoris Liu <tianliu@google.com>
Tue, 1 Sep 2015 23:03:58 +0000 (16:03 -0700)
committerDoris Liu <tianliu@google.com>
Wed, 2 Sep 2015 23:25:37 +0000 (23:25 +0000)
When AnimationDrawable becomes VISIBLE again, the current behavior is to
start the Drawable from the first frame if it's not running. For one shot
AnimationDrawable, it should start once and stay on the last frame.

Bug: 23725157
Change-Id: I5f3095aa00b07fefe7f992851d4190f268c266b2

graphics/java/android/graphics/drawable/AnimationDrawable.java

index e1975c9..521c74b 100644 (file)
@@ -105,10 +105,12 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An
     /**
      * Sets whether this AnimationDrawable is visible.
      * <p>
-     * When the drawable becomes invisible, it will pause its animation. A
-     * subsequent change to visible with <code>restart</code> set to true will
-     * restart the animation from the first frame. If <code>restart</code> is
-     * false, the animation will resume from the most recent frame.
+     * When the drawable becomes invisible, it will pause its animation. A subsequent change to
+     * visible with <code>restart</code> set to true will restart the animation from the
+     * first frame. If <code>restart</code> is false, the drawable will resume from the most recent
+     * frame. If the drawable has already reached the last frame, it will then loop back to the
+     * first frame, unless it's a one shot drawable (set through {@link #setOneShot(boolean)}),
+     * in which case, it will stay on the last frame.
      *
      * @param visible true if visible, false otherwise
      * @param restart when visible, true to force the animation to restart
@@ -120,7 +122,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An
         final boolean changed = super.setVisible(visible, restart);
         if (visible) {
             if (restart || changed) {
-                boolean startFromZero = restart || !mRunning ||
+                boolean startFromZero = restart || (!mRunning && !mAnimationState.mOneShot) ||
                         mCurFrame >= mAnimationState.getChildCount();
                 setFrame(startFromZero ? 0 : mCurFrame, true, mAnimating);
             }
@@ -131,7 +133,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An
     }
 
     /**
-     * Starts the animation, looping if necessary. This method has no effect
+     * Starts the animation from the first frame, looping if necessary. This method has no effect
      * if the animation is running.
      * <p>
      * <strong>Note:</strong> Do not call this in the
@@ -158,7 +160,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An
     }
 
     /**
-     * Stops the animation. This method has no effect if the animation is not
+     * Stops the animation at the current frame. This method has no effect if the animation is not
      * running.
      *
      * @see #isRunning()
@@ -169,6 +171,7 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An
         mAnimating = false;
 
         if (isRunning()) {
+            mCurFrame = 0;
             unscheduleSelf(this);
         }
     }
@@ -196,7 +199,6 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An
 
     @Override
     public void unscheduleSelf(Runnable what) {
-        mCurFrame = 0;
         mRunning = false;
         super.unscheduleSelf(what);
     }