From 5e0c72a2c593de712debf7294e39fb36553a2b30 Mon Sep 17 00:00:00 2001 From: wilsonshih Date: Tue, 8 Feb 2022 10:06:28 +0800 Subject: Estimate animation duration for AVD and AnimationDrawable For Android S, we use the API windowSplashScreenAnimationDuration to know the duration of the animatable icon would be, however, developers can be confused to thought that this API is used to declare the duration of splash screen. To get the real duration of the animatable icon, if it is an AVD or AnimationDrawable, try to get the animation duration from it. Test: atest StartingSurfaceDrawerTests SplashscreenTests Bug: 211707095 Change-Id: Ia3f8c5cd50bab164360e07d9ced87ff7f4321e96 --- .../graphics/drawable/AnimatedVectorDrawable.java | 27 ++++++++++++++++++++-- .../graphics/drawable/AnimationDrawable.java | 19 +++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) (limited to 'graphics/java') diff --git a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java index 33b09b8831ce..55f205bb14a6 100644 --- a/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java +++ b/graphics/java/android/graphics/drawable/AnimatedVectorDrawable.java @@ -690,6 +690,14 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { } } + /** + * Gets the total duration of the animation + * @hide + */ + public long getTotalDuration() { + return mAnimatorSet.getTotalDuration(); + } + private static class AnimatedVectorDrawableState extends ConstantState { @Config int mChangingConfigurations; VectorDrawable mVectorDrawable; @@ -1074,6 +1082,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { boolean isInfinite(); void pause(); void resume(); + long getTotalDuration(); } private static class VectorDrawableAnimatorUI implements VectorDrawableAnimator { @@ -1085,6 +1094,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { // setup by init(). private ArrayList mListenerArray = null; private boolean mIsInfinite = false; + private long mTotalDuration; VectorDrawableAnimatorUI(@NonNull AnimatedVectorDrawable drawable) { mDrawable = drawable; @@ -1100,7 +1110,8 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { // Keep a deep copy of the set, such that set can be still be constantly representing // the static content from XML file. mSet = set.clone(); - mIsInfinite = mSet.getTotalDuration() == Animator.DURATION_INFINITE; + mTotalDuration = mSet.getTotalDuration(); + mIsInfinite = mTotalDuration == Animator.DURATION_INFINITE; // If there are listeners added before calling init(), now they should be setup. if (mListenerArray != null && !mListenerArray.isEmpty()) { @@ -1219,6 +1230,11 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { private void invalidateOwningView() { mDrawable.invalidateSelf(); } + + @Override + public long getTotalDuration() { + return mTotalDuration; + } } /** @@ -1249,6 +1265,7 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { private int mLastListenerId = 0; private final IntArray mPendingAnimationActions = new IntArray(); private final AnimatedVectorDrawable mDrawable; + private long mTotalDuration; VectorDrawableAnimatorRT(AnimatedVectorDrawable drawable) { mDrawable = drawable; @@ -1270,7 +1287,8 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { .getNativeTree(); nSetVectorDrawableTarget(mSetPtr, vectorDrawableTreePtr); mInitialized = true; - mIsInfinite = set.getTotalDuration() == Animator.DURATION_INFINITE; + mTotalDuration = set.getTotalDuration(); + mIsInfinite = mTotalDuration == Animator.DURATION_INFINITE; // Check reversible. mIsReversible = true; @@ -1796,6 +1814,11 @@ public class AnimatedVectorDrawable extends Drawable implements Animatable2 { } mPendingAnimationActions.clear(); } + + @Override + public long getTotalDuration() { + return mTotalDuration; + } } private static native long nCreateAnimatorSet(); diff --git a/graphics/java/android/graphics/drawable/AnimationDrawable.java b/graphics/java/android/graphics/drawable/AnimationDrawable.java index 8c3fa441cbb0..7fd2201e7108 100644 --- a/graphics/java/android/graphics/drawable/AnimationDrawable.java +++ b/graphics/java/android/graphics/drawable/AnimationDrawable.java @@ -424,6 +424,17 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An System.arraycopy(mDurations, 0, newDurations, 0, oldSize); mDurations = newDurations; } + + public long getTotalDuration() { + if (mDurations != null) { + int total = 0; + for (int dur : mDurations) { + total += dur; + } + return total; + } + return 0; + } } @Override @@ -435,6 +446,14 @@ public class AnimationDrawable extends DrawableContainer implements Runnable, An } } + /** + * Gets the total duration of the animation + * @hide + */ + public long getTotalDuration() { + return mAnimationState.getTotalDuration(); + } + private AnimationDrawable(AnimationState state, Resources res) { final AnimationState as = new AnimationState(state, this, res); setConstantState(as); -- cgit v1.2.3-59-g8ed1b