1 package com.bumptech.glide.load.resource.transcode; 2 3 import android.graphics.Bitmap; 4 5 import com.bumptech.glide.load.engine.Resource; 6 import com.bumptech.glide.load.resource.bitmap.GlideBitmapDrawable; 7 import com.bumptech.glide.load.resource.drawable.GlideDrawable; 8 import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; 9 10 /** 11 * An {@link com.bumptech.glide.load.resource.transcode.ResourceTranscoder} that can transcode either an 12 * {@link Bitmap} or an {@link com.bumptech.glide.load.resource.gif.GifDrawable} into an 13 * {@link android.graphics.drawable.Drawable}. 14 */ 15 public class GifBitmapWrapperDrawableTranscoder implements ResourceTranscoder<GifBitmapWrapper, GlideDrawable> { 16 private final ResourceTranscoder<Bitmap, GlideBitmapDrawable> bitmapDrawableResourceTranscoder; 17 18 public GifBitmapWrapperDrawableTranscoder( 19 ResourceTranscoder<Bitmap, GlideBitmapDrawable> bitmapDrawableResourceTranscoder) { 20 this.bitmapDrawableResourceTranscoder = bitmapDrawableResourceTranscoder; 21 } 22 23 @SuppressWarnings("unchecked") 24 @Override 25 public Resource<GlideDrawable> transcode(Resource<GifBitmapWrapper> toTranscode) { 26 GifBitmapWrapper gifBitmap = toTranscode.get(); 27 Resource<Bitmap> bitmapResource = gifBitmap.getBitmapResource(); 28 29 final Resource<? extends GlideDrawable> result; 30 if (bitmapResource != null) { 31 result = bitmapDrawableResourceTranscoder.transcode(bitmapResource); 32 } else { 33 result = gifBitmap.getGifResource(); 34 } 35 // This is unchecked but always safe, anything that extends a Drawable can be safely cast to a Drawable. 36 return (Resource<GlideDrawable>) result; 37 } 38 39 @Override 40 public String getId() { 41 return "GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode"; 42 } 43 } 44