提交 787279ab 编写于 作者: S Sam Judd

Retain bounds when mutating GlideBitmapDrawable.

Follows the same pattern as BitmapDrawable in the
Android framework.

Fixes #118
上级 ab87f38c
package com.bumptech.glide.load.resource.bitmap; package com.bumptech.glide.load.resource.bitmap;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat; import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
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.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.robolectric.Robolectric; import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class GlideBitmapDrawableTest { public class GlideBitmapDrawableTest {
...@@ -80,11 +93,6 @@ public class GlideBitmapDrawableTest { ...@@ -80,11 +93,6 @@ public class GlideBitmapDrawableTest {
assertEquals(drawable.getBitmap(), newDrawable.getBitmap()); assertEquals(drawable.getBitmap(), newDrawable.getBitmap());
} }
@Test
public void testMutateReturnsNewDrawable() {
assertNotSame(drawable, drawable.mutate());
}
@Test @Test
public void testMutatedDrawableIsGlideBitmapDrawable() { public void testMutatedDrawableIsGlideBitmapDrawable() {
Drawable newDrawable = drawable.mutate(); Drawable newDrawable = drawable.mutate();
...@@ -96,4 +104,61 @@ public class GlideBitmapDrawableTest { ...@@ -96,4 +104,61 @@ public class GlideBitmapDrawableTest {
GlideBitmapDrawable mutated = (GlideBitmapDrawable) drawable.mutate(); GlideBitmapDrawable mutated = (GlideBitmapDrawable) drawable.mutate();
assertEquals(drawable.getBitmap(), mutated.getBitmap()); assertEquals(drawable.getBitmap(), mutated.getBitmap());
} }
@Test
public void testRetainsSameBoundsAcrossMutation() {
Rect bounds = new Rect(0, 0, 100, 100);
drawable.setBounds(bounds);
drawable.onBoundsChange(bounds);
Canvas canvas = mock(Canvas.class);
drawable.draw(canvas);
Drawable mutated = drawable.mutate();
mutated.draw(canvas);
assertThat(mutated.getBounds(), equalTo(bounds));
verify(canvas, times(2)).drawBitmap(eq(bitmap), isNull(Rect.class), eq(bounds), any(Paint.class));
}
@Test
public void testMutatedDrawableUsesNewPaint() {
drawable.setAlpha(1);
Drawable newDrawable = drawable.getConstantState().newDrawable();
Drawable mutated = drawable.mutate();
mutated.setAlpha(100);
Canvas canvas = mock(Canvas.class);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Paint paint = (Paint) invocation.getArguments()[3];
assertThat(paint.getAlpha(), equalTo(1));
return null;
}
}).when(canvas).drawBitmap(any(Bitmap.class), any(Rect.class), any(Rect.class), any(Paint.class));
newDrawable.draw(canvas);
verify(canvas).drawBitmap(eq(bitmap), any(Rect.class), any(Rect.class), any(Paint.class));
}
@Test
public void testMutatedDrawableUsesNewColorFilter() {
final ColorFilter originalColorFilter = new LightingColorFilter(1, 1);
drawable.setColorFilter(originalColorFilter);
Drawable newDrawable = drawable.getConstantState().newDrawable();
Drawable mutated = drawable.mutate();
ColorFilter newColorFilter = new LightingColorFilter(2, 2);
mutated.setColorFilter(newColorFilter);
Canvas canvas = mock(Canvas.class);
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Paint paint = (Paint) invocation.getArguments()[3];
assertThat(paint.getColorFilter(), equalTo(originalColorFilter));
return null;
}
}).when(canvas).drawBitmap(any(Bitmap.class), any(Rect.class), any(Rect.class), any(Paint.class));
newDrawable.draw(canvas);
verify(canvas).drawBitmap(eq(bitmap), any(Rect.class), any(Rect.class), any(Paint.class));
}
} }
\ No newline at end of file
...@@ -21,6 +21,7 @@ public class GlideBitmapDrawable extends GlideDrawable { ...@@ -21,6 +21,7 @@ public class GlideBitmapDrawable extends GlideDrawable {
private int width; private int width;
private int height; private int height;
private boolean applyGravity; private boolean applyGravity;
private boolean mutated;
private BitmapState state; private BitmapState state;
public GlideBitmapDrawable(Resources res, Bitmap bitmap) { public GlideBitmapDrawable(Resources res, Bitmap bitmap) {
...@@ -120,7 +121,11 @@ public class GlideBitmapDrawable extends GlideDrawable { ...@@ -120,7 +121,11 @@ public class GlideBitmapDrawable extends GlideDrawable {
@Override @Override
public Drawable mutate() { public Drawable mutate() {
return new GlideBitmapDrawable(null, new BitmapState(state)); if (!mutated && super.mutate() == this) {
state = new BitmapState(state);
mutated = true;
}
return this;
} }
public Bitmap getBitmap() { public Bitmap getBitmap() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册