BitmapRequestBuilder.java revision ac28599e2b40e0dd6b97f6a91849585531264622
1package com.bumptech.glide; 2 3import android.content.Context; 4import android.graphics.Bitmap; 5import android.graphics.drawable.Drawable; 6import android.os.ParcelFileDescriptor; 7import android.view.animation.Animation; 8import com.bumptech.glide.load.DecodeFormat; 9import com.bumptech.glide.load.ResourceDecoder; 10import com.bumptech.glide.load.ResourceEncoder; 11import com.bumptech.glide.load.Transformation; 12import com.bumptech.glide.load.resource.bitmap.CenterCrop; 13import com.bumptech.glide.load.resource.bitmap.Downsampler; 14import com.bumptech.glide.load.resource.bitmap.FileDescriptorBitmapDecoder; 15import com.bumptech.glide.load.resource.bitmap.FitCenter; 16import com.bumptech.glide.load.resource.bitmap.ImageVideoBitmapDecoder; 17import com.bumptech.glide.load.resource.bitmap.StreamBitmapDecoder; 18import com.bumptech.glide.load.resource.bitmap.VideoBitmapDecoder; 19import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; 20import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 21import com.bumptech.glide.load.model.ImageVideoWrapper; 22import com.bumptech.glide.provider.LoadProvider; 23import com.bumptech.glide.request.bitmap.RequestListener; 24 25import java.io.InputStream; 26 27/** 28 * A class for creating a request to load a bitmap for an image or from a video. Sets a variety of type independent 29 * options including resizing, animations, and placeholders. 30 * 31 * @paramThe type of model that will be loaded into the target. 32 * @param The type of the transcoded resource that the target will receive 33 */ 34@SuppressWarnings("unused") //public api 35public class BitmapRequestBuilder<ModelType, TranscodeType> extends GenericRequestBuilder<ModelType, ImageVideoWrapper, 36 Bitmap, TranscodeType> { 37 private final BitmapPool bitmapPool; 38 private Downsampler downsampler = Downsampler.AT_LEAST; 39 private DecodeFormat decodeFormat = DecodeFormat.PREFER_RGB_565; 40 private ResourceDecoder<InputStream, Bitmap> imageDecoder; 41 private ResourceDecoder<ParcelFileDescriptor, Bitmap> videoDecoder; 42 43 BitmapRequestBuilder(Context context, ModelType model, 44 LoadProvider<ModelType, ImageVideoWrapper, Bitmap, TranscodeType> streamLoadProvider, 45 Class<TranscodeType> transcodeClass, Glide glide) { 46 super(context, model, streamLoadProvider, transcodeClass, glide); 47 this.bitmapPool = glide.getBitmapPool(); 48 49 imageDecoder = new StreamBitmapDecoder(bitmapPool); 50 videoDecoder = new FileDescriptorBitmapDecoder(bitmapPool); 51 } 52 53 /** 54 * Load images at a size near the size of the target using {@link Downsampler#AT_LEAST}. 55 * 56 * @see #downsample(Downsampler) 57 * 58 * @return This RequestBuilder 59 */ 60 public BitmapRequestBuilder<ModelType, TranscodeType> approximate() { 61 return downsample(Downsampler.AT_LEAST); 62 } 63 64 /** 65 * Load images at their original size using {@link Downsampler#NONE}. 66 * 67 * @see #downsample(Downsampler) 68 * 69 * @return This RequestBuilder 70 */ 71 public BitmapRequestBuilder<ModelType, TranscodeType> asIs() { 72 return downsample(Downsampler.NONE); 73 } 74 75 /** 76 * Load images using the given {@link Downsampler}. Replaces any existing image decoder. Defaults to 77 * {@link Downsampler#AT_LEAST}. Will be ignored if the data represented by the model is a video. This replaces any 78 * previous calls to {@link #imageDecoder(ResourceDecoder)} and {@link #decoder(ResourceDecoder)} with default 79 * decoders with the appropriate options set. 80 * 81 * @see #imageDecoder 82 * 83 * @param downsampler The downsampler 84 * @return This RequestBuilder 85 */ 86 private BitmapRequestBuilder<ModelType, TranscodeType> downsample(Downsampler downsampler) { 87 this.downsampler = downsampler; 88 imageDecoder = new StreamBitmapDecoder(downsampler, bitmapPool, decodeFormat); 89 super.decoder(new ImageVideoBitmapDecoder(imageDecoder, videoDecoder)); 90 return this; 91 } 92 93 @Override 94 public BitmapRequestBuilder<ModelType, TranscodeType> thumbnail(float sizeMultiplier) { 95 super.thumbnail(sizeMultiplier); 96 return this; 97 } 98 99 public BitmapRequestBuilder<ModelType, TranscodeType> thumbnail(BitmapRequestBuilder<ModelType, TranscodeType> 100 thumbnailRequest) { 101 super.thumbnail(thumbnailRequest); 102 return this; 103 } 104 105 @Override 106 public BitmapRequestBuilder<ModelType, TranscodeType> sizeMultiplier(float sizeMultiplier) { 107 super.sizeMultiplier(sizeMultiplier); 108 return this; 109 } 110 111 @Override 112 public BitmapRequestBuilder<ModelType, TranscodeType> decoder(ResourceDecoder<ImageVideoWrapper, Bitmap> decoder) { 113 super.decoder(decoder); 114 return this; 115 } 116 117 @Override 118 public BitmapRequestBuilder<ModelType, TranscodeType> cacheDecoder( 119 ResourceDecoder<InputStream, Bitmap> cacheDecoder) { 120 super.cacheDecoder(cacheDecoder); 121 return this; 122 } 123 124 @Override 125 public BitmapRequestBuilder<ModelType, TranscodeType> encoder(ResourceEncoder<Bitmap> encoder) { 126 super.encoder(encoder); 127 return this; 128 } 129 130 public BitmapRequestBuilder<ModelType, TranscodeType> imageDecoder(ResourceDecoder<InputStream, Bitmap> decoder) { 131 imageDecoder = decoder; 132 super.decoder(new ImageVideoBitmapDecoder(decoder, videoDecoder)); 133 return this; 134 } 135 136 public BitmapRequestBuilder<ModelType, TranscodeType> videoDecoder( 137 ResourceDecoder<ParcelFileDescriptor, Bitmap> decoder) { 138 videoDecoder = decoder; 139 super.decoder(new ImageVideoBitmapDecoder(imageDecoder, decoder)); 140 return this; 141 } 142 143 /** 144 * Sets the preferred format for {@link Bitmap}s decoded in this request. Defaults to 145 * {@link DecodeFormat#PREFER_RGB_565}. This replaces any previous calls to {@link #imageDecoder(ResourceDecoder)}, 146 * {@link #videoDecoder(ResourceDecoder)} and {@link #decoder(ResourceDecoder)} with default decoders with the 147 * appropriate options set. 148 * 149 * <p> 150 * Note - If using a {@link Transformation} that expect bitmaps to support transparency, this should always be 151 * set to ALWAYS_ARGB_8888. RGB_565 requires fewer bytes per pixel and is generally preferable, but it does not 152 * support transparency. 153 * </p> 154 * 155 * @see DecodeFormat 156 * 157 * @param format The format to use. 158 * @return This request builder. 159 */ 160 public BitmapRequestBuilder<ModelType, TranscodeType> format(DecodeFormat format) { 161 this.decodeFormat = format; 162 imageDecoder = new StreamBitmapDecoder(downsampler, bitmapPool, format); 163 videoDecoder = new FileDescriptorBitmapDecoder(new VideoBitmapDecoder(), bitmapPool, format); 164 super.decoder(new ImageVideoBitmapDecoder(imageDecoder, videoDecoder)); 165 return this; 166 } 167 168 @Override 169 public BitmapRequestBuilder<ModelType, TranscodeType> priority(Priority priority) { 170 super.priority(priority); 171 return this; 172 } 173 174 /** 175 * Transform images using {@link CenterCrop}. 176 * 177 * @return This RequestBuilder 178 */ 179 public BitmapRequestBuilder<ModelType, TranscodeType> centerCrop() { 180 return transform(new CenterCrop(bitmapPool)); 181 } 182 183 /** 184 * Transform images using {@link FitCenter}. 185 * 186 * @return This RequestBuilder 187 */ 188 public BitmapRequestBuilder<ModelType, TranscodeType> fitCenter() { 189 return transform(new FitCenter(bitmapPool)); 190 } 191 192 @Override 193 public BitmapRequestBuilder<ModelType, TranscodeType> transform(Transformation<Bitmap> transformation) { 194 super.transform(transformation); 195 return this; 196 } 197 198 @Override 199 public BitmapRequestBuilder<ModelType, TranscodeType> transcoder( 200 ResourceTranscoder<Bitmap, TranscodeType> transcoder) { 201 super.transcoder(transcoder); 202 return this; 203 } 204 205 @Override 206 public BitmapRequestBuilder<ModelType, TranscodeType> animate(int animationId) { 207 super.animate(animationId); 208 return this; 209 } 210 211 @Override 212 public BitmapRequestBuilder<ModelType, TranscodeType> animate(Animation animation) { 213 super.animate(animation); 214 return this; 215 } 216 217 @Override 218 public BitmapRequestBuilder<ModelType, TranscodeType> placeholder(int resourceId) { 219 super.placeholder(resourceId); 220 return this; 221 } 222 223 @Override 224 public BitmapRequestBuilder<ModelType, TranscodeType> placeholder(Drawable drawable) { 225 super.placeholder(drawable); 226 return this; 227 } 228 229 @Override 230 public BitmapRequestBuilder<ModelType, TranscodeType> error(int resourceId) { 231 super.error(resourceId); 232 return this; 233 } 234 235 @Override 236 public BitmapRequestBuilder<ModelType, TranscodeType> error(Drawable drawable) { 237 super.error(drawable); 238 return this; 239 } 240 241 @Override 242 public BitmapRequestBuilder<ModelType, TranscodeType> listener(RequestListener<ModelType> requestListener) { 243 super.listener(requestListener); 244 return this; 245 } 246} 247