提交 38ced22f 编写于 作者: S Sam Judd

Apply gravity in GifDrawable.

Fixes #206
上级 3ea7f151
...@@ -4,6 +4,7 @@ import android.graphics.Bitmap; ...@@ -4,6 +4,7 @@ import android.graphics.Bitmap;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint; 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 com.bumptech.glide.gifdecoder.GifDecoder; import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.gifdecoder.GifHeader; import com.bumptech.glide.gifdecoder.GifHeader;
...@@ -23,7 +24,6 @@ import static org.junit.Assert.assertFalse; ...@@ -23,7 +24,6 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.eq; import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull; import static org.mockito.Matchers.isNull;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
...@@ -60,7 +60,7 @@ public class GifDrawableTest { ...@@ -60,7 +60,7 @@ public class GifDrawableTest {
Canvas canvas = mock(Canvas.class); Canvas canvas = mock(Canvas.class);
drawable.draw(canvas); drawable.draw(canvas);
verify(canvas).drawBitmap(eq(firstFrame), anyInt(), anyInt(), any(Paint.class)); verify(canvas).drawBitmap(eq(firstFrame), anyRect(), anyRect(), anyPaint());
} }
@Test @Test
...@@ -71,8 +71,8 @@ public class GifDrawableTest { ...@@ -71,8 +71,8 @@ public class GifDrawableTest {
when(frameManager.getCurrentFrame()).thenReturn(null); when(frameManager.getCurrentFrame()).thenReturn(null);
drawable.draw(canvas); drawable.draw(canvas);
verify(canvas).drawBitmap(eq(firstFrame), anyInt(), anyInt(), any(Paint.class)); verify(canvas).drawBitmap(eq(firstFrame), anyRect(), anyRect(), anyPaint());
verify(canvas, never()).drawBitmap((Bitmap) isNull(), anyInt(), anyInt(), any(Paint.class)); verify(canvas, never()).drawBitmap((Bitmap) isNull(), anyRect(), anyRect(), anyPaint());
} }
@Test @Test
...@@ -80,7 +80,7 @@ public class GifDrawableTest { ...@@ -80,7 +80,7 @@ public class GifDrawableTest {
drawable = new GifDrawable(gifDecoder, frameManager, null, bitmapPool); drawable = new GifDrawable(gifDecoder, frameManager, null, bitmapPool);
Canvas canvas = mock(Canvas.class); Canvas canvas = mock(Canvas.class);
verify(canvas, never()).drawBitmap(any(Bitmap.class), anyInt(), anyInt(), any(Paint.class)); verify(canvas, never()).drawBitmap(any(Bitmap.class), anyRect(), anyRect(), anyPaint());
} }
@Test @Test
...@@ -90,8 +90,8 @@ public class GifDrawableTest { ...@@ -90,8 +90,8 @@ public class GifDrawableTest {
when(frameManager.getCurrentFrame()).thenReturn(currentFrame); when(frameManager.getCurrentFrame()).thenReturn(currentFrame);
drawable.draw(canvas); drawable.draw(canvas);
verify(canvas).drawBitmap(eq(currentFrame), anyInt(), anyInt(), any(Paint.class)); verify(canvas).drawBitmap(eq(currentFrame), anyRect(), anyRect(), anyPaint());
verify(canvas, never()).drawBitmap(eq(firstFrame), anyInt(), anyInt(), any(Paint.class)); verify(canvas, never()).drawBitmap(eq(firstFrame), anyRect(), anyRect(), anyPaint());
} }
@Test @Test
...@@ -459,7 +459,15 @@ public class GifDrawableTest { ...@@ -459,7 +459,15 @@ public class GifDrawableTest {
drawable.recycle(); drawable.recycle();
Canvas canvas = mock(Canvas.class); Canvas canvas = mock(Canvas.class);
drawable.draw(canvas); drawable.draw(canvas);
verify(canvas, never()).drawBitmap(any(Bitmap.class), anyInt(), anyInt(), any(Paint.class)); verify(canvas, never()).drawBitmap(any(Bitmap.class), anyRect(), anyRect(), anyPaint());
}
private static Paint anyPaint() {
return any(Paint.class);
}
private static Rect anyRect() {
return any(Rect.class);
} }
private void runLoops(int loopCount, int frameCount) { private void runLoops(int loopCount, int frameCount) {
......
...@@ -8,9 +8,11 @@ import android.graphics.Canvas; ...@@ -8,9 +8,11 @@ import android.graphics.Canvas;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import android.graphics.Paint; 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 android.os.Build; import android.os.Build;
import android.view.Gravity;
import com.bumptech.glide.gifdecoder.GifDecoder; import com.bumptech.glide.gifdecoder.GifDecoder;
import com.bumptech.glide.gifdecoder.GifHeader; import com.bumptech.glide.gifdecoder.GifHeader;
import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.Transformation;
...@@ -22,6 +24,7 @@ import com.bumptech.glide.load.resource.drawable.GlideDrawable; ...@@ -22,6 +24,7 @@ import com.bumptech.glide.load.resource.drawable.GlideDrawable;
*/ */
public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameCallback { public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameCallback {
private final Paint paint = new Paint(); private final Paint paint = new Paint();
private final Rect destRect = new Rect();
private final GifFrameManager frameManager; private final GifFrameManager frameManager;
private final GifState state; private final GifState state;
private final GifDecoder decoder; private final GifDecoder decoder;
...@@ -43,6 +46,8 @@ public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameC ...@@ -43,6 +46,8 @@ public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameC
/** The number of times to loop through the gif animation. */ /** The number of times to loop through the gif animation. */
private int maxLoopCount = LOOP_FOREVER; private int maxLoopCount = LOOP_FOREVER;
private boolean applyGravity;
/** /**
* Constructor for GifDrawable. * Constructor for GifDrawable.
* *
...@@ -177,14 +182,26 @@ public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameC ...@@ -177,14 +182,26 @@ public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameC
this.isRunning = isRunning; this.isRunning = isRunning;
} }
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
applyGravity = true;
}
@Override @Override
public void draw(Canvas canvas) { public void draw(Canvas canvas) {
if (isRecycled) { if (isRecycled) {
return; return;
} }
if (applyGravity) {
Gravity.apply(GifState.GRAVITY, getIntrinsicWidth(), getIntrinsicHeight(), getBounds(), destRect);
applyGravity = false;
}
Bitmap currentFrame = frameManager.getCurrentFrame(); Bitmap currentFrame = frameManager.getCurrentFrame();
Bitmap toDraw = currentFrame != null ? currentFrame : state.firstFrame; Bitmap toDraw = currentFrame != null ? currentFrame : state.firstFrame;
canvas.drawBitmap(toDraw, 0, 0, paint); canvas.drawBitmap(toDraw, null, destRect, paint);
} }
@Override @Override
...@@ -266,6 +283,7 @@ public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameC ...@@ -266,6 +283,7 @@ public class GifDrawable extends GlideDrawable implements GifFrameManager.FrameC
} }
static class GifState extends ConstantState { static class GifState extends ConstantState {
private static final int GRAVITY = Gravity.FILL;
String id; String id;
GifHeader gifHeader; GifHeader gifHeader;
byte[] data; byte[] data;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册