GifDrawableTransformation.java revision 4185efc3779f8caf2dd8103aa84705e7a88f679c
121c2822985058df655ee4874798297f74c5e367eSam Juddpackage com.bumptech.glide.load.resource.gif;
221c2822985058df655ee4874798297f74c5e367eSam Judd
321c2822985058df655ee4874798297f74c5e367eSam Juddimport android.graphics.Bitmap;
421c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.Transformation;
521c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.engine.Resource;
621c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
721c2822985058df655ee4874798297f74c5e367eSam Juddimport com.bumptech.glide.load.resource.bitmap.BitmapResource;
821c2822985058df655ee4874798297f74c5e367eSam Judd
921c2822985058df655ee4874798297f74c5e367eSam Judd/**
1021c2822985058df655ee4874798297f74c5e367eSam Judd * An {@link com.bumptech.glide.load.Transformation} that wraps a transformation for a {@link Bitmap}
1121c2822985058df655ee4874798297f74c5e367eSam Judd * and can apply it to every frame of any {@link com.bumptech.glide.load.resource.gif.GifDrawable}.
1221c2822985058df655ee4874798297f74c5e367eSam Judd */
1321c2822985058df655ee4874798297f74c5e367eSam Juddpublic class GifDrawableTransformation implements Transformation<GifDrawable> {
1421c2822985058df655ee4874798297f74c5e367eSam Judd    private Transformation<Bitmap> wrapped;
1521c2822985058df655ee4874798297f74c5e367eSam Judd    private BitmapPool bitmapPool;
1621c2822985058df655ee4874798297f74c5e367eSam Judd
1721c2822985058df655ee4874798297f74c5e367eSam Judd    public GifDrawableTransformation(Transformation<Bitmap> wrapped, BitmapPool bitmapPool) {
1821c2822985058df655ee4874798297f74c5e367eSam Judd        this.wrapped = wrapped;
1921c2822985058df655ee4874798297f74c5e367eSam Judd        this.bitmapPool = bitmapPool;
2021c2822985058df655ee4874798297f74c5e367eSam Judd    }
2121c2822985058df655ee4874798297f74c5e367eSam Judd
2221c2822985058df655ee4874798297f74c5e367eSam Judd    @Override
2321c2822985058df655ee4874798297f74c5e367eSam Judd    public Resource<GifDrawable> transform(Resource<GifDrawable> resource, int outWidth, int outHeight) {
2421c2822985058df655ee4874798297f74c5e367eSam Judd        GifDrawable drawable = resource.get();
2521c2822985058df655ee4874798297f74c5e367eSam Judd
2621c2822985058df655ee4874798297f74c5e367eSam Judd        // The drawable needs to be initialized with the correct width and height in order for a view displaying it
2721c2822985058df655ee4874798297f74c5e367eSam Judd        // to end up with the right dimensions. Since our transformations may arbitrarily modify the dimensions of
2821c2822985058df655ee4874798297f74c5e367eSam Judd        // our gif, here we create a stand in for a frame and pass it to the transformation to see what the final
294185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        // transformed dimensions will be so that our drawable can report the correct intrinsic width and height.
304185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Bitmap firstFrame = resource.get().getFirstFrame();
314185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Resource<Bitmap> bitmapResource = new BitmapResource(firstFrame, bitmapPool);
324185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Resource<Bitmap> transformed = wrapped.transform(bitmapResource, outWidth, outHeight);
3321c2822985058df655ee4874798297f74c5e367eSam Judd        if (bitmapResource != transformed) {
3421c2822985058df655ee4874798297f74c5e367eSam Judd            bitmapResource.recycle();
3521c2822985058df655ee4874798297f74c5e367eSam Judd        }
364185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        Bitmap transformedFrame = transformed.get();
3721c2822985058df655ee4874798297f74c5e367eSam Judd
384185efc3779f8caf2dd8103aa84705e7a88f679cSam Judd        drawable.setFrameTransformation(wrapped, transformedFrame);
3921c2822985058df655ee4874798297f74c5e367eSam Judd        return resource;
4021c2822985058df655ee4874798297f74c5e367eSam Judd    }
4121c2822985058df655ee4874798297f74c5e367eSam Judd
4221c2822985058df655ee4874798297f74c5e367eSam Judd    @Override
4321c2822985058df655ee4874798297f74c5e367eSam Judd    public String getId() {
4421c2822985058df655ee4874798297f74c5e367eSam Judd        return wrapped.getId();
4521c2822985058df655ee4874798297f74c5e367eSam Judd    }
4621c2822985058df655ee4874798297f74c5e367eSam Judd}
47