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:
@@ -17,7 +17,9 @@
|
|||||||
package com.android.settings.accessibility;
|
package com.android.settings.accessibility;
|
||||||
|
|
||||||
import android.content.Context;
|
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.graphics.drawable.Drawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.widget.ImageView;
|
import android.widget.ImageView;
|
||||||
@@ -36,6 +38,14 @@ public class AnimatedImagePreference extends Preference {
|
|||||||
private Uri mImageUri;
|
private Uri mImageUri;
|
||||||
private int mMaxHeight = -1;
|
private int mMaxHeight = -1;
|
||||||
|
|
||||||
|
private final Animatable2.AnimationCallback mAnimationCallback =
|
||||||
|
new Animatable2.AnimationCallback() {
|
||||||
|
@Override
|
||||||
|
public void onAnimationEnd(Drawable drawable) {
|
||||||
|
((Animatable2) drawable).start();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
AnimatedImagePreference(Context context) {
|
AnimatedImagePreference(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
setLayoutResource(R.layout.preference_animated_image);
|
setLayoutResource(R.layout.preference_animated_image);
|
||||||
@@ -51,12 +61,10 @@ public class AnimatedImagePreference extends Preference {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (mImageUri != null) {
|
if (mImageUri != null) {
|
||||||
imageView.setImageURI(mImageUri);
|
resetAnimation(imageView.getDrawable());
|
||||||
|
|
||||||
final Drawable drawable = imageView.getDrawable();
|
imageView.setImageURI(mImageUri);
|
||||||
if (drawable instanceof AnimatedImageDrawable) {
|
startAnimation(imageView.getDrawable());
|
||||||
((AnimatedImageDrawable) drawable).start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mMaxHeight > -1) {
|
if (mMaxHeight > -1) {
|
||||||
@@ -87,4 +95,30 @@ public class AnimatedImagePreference extends Preference {
|
|||||||
notifyChanged();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -19,12 +19,15 @@ package com.android.settings.accessibility;
|
|||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.Mockito.doReturn;
|
import static org.mockito.Mockito.doReturn;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.spy;
|
import static org.mockito.Mockito.spy;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.drawable.AnimatedImageDrawable;
|
import android.graphics.drawable.AnimatedImageDrawable;
|
||||||
|
import android.graphics.drawable.AnimatedVectorDrawable;
|
||||||
|
import android.graphics.drawable.AnimationDrawable;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@@ -37,7 +40,6 @@ import com.android.settings.R;
|
|||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.mockito.Mock;
|
|
||||||
import org.mockito.MockitoAnnotations;
|
import org.mockito.MockitoAnnotations;
|
||||||
import org.mockito.Spy;
|
import org.mockito.Spy;
|
||||||
import org.robolectric.RobolectricTestRunner;
|
import org.robolectric.RobolectricTestRunner;
|
||||||
@@ -54,9 +56,6 @@ public class AnimatedImagePreferenceTest {
|
|||||||
@Spy
|
@Spy
|
||||||
private ImageView mImageView;
|
private ImageView mImageView;
|
||||||
|
|
||||||
@Mock
|
|
||||||
private AnimatedImageDrawable mAnimatedImageDrawable;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
MockitoAnnotations.initMocks(this);
|
MockitoAnnotations.initMocks(this);
|
||||||
@@ -72,14 +71,39 @@ public class AnimatedImagePreferenceTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void readImageUri_animatedImage_startAnimation() {
|
public void playAnimation_animatedImageDrawable_success() {
|
||||||
|
final AnimatedImageDrawable drawable = mock(AnimatedImageDrawable.class);
|
||||||
doReturn(mImageView).when(mRootView).findViewById(R.id.animated_img);
|
doReturn(mImageView).when(mRootView).findViewById(R.id.animated_img);
|
||||||
doReturn(mAnimatedImageDrawable).when(mImageView).getDrawable();
|
doReturn(drawable).when(mImageView).getDrawable();
|
||||||
|
|
||||||
mAnimatedImagePreference.setImageUri(mImageUri);
|
mAnimatedImagePreference.setImageUri(mImageUri);
|
||||||
mAnimatedImagePreference.onBindViewHolder(mViewHolder);
|
mAnimatedImagePreference.onBindViewHolder(mViewHolder);
|
||||||
|
|
||||||
verify(mAnimatedImageDrawable).start();
|
verify(drawable).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void playAnimation_animatedVectorDrawable_success() {
|
||||||
|
final AnimatedVectorDrawable drawable = mock(AnimatedVectorDrawable.class);
|
||||||
|
doReturn(mImageView).when(mRootView).findViewById(R.id.animated_img);
|
||||||
|
doReturn(drawable).when(mImageView).getDrawable();
|
||||||
|
|
||||||
|
mAnimatedImagePreference.setImageUri(mImageUri);
|
||||||
|
mAnimatedImagePreference.onBindViewHolder(mViewHolder);
|
||||||
|
|
||||||
|
verify(drawable).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void playAnimation_animationDrawable_success() {
|
||||||
|
final AnimationDrawable drawable = mock(AnimationDrawable.class);
|
||||||
|
doReturn(mImageView).when(mRootView).findViewById(R.id.animated_img);
|
||||||
|
doReturn(drawable).when(mImageView).getDrawable();
|
||||||
|
|
||||||
|
mAnimatedImagePreference.setImageUri(mImageUri);
|
||||||
|
mAnimatedImagePreference.onBindViewHolder(mViewHolder);
|
||||||
|
|
||||||
|
verify(drawable).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Reference in New Issue
Block a user