1package com.bumptech.glide.load.resource.gif; 2 3import android.graphics.Bitmap; 4 5import com.bumptech.glide.load.Transformation; 6import com.bumptech.glide.load.engine.Resource; 7import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 8import com.bumptech.glide.load.resource.bitmap.BitmapResource; 9 10/** 11 * An {@link com.bumptech.glide.load.Transformation} that wraps a transformation for a {@link Bitmap} 12 * and can apply it to every frame of any {@link com.bumptech.glide.load.resource.gif.GifDrawable}. 13 */ 14public class GifDrawableTransformation implements Transformation<GifDrawable> { 15 private final Transformation<Bitmap> wrapped; 16 private final BitmapPool bitmapPool; 17 18 public GifDrawableTransformation(Transformation<Bitmap> wrapped, BitmapPool bitmapPool) { 19 this.wrapped = wrapped; 20 this.bitmapPool = bitmapPool; 21 } 22 23 @Override 24 public Resource<GifDrawable> transform(Resource<GifDrawable> resource, int outWidth, int outHeight) { 25 GifDrawable drawable = resource.get(); 26 27 // The drawable needs to be initialized with the correct width and height in order for a view displaying it 28 // to end up with the right dimensions. Since our transformations may arbitrarily modify the dimensions of 29 // our gif, here we create a stand in for a frame and pass it to the transformation to see what the final 30 // transformed dimensions will be so that our drawable can report the correct intrinsic width and height. 31 Bitmap firstFrame = resource.get().getFirstFrame(); 32 Resource<Bitmap> bitmapResource = new BitmapResource(firstFrame, bitmapPool); 33 Resource<Bitmap> transformed = wrapped.transform(bitmapResource, outWidth, outHeight); 34 if (!bitmapResource.equals(transformed)) { 35 bitmapResource.recycle(); 36 } 37 Bitmap transformedFrame = transformed.get(); 38 39 drawable.setFrameTransformation(wrapped, transformedFrame); 40 return resource; 41 } 42 43 @Override 44 public String getId() { 45 return wrapped.getId(); 46 } 47} 48