1package com.xtremelabs.robolectric.shadows; 2 3import static com.xtremelabs.robolectric.Robolectric.shadowOf; 4 5import android.content.res.Resources; 6import android.graphics.Bitmap; 7import android.graphics.Canvas; 8import android.graphics.ColorFilter; 9import android.graphics.Paint; 10import android.graphics.Shader.TileMode; 11import android.graphics.drawable.BitmapDrawable; 12 13import com.xtremelabs.robolectric.internal.Implementation; 14import com.xtremelabs.robolectric.internal.Implements; 15import com.xtremelabs.robolectric.internal.RealObject; 16 17@SuppressWarnings({"UnusedDeclaration"}) 18@Implements(BitmapDrawable.class) 19public class ShadowBitmapDrawable extends ShadowDrawable { 20 private Bitmap bitmap; 21 private ColorFilter colorFilter; 22 private String drawableCreateFromStreamSource; 23 private String drawableCreateFromPath; 24 25 @RealObject private BitmapDrawable realBitmapDrawable; 26 private TileMode tileModeX; 27 private TileMode tileModeY; 28 29 public void __constructor__(Bitmap bitmap) { 30 this.bitmap = bitmap; 31 } 32 33 public void __constructor__(Resources resources, Bitmap bitmap) { 34 this.bitmap = bitmap; 35 } 36 37 /** 38 * Draws the contained bitmap onto the canvas at 0,0 with a default {@code Paint} 39 * 40 * @param canvas the canvas to draw on 41 */ 42 @Implementation 43 public void draw(Canvas canvas) { 44 Paint paint = new Paint(); 45 paint.setColorFilter(colorFilter); 46 canvas.drawBitmap(realBitmapDrawable.getBitmap(), 0, 0, paint); 47 } 48 49 @Implementation 50 public void setColorFilter(android.graphics.ColorFilter colorFilter) { 51 this.colorFilter = colorFilter; 52 } 53 54 @Implementation 55 public android.graphics.Bitmap getBitmap() { 56 return bitmap; 57 } 58 59 /** 60 * Non-Android accessor that tells you the resource id that this {@code BitmapDrawable} was loaded from. This lets 61 * your tests assert that the bitmap is correct without having to actually load the bitmap. 62 * 63 * @return resource id from which this {@code BitmapDrawable} was loaded 64 * @deprecated use com.xtremelabs.robolectric.shadows.ShadowBitmap#getLoadedFromResourceId() instead. 65 */ 66 @Deprecated 67 @Override 68 public int getLoadedFromResourceId() { 69 return shadowOf(bitmap).getLoadedFromResourceId(); 70 } 71 72 // Used by ShadowDrawable.createFromStream() 73 public void setSource(String drawableCreateFromStreamSource) { 74 this.drawableCreateFromStreamSource = drawableCreateFromStreamSource; 75 } 76 77 public String getSource() { 78 return drawableCreateFromStreamSource; 79 } 80 81 //Used by ShadowDrawable.createFromPath() 82 public void setPath(String drawableCreateFromPath) { 83 this.drawableCreateFromPath = drawableCreateFromPath; 84 } 85 86 public String getPath() { 87 return drawableCreateFromPath; 88 } 89 90 @Implementation 91 public void setTileModeX(TileMode mode) { 92 tileModeX = mode; 93 } 94 95 @Implementation 96 public TileMode getTileModeX() { 97 return tileModeX; 98 } 99 100 @Implementation 101 public void setTileModeY(TileMode mode) { 102 tileModeY = mode; 103 } 104 105 @Implementation 106 public TileMode getTileModeY() { 107 return tileModeY; 108 } 109 110 @Implementation 111 public void setTileModeXY(TileMode modeX, TileMode modeY) { 112 setTileModeX(modeX); 113 setTileModeY(modeY); 114 } 115 116 @Override 117 @Implementation 118 public boolean equals(Object o) { 119 if (this == o) return true; 120 if (o == null || getClass() != ShadowBitmapDrawable.class) return false; 121 122 ShadowBitmapDrawable that = shadowOf((BitmapDrawable) o); 123 124 if (bitmap != null ? !bitmap.equals(that.bitmap) : that.bitmap != null) return false; 125 126 return super.equals(o); 127 } 128 129 @Override 130 @Implementation 131 public int hashCode() { 132 return bitmap != null ? bitmap.hashCode() : 0; 133 } 134 135 @Override 136 @Implementation 137 public String toString() { 138 return "ShadowBitmapDrawable{" + 139 "bitmap=" + bitmap + 140 '}'; 141 } 142} 143