package com.bumptech.glide.load.resource.gif; import android.graphics.Bitmap; import com.bumptech.glide.load.Transformation; import com.bumptech.glide.load.engine.Resource; import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; import com.bumptech.glide.load.resource.bitmap.BitmapResource; /** * An {@link com.bumptech.glide.load.Transformation} that wraps a transformation for a {@link Bitmap} * and can apply it to every frame of any {@link com.bumptech.glide.load.resource.gif.GifDrawable}. */ public class GifDrawableTransformation implements Transformation { private final Transformation wrapped; private final BitmapPool bitmapPool; public GifDrawableTransformation(Transformation wrapped, BitmapPool bitmapPool) { this.wrapped = wrapped; this.bitmapPool = bitmapPool; } @Override public Resource transform(Resource resource, int outWidth, int outHeight) { GifDrawable drawable = resource.get(); // The drawable needs to be initialized with the correct width and height in order for a view displaying it // to end up with the right dimensions. Since our transformations may arbitrarily modify the dimensions of // our gif, here we create a stand in for a frame and pass it to the transformation to see what the final // transformed dimensions will be so that our drawable can report the correct intrinsic width and height. Bitmap firstFrame = resource.get().getFirstFrame(); Resource bitmapResource = new BitmapResource(firstFrame, bitmapPool); Resource transformed = wrapped.transform(bitmapResource, outWidth, outHeight); if (!bitmapResource.equals(transformed)) { bitmapResource.recycle(); } Bitmap transformedFrame = transformed.get(); drawable.setFrameTransformation(wrapped, transformedFrame); return resource; } @Override public String getId() { return wrapped.getId(); } }