Fixing the animation of drawable couldn't play automatically for the banner.

Root cause:
Only supported AnimatedImageDrawable before.

Solution:
Support all classes which implement Animatable

Bug: 190032215
Test: atest AnimatedImagePreferenceTest
Change-Id: Iba18bfab9a46fd02f642d66a3bb9b9ce513c4456
This commit is contained in:
Peter_Liang
2021-06-03 20:47:06 +08:00
parent 2076ebf2f7
commit b1bcedc055
2 changed files with 71 additions and 13 deletions

View File

@@ -17,7 +17,9 @@
package com.android.settings.accessibility;
import android.content.Context;
import android.graphics.drawable.AnimatedImageDrawable;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Animatable2;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;
@@ -36,6 +38,14 @@ public class AnimatedImagePreference extends Preference {
private Uri mImageUri;
private int mMaxHeight = -1;
private final Animatable2.AnimationCallback mAnimationCallback =
new Animatable2.AnimationCallback() {
@Override
public void onAnimationEnd(Drawable drawable) {
((Animatable2) drawable).start();
}
};
AnimatedImagePreference(Context context) {
super(context);
setLayoutResource(R.layout.preference_animated_image);
@@ -51,12 +61,10 @@ public class AnimatedImagePreference extends Preference {
}
if (mImageUri != null) {
imageView.setImageURI(mImageUri);
resetAnimation(imageView.getDrawable());
final Drawable drawable = imageView.getDrawable();
if (drawable instanceof AnimatedImageDrawable) {
((AnimatedImageDrawable) drawable).start();
}
imageView.setImageURI(mImageUri);
startAnimation(imageView.getDrawable());
}
if (mMaxHeight > -1) {
@@ -87,4 +95,30 @@ public class AnimatedImagePreference extends Preference {
notifyChanged();
}
}
private void startAnimation(Drawable drawable) {
if (!(drawable instanceof Animatable)) {
return;
}
if (drawable instanceof Animatable2) {
((Animatable2) drawable).registerAnimationCallback(mAnimationCallback);
} else if (drawable instanceof AnimationDrawable) {
((AnimationDrawable) drawable).setOneShot(false);
}
((Animatable) drawable).start();
}
private void resetAnimation(Drawable drawable) {
if (!(drawable instanceof Animatable)) {
return;
}
if (drawable instanceof Animatable2) {
((Animatable2) drawable).clearAnimationCallbacks();
}
((Animatable) drawable).stop();
}
}