1 package com.bumptech.glide.load.resource.bitmap; 2 3 import android.graphics.Bitmap; 4 5 import com.bumptech.glide.load.engine.Resource; 6 import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; 7 import com.bumptech.glide.util.Util; 8 9 /** 10 * A resource wrapping a {@link android.graphics.Bitmap} object. 11 */ 12 public class BitmapResource implements Resource<Bitmap> { 13 private final Bitmap bitmap; 14 private final BitmapPool bitmapPool; 15 16 /** 17 * Returns a new {@link BitmapResource} wrapping the given {@link Bitmap} if the Bitmap is non-null or null if the 18 * given Bitmap is null. 19 * 20 * @param bitmap A Bitmap. 21 * @param bitmapPool A non-null {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool}. 22 */ 23 public static BitmapResource obtain(Bitmap bitmap, BitmapPool bitmapPool) { 24 if (bitmap == null) { 25 return null; 26 } else { 27 return new BitmapResource(bitmap, bitmapPool); 28 } 29 } 30 31 public BitmapResource(Bitmap bitmap, BitmapPool bitmapPool) { 32 if (bitmap == null) { 33 throw new NullPointerException("Bitmap must not be null"); 34 } 35 if (bitmapPool == null) { 36 throw new NullPointerException("BitmapPool must not be null"); 37 } 38 this.bitmap = bitmap; 39 this.bitmapPool = bitmapPool; 40 } 41 42 @Override 43 public Bitmap get() { 44 return bitmap; 45 } 46 47 @Override 48 public int getSize() { 49 return Util.getBitmapByteSize(bitmap); 50 } 51 52 @Override 53 public void recycle() { 54 if (!bitmapPool.put(bitmap)) { 55 bitmap.recycle(); 56 } 57 } 58 } 59