Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support tint list on GifDrawable. #5341

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@
import static com.bumptech.glide.gifdecoder.GifDecoder.TOTAL_ITERATION_COUNT_FOREVER;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Animatable;
import android.graphics.drawable.Drawable;
Expand Down Expand Up @@ -45,6 +49,8 @@ public class GifDrawable extends Drawable

private static final int GRAVITY = Gravity.FILL;

private static final PorterDuff.Mode DEFAULT_TINT_MODE = PorterDuff.Mode.SRC_IN;

private final GifState state;
/** True if the drawable is currently animating. */
private boolean isRunning;
Expand All @@ -66,6 +72,10 @@ public class GifDrawable extends Drawable

private boolean applyGravity;
private Paint paint;
private ColorFilter colorFilter;
private ColorStateList tint;
private PorterDuff.Mode tintMode = DEFAULT_TINT_MODE;
private ColorFilter tintFilter;
private Rect destRect;

/** Callbacks to notify loop completion of a gif, where the loop count is explicitly specified. */
Expand Down Expand Up @@ -288,7 +298,16 @@ public void draw(@NonNull Canvas canvas) {
}

Bitmap currentFrame = state.frameLoader.getCurrentFrame();
canvas.drawBitmap(currentFrame, null, getDestRect(), getPaint());
Paint paint = getPaint();
if (colorFilter != null) {
// ColorFilter disables tint list. See Drawable#setColorFilter().
paint.setColorFilter(colorFilter);
} else if (tintFilter != null) {
paint.setColorFilter(tintFilter);
} else {
paint.setColorFilter(null);
}
canvas.drawBitmap(currentFrame, null, getDestRect(), paint);
}

@Override
Expand All @@ -298,7 +317,46 @@ public void setAlpha(int i) {

@Override
public void setColorFilter(ColorFilter colorFilter) {
getPaint().setColorFilter(colorFilter);
this.colorFilter = colorFilter;
invalidateSelf();
}

@Override
public ColorFilter getColorFilter() {
return getPaint().getColorFilter();
}

@Override
public void setTintList(ColorStateList tint) {
this.tint = tint;
updateTintFilter();
invalidateSelf();
}

@Override
public void setTintMode(PorterDuff.Mode tintMode) {
this.tintMode = tintMode;
updateTintFilter();
invalidateSelf();
}

@Override
protected boolean onStateChange(int[] stateSet) {
if (tint != null) {
updateTintFilter();
return true;
}
return false;
}

private void updateTintFilter() {
if (tint != null) {
int color = tint.getColorForState(getState(), Color.TRANSPARENT);
PorterDuff.Mode mode = tintMode != null ? tintMode : DEFAULT_TINT_MODE;
tintFilter = new PorterDuffColorFilter(color, mode);
} else {
tintFilter = null;
}
}

private Rect getDestRect() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import static org.mockito.Mockito.when;

import android.app.Application;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
Expand Down Expand Up @@ -566,14 +567,113 @@ public void testSetAlphaSetsAlphaOnPaint() {

@Test
public void testSetColorFilterSetsColorFilterOnPaint() {
// Use a real Paint object, as this test depends on Paint#(get|set)ColorFilter.
drawable = new GifDrawable(frameLoader, new Paint());
ColorFilter colorFilter = new PorterDuffColorFilter(Color.RED, Mode.ADD);
drawable.setColorFilter(colorFilter);
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);

ArgumentCaptor<Paint> captor = ArgumentCaptor.forClass(Paint.class);
verify(canvas).drawBitmap(isA(Bitmap.class), isNull(), isA(Rect.class), captor.capture());
assertThat(captor.getValue().getColorFilter()).isEqualTo(colorFilter);
}

@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
@Test
public void testSetTintListSetsColorFilterOnPaint() {
// Use a real Paint object, as this test depends on Paint#(get|set)ColorFilter.
Paint paint = new Paint();
drawable = new GifDrawable(frameLoader, paint);
ColorStateList tint =
new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}, new int[0]},
new int[] {Color.RED, Color.GREEN});
drawable.setTintList(tint);
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);

ArgumentCaptor<Paint> captor = ArgumentCaptor.forClass(Paint.class);
verify(canvas).drawBitmap(isA(Bitmap.class), isNull(), isA(Rect.class), captor.capture());
assertThat(captor.getValue().getColorFilter()).isEqualTo(new PorterDuffColorFilter(Color.GREEN, Mode.SRC_IN));
}

@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
@Test
public void testSetTintListSetsColorFilterForPressedStateOnPaint() {
// Use a real Paint object, as this test depends on Paint#(get|set)ColorFilter.
drawable = new GifDrawable(frameLoader, new Paint());
ColorStateList tint =
new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}, new int[0]},
new int[] {Color.RED, Color.GREEN});
drawable.setTintList(tint);
assertThat(drawable.setState(new int[] {android.R.attr.state_pressed})).isTrue();
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);

ArgumentCaptor<Paint> captor = ArgumentCaptor.forClass(Paint.class);
verify(canvas).drawBitmap(isA(Bitmap.class), isNull(), isA(Rect.class), captor.capture());
assertThat(captor.getValue().getColorFilter()).isEqualTo(new PorterDuffColorFilter(Color.RED, Mode.SRC_IN));
}

@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
@Test
public void testSetTintModeSetsColorFilterOnPaint() {
// Use a real Paint object, as this test depends on Paint#(get|set)ColorFilter.
drawable = new GifDrawable(frameLoader, new Paint());
ColorStateList tint =
new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}, new int[0]},
new int[] {Color.RED, Color.GREEN});
drawable.setTintList(tint);
drawable.setTintMode(Mode.ADD);
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);

ArgumentCaptor<Paint> captor = ArgumentCaptor.forClass(Paint.class);
verify(canvas).drawBitmap(isA(Bitmap.class), isNull(), isA(Rect.class), captor.capture());
assertThat(captor.getValue().getColorFilter()).isEqualTo(new PorterDuffColorFilter(Color.GREEN, Mode.ADD));
}

@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
@Test
public void testNullTintModeFallsBackToDefaultTintMode() {
// Use a real Paint object, as this test depends on Paint#(get|set)ColorFilter.
drawable = new GifDrawable(frameLoader, new Paint());
ColorStateList tint =
new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}, new int[0]},
new int[] {Color.RED, Color.GREEN});
drawable.setTintList(tint);
drawable.setTintMode(null);
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);

ArgumentCaptor<Paint> captor = ArgumentCaptor.forClass(Paint.class);
verify(canvas).drawBitmap(isA(Bitmap.class), isNull(), isA(Rect.class), captor.capture());
assertThat(captor.getValue().getColorFilter()).isEqualTo(new PorterDuffColorFilter(Color.GREEN, Mode.SRC_IN));
}

@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
@Test
public void testSetColorFilterIsPrioritizedThanSetTintList() {
// Use a real Paint object, as this test depends on Paint#(get|set)ColorFilter.
drawable = new GifDrawable(frameLoader, new Paint());
ColorFilter colorFilter = new PorterDuffColorFilter(Color.BLUE, Mode.ADD);
drawable.setColorFilter(colorFilter);
ColorStateList tint =
new ColorStateList(
new int[][] {new int[] {android.R.attr.state_pressed}, new int[0]},
new int[] {Color.RED, Color.GREEN});
drawable.setTintList(tint);
drawable.setTintMode(Mode.ADD);
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);

// Use ArgumentCaptor instead of eq() due to b/73121412 where ShadowPorterDuffColorFilter.equals
// uses a method that can't be found (PorterDuffColorFilter.getColor).
ArgumentCaptor<ColorFilter> captor = ArgumentCaptor.forClass(ColorFilter.class);
verify(paint).setColorFilter(captor.capture());
assertThat(captor.getValue()).isSameInstanceAs(colorFilter);
ArgumentCaptor<Paint> captor = ArgumentCaptor.forClass(Paint.class);
verify(canvas).drawBitmap(isA(Bitmap.class), isNull(), isA(Rect.class), captor.capture());
assertThat(captor.getValue().getColorFilter()).isEqualTo(colorFilter);
}

@Test
Expand Down