Adjust AnimationListener for infinitely animated webp
@@ -66,6 +66,8 @@ public class AnimatedDrawable2 extends Drawable implements Animatable, DrawableW
private volatile boolean mIsRunning;
private long mStartTimeMs;
private long mLastFrameAnimationTimeMs;
+ private long mExpectedRenderTimeMs;
+ private int mLastDrawnFrameNumber;
private long mFrameSchedulingDelayMs = DEFAULT_FRAME_SCHEDULING_DELAY_MS;
private long mFrameSchedulingOffsetMs = DEFAULT_FRAME_SCHEDULING_OFFSET_MS;
@@ -134,7 +136,9 @@ public class AnimatedDrawable2 extends Drawable implements Animatable, DrawableW
}
mIsRunning = true;
mStartTimeMs = now();
+ mExpectedRenderTimeMs = mStartTimeMs;
mLastFrameAnimationTimeMs = -1;
+ mLastDrawnFrameNumber = -1;
invalidateSelf();
mAnimationListener.onAnimationStart(this);
}
@@ -149,7 +153,9 @@ public class AnimatedDrawable2 extends Drawable implements Animatable, DrawableW
}
mIsRunning = false;
mStartTimeMs = 0;
+ mExpectedRenderTimeMs = mStartTimeMs;
mLastFrameAnimationTimeMs = -1;
+ mLastDrawnFrameNumber = -1;
unscheduleSelf(mInvalidateRunnable);
mAnimationListener.onAnimationStop(this);
}
@@ -193,15 +199,19 @@ public class AnimatedDrawable2 extends Drawable implements Animatable, DrawableW
mAnimationListener.onAnimationStop(this);
mIsRunning = false;
} else if (frameNumberToDraw == 0) {
+ if (mLastDrawnFrameNumber != -1 && actualRenderTimeStartMs >= mExpectedRenderTimeMs) {
mAnimationListener.onAnimationRepeat(this);
}
-
- // Notify listeners that we're about to draw a new frame and
- // that the animation might be repeated
- mAnimationListener.onAnimationFrame(this, frameNumberToDraw);
+ }
// Draw the frame
boolean frameDrawn = mAnimationBackend.drawFrame(this, canvas, frameNumberToDraw);
+ if (frameDrawn) {
+ // Notify listeners that we draw a new frame and
+ // that the animation might be repeated
+ mAnimationListener.onAnimationFrame(this, frameNumberToDraw);
+ mLastDrawnFrameNumber = frameNumberToDraw;
+ }
// Log potential dropped frames
if (!frameDrawn) {
@@ -317,6 +327,7 @@ public class AnimatedDrawable2 extends Drawable implements Animatable, DrawableW
// In order to jump to a given frame, we have to compute the correct start time
mLastFrameAnimationTimeMs = mFrameScheduler.getTargetRenderTimeMs(targetFrameNumber);
mStartTimeMs = now() - mLastFrameAnimationTimeMs;
+ mExpectedRenderTimeMs = mStartTimeMs;
invalidateSelf();
}
@@ -412,7 +423,8 @@ public class AnimatedDrawable2 extends Drawable implements Animatable, DrawableW
* @param targetAnimationTimeMs the time in ms to update the frame
*/
private void scheduleNextFrame(long targetAnimationTimeMs) {
- scheduleSelf(mInvalidateRunnable, mStartTimeMs + targetAnimationTimeMs);
+ mExpectedRenderTimeMs = mStartTimeMs + targetAnimationTimeMs;
+ scheduleSelf(mInvalidateRunnable, mExpectedRenderTimeMs);
}
private void onFrameDropped() {
