org.robolectric.shadows.ShadowBitmapDrawable Maven / Gradle / Ivy
package org.robolectric.shadows;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.util.ReflectionHelpers;
import org.robolectric.util.ReflectionHelpers.ClassParameter;
import static org.robolectric.Shadows.shadowOf;
import static org.robolectric.internal.Shadow.directlyOn;
/**
* Shadow for {@link android.graphics.drawable.BitmapDrawable}.
*/
@SuppressWarnings({"UnusedDeclaration"})
@Implements(BitmapDrawable.class)
public class ShadowBitmapDrawable extends ShadowDrawable {
private ColorFilter colorFilter;
String drawableCreateFromStreamSource;
String drawableCreateFromPath;
@RealObject private BitmapDrawable realBitmapDrawable;
/**
* Draws the contained bitmap onto the canvas at 0,0 with a default {@code Paint}
*
* @param canvas the canvas to draw on
*/
@Implementation
public void draw(Canvas canvas) {
Paint paint = new Paint();
paint.setColorFilter(colorFilter);
canvas.drawBitmap(realBitmapDrawable.getBitmap(), 0, 0, paint);
}
@Implementation
public Drawable mutate() {
Bitmap bitmap = realBitmapDrawable.getBitmap();
BitmapDrawable real = ReflectionHelpers.callConstructor(BitmapDrawable.class, ClassParameter.from(Bitmap.class, bitmap));
ShadowBitmapDrawable shadow = shadowOf(real);
shadow.colorFilter = this.colorFilter;
shadow.drawableCreateFromStreamSource = drawableCreateFromStreamSource;
return real;
}
@Implementation
public void setColorFilter(ColorFilter colorFilter) {
this.colorFilter = colorFilter;
directlyOn(realBitmapDrawable, BitmapDrawable.class).setColorFilter(colorFilter);
}
/**
* Non-Android accessor that tells you the resource id that this {@code BitmapDrawable} was loaded from. This lets
* your tests assert that the bitmap is correct without having to actually load the bitmap.
*
* @return resource id from which this {@code BitmapDrawable} was loaded
* @deprecated use ShadowBitmap#getCreatedFromResId() instead.
*/
@Override
public int getCreatedFromResId() {
return shadowOf(realBitmapDrawable.getBitmap()).getCreatedFromResId();
}
public String getSource() {
return drawableCreateFromStreamSource;
}
public String getPath() {
return drawableCreateFromPath;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy