GlideBitmapDrawable.java revision 787279ab3e72c83626e65382a1dbd97f45e6cbd3
1package com.bumptech.glide.load.resource.bitmap; 2 3import android.content.res.Resources; 4import android.graphics.Bitmap; 5import android.graphics.Canvas; 6import android.graphics.ColorFilter; 7import android.graphics.Paint; 8import android.graphics.PixelFormat; 9import android.graphics.Rect; 10import android.graphics.drawable.Drawable; 11import android.util.DisplayMetrics; 12import android.view.Gravity; 13 14import com.bumptech.glide.load.resource.drawable.GlideDrawable; 15 16/** 17 * A static {@link com.bumptech.glide.load.resource.drawable.GlideDrawable} for displaying a single image. 18 */ 19public class GlideBitmapDrawable extends GlideDrawable { 20 private final Rect destRect = new Rect(); 21 private int width; 22 private int height; 23 private boolean applyGravity; 24 private boolean mutated; 25 private BitmapState state; 26 27 public GlideBitmapDrawable(Resources res, Bitmap bitmap) { 28 this(res, new BitmapState(bitmap)); 29 } 30 31 GlideBitmapDrawable(Resources res, BitmapState state) { 32 this.state = state; 33 final int targetDensity; 34 if (res != null) { 35 final int density = res.getDisplayMetrics().densityDpi; 36 targetDensity = density == 0 ? DisplayMetrics.DENSITY_DEFAULT : density; 37 state.targetDensity = targetDensity; 38 } else { 39 targetDensity = state.targetDensity; 40 } 41 width = state.bitmap.getScaledWidth(targetDensity); 42 height = state.bitmap.getScaledHeight(targetDensity); 43 } 44 45 @Override 46 public int getIntrinsicWidth() { 47 return width; 48 } 49 50 @Override 51 public int getIntrinsicHeight() { 52 return height; 53 } 54 55 @Override 56 public boolean isAnimated() { 57 return false; 58 } 59 60 @Override 61 public void setLoopCount(int loopCount) { 62 // Do nothing. 63 } 64 65 @Override 66 public void start() { 67 // Do nothing. 68 } 69 70 @Override 71 public void stop() { 72 // Do nothing. 73 } 74 75 @Override 76 public boolean isRunning() { 77 return false; 78 } 79 80 @Override 81 protected void onBoundsChange(Rect bounds) { 82 super.onBoundsChange(bounds); 83 applyGravity = true; 84 } 85 86 @Override 87 public ConstantState getConstantState() { 88 return state; 89 } 90 91 @Override 92 public void draw(Canvas canvas) { 93 if (applyGravity) { 94 Gravity.apply(BitmapState.GRAVITY, width, height, getBounds(), destRect); 95 applyGravity = false; 96 } 97 canvas.drawBitmap(state.bitmap, null, destRect, state.paint); 98 } 99 100 @Override 101 public void setAlpha(int alpha) { 102 int currentAlpha = state.paint.getAlpha(); 103 if (currentAlpha != alpha) { 104 state.setAlpha(alpha); 105 invalidateSelf(); 106 } 107 } 108 109 @Override 110 public void setColorFilter(ColorFilter colorFilter) { 111 state.setColorFilter(colorFilter); 112 invalidateSelf(); 113 } 114 115 @Override 116 public int getOpacity() { 117 Bitmap bm = state.bitmap; 118 return (bm == null || bm.hasAlpha() || state.paint.getAlpha() < 255) 119 ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE; 120 } 121 122 @Override 123 public Drawable mutate() { 124 if (!mutated && super.mutate() == this) { 125 state = new BitmapState(state); 126 mutated = true; 127 } 128 return this; 129 } 130 131 public Bitmap getBitmap() { 132 return state.bitmap; 133 } 134 135 static class BitmapState extends ConstantState { 136 private static final int DEFAULT_PAINT_FLAGS = Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG; 137 private static final Paint DEFAULT_PAINT = new Paint(DEFAULT_PAINT_FLAGS); 138 private static final int GRAVITY = Gravity.FILL; 139 140 final Bitmap bitmap; 141 142 int targetDensity; 143 Paint paint = DEFAULT_PAINT; 144 145 public BitmapState(Bitmap bitmap) { 146 this.bitmap = bitmap; 147 } 148 149 150 BitmapState(BitmapState other) { 151 this(other.bitmap); 152 targetDensity = other.targetDensity; 153 } 154 155 void setColorFilter(ColorFilter colorFilter) { 156 mutatePaint(); 157 paint.setColorFilter(colorFilter); 158 } 159 160 void setAlpha(int alpha) { 161 mutatePaint(); 162 paint.setAlpha(alpha); 163 } 164 165 void mutatePaint() { 166 if (paint == DEFAULT_PAINT) { 167 paint = new Paint(DEFAULT_PAINT_FLAGS); 168 } 169 } 170 171 @Override 172 public Drawable newDrawable() { 173 return new GlideBitmapDrawable(null, this); 174 } 175 176 @Override 177 public Drawable newDrawable(Resources res) { 178 return new GlideBitmapDrawable(res, this); 179 } 180 181 @Override 182 public int getChangingConfigurations() { 183 return 0; 184 } 185 } 186} 187