aboutsummaryrefslogtreecommitdiff
path: root/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java
diff options
context:
space:
mode:
Diffstat (limited to 'library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java')
-rw-r--r--library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java94
1 files changed, 85 insertions, 9 deletions
diff --git a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java
index 5c0784c0..55edd294 100644
--- a/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java
+++ b/library/src/main/java/com/bumptech/glide/load/resource/gif/GifDrawable.java
@@ -1,6 +1,8 @@
package com.bumptech.glide.load.resource.gif;
import android.annotation.TargetApi;
+import android.content.Context;
+import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
@@ -11,30 +13,47 @@ import android.graphics.drawable.Drawable;
import android.os.Build;
import com.bumptech.glide.gifdecoder.GifDecoder;
+import com.bumptech.glide.gifdecoder.GifHeader;
+import com.bumptech.glide.load.Transformation;
/**
* An animated {@link android.graphics.drawable.Drawable} that plays the frames of an animated GIF.
*/
public class GifDrawable extends Drawable implements Animatable, GifFrameManager.FrameCallback {
- private final Paint paint;
- private final GifFrameManager frameManager;
- private int width;
- private int height;
+ private final Paint paint = new Paint();
+ private GifFrameManager frameManager;
+ private GifState state;
+ private int width = -1;
+ private int height = -1;
private GifDecoder decoder;
private boolean isRunning;
private Bitmap currentFrame;
private boolean isRecycled;
- public GifDrawable(GifDecoder decoder, GifFrameManager frameManager) {
+ public GifDrawable(String id, GifHeader gifHeader, byte[] data, Context context,
+ Transformation<Bitmap> frameTransformation, int targetWidth, int targetHeight,
+ GifDecoder.BitmapProvider bitmapProvider) {
+ this(new GifState(id, gifHeader, data, context, frameTransformation, targetWidth, targetHeight,
+ bitmapProvider));
+ }
+
+ private GifDrawable(GifState state) {
+ this.state = state;
+ this.decoder = new GifDecoder(state.bitmapProvider);
+ decoder.setData(state.id, state.gifHeader, state.data);
+ frameManager = new GifFrameManager(state.context, decoder, state.frameTransformation, state.targetWidth,
+ state.targetHeight);
+ }
+
+ // For testing.
+ GifDrawable(GifDecoder decoder, GifFrameManager frameManager) {
this.decoder = decoder;
this.frameManager = frameManager;
- width = -1;
- height = -1;
-
- paint = new Paint();
+ this.state = new GifState(null);
}
+
@Override
public void start() {
if (!isRunning) {
@@ -126,6 +145,11 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
frameManager.getNextFrame(this);
}
+ @Override
+ public ConstantState getConstantState() {
+ return state;
+ }
+
/**
* Clears any resources for loading frames that are currently held on to by this object.
*/
@@ -137,4 +161,56 @@ public class GifDrawable extends Drawable implements Animatable, GifFrameManager
boolean isRecycled() {
return isRecycled;
}
+
+ static class GifState extends ConstantState {
+ String id;
+ GifHeader gifHeader;
+ byte[] data;
+ Context context;
+ Transformation<Bitmap> frameTransformation;
+ int targetWidth;
+ int targetHeight;
+ GifDecoder.BitmapProvider bitmapProvider;
+
+ public GifState(String id, GifHeader header, byte[] data, Context context,
+ Transformation<Bitmap> frameTransformation, int targetWidth, int targetHeight,
+ GifDecoder.BitmapProvider provider) {
+ this.id = id;
+ gifHeader = header;
+ this.data = data;
+ this.context = context.getApplicationContext();
+ this.frameTransformation = frameTransformation;
+ this.targetWidth = targetWidth;
+ this.targetHeight = targetHeight;
+ bitmapProvider = provider;
+ }
+
+ public GifState(GifState original) {
+ if (original != null) {
+ id = original.id;
+ gifHeader = original.gifHeader;
+ data = original.data;
+ context = original.context;
+ frameTransformation = original.frameTransformation;
+ targetWidth = original.targetWidth;
+ targetHeight = original.targetHeight;
+ bitmapProvider = original.bitmapProvider;
+ }
+ }
+
+ @Override
+ public Drawable newDrawable(Resources res) {
+ return super.newDrawable(res);
+ }
+
+ @Override
+ public Drawable newDrawable() {
+ return new GifDrawable(this);
+ }
+
+ @Override
+ public int getChangingConfigurations() {
+ return 0;
+ }
+ }
}