I created a custom object that has a Bitmap field. I’m drawing several of these objects to the canvas of a View to mimic a scrolling horizontal image gallery.
When the user long presses one of the images, I want to change the opacity of the remaining Bitmaps to a specified percentage. This could give the impression that they’ve darkened for “edit mode”, or it could mean they’ve returned to normal. (Please note that I don’t want to permanently alter the Bitmaps. I want to be able to adjust their opacity on the fly.)
I pieced together the following code from various forums, and everything seems to be working except for the change in opacity. I’ve confirmed that my Bitmaps are mutable and have alpha every step of the way. What am I doing wrong?
Targeting Android 2.1, API Level 7
View (abridged for brevity):
public class CanvasStoryEdit2 extends View
{
public CanvasStoryEdit2(Context context, AttributeSet attrs) {
super(context, attrs);
for (int i = 0; i < getResources().getInteger(R.integer.maxAllowedSlides); i++)
{
ImageStoryEdit img = new ImageStoryEdit();
//test images
if (i == 0) { resource = R.drawable.a1; }
else if (i == 1) { resource = R.drawable.a2; }
else if (i == 2) { resource = R.drawable.a3; }
else if (i == 3) { resource = R.drawable.a4; }
else if (i == 4) { resource = R.drawable.a5; }
Bitmap bmp = BitmapFactory.decodeResource(getResources(), resource);
Log.d("TEST", "[" + Integer.toString(i) + "] - config: " + bmp.getConfig().toString());
Log.d("TEST", "[" + Integer.toString(i) + "] - hasAlpha: " + Boolean.toString(bmp.hasAlpha()));
Log.d("TEST", "[" + Integer.toString(i) + "] - isMutable: " + Boolean.toString(bmp.isMutable()));
final int imgHeight = bmp.getHeight() / (bmp.getWidth() / imgWidth);
bmp = Bitmap.createScaledBitmap(bmp, imgWidth, imgHeight, false);
Log.d("TEST", "[" + Integer.toString(i) + "] - config: " + bmp.getConfig().toString());
Log.d("TEST", "[" + Integer.toString(i) + "] - hasAlpha: " + Boolean.toString(bmp.hasAlpha()));
Log.d("TEST", "[" + Integer.toString(i) + "] - isMutable: " + Boolean.toString(bmp.isMutable()));
int width = bmp.getWidth();
int height = bmp.getHeight();
int[] pixels = new int[width * height];
bmp.getPixels(pixels, 0, width, 0, 0, width, height);
bmp.recycle();
bmp = null;
img.setBmp(Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888));
img.getBmp().setPixels(pixels, 0, width, 0, 0, width, height);
pixels = null;
Log.d("TEST", "[" + Integer.toString(i) + "] - config: " + img.getBmp().getConfig().toString());
Log.d("TEST", "[" + Integer.toString(i) + "] - hasAlpha: " + Boolean.toString(img.getBmp().hasAlpha()));
Log.d("TEST", "[" + Integer.toString(i) + "] - isMutable: " + Boolean.toString(img.getBmp().isMutable()));
imageStoryEditList.add(img);
}
}
Call made on long press:
{
img.setOpacity(50);
invalidate();
}
ImageStoryEdit (also abridged):
public class ImageStoryEdit
{
private int opacity;
public Bitmap bmp;
public Bitmap getBmp() {
return bmp;
}
public void setBmp(Bitmap bmp)
{
this.bmp = bmp;
UpdateRectF();
}
public int getOpacity()
{
return opacity;
}
public void setOpacity(int percent)
{
//ADJUST FOR RANGE OF 0-100%
percent = percent < 0 ? 0 : percent;
percent = percent > 100 ? 100 : percent;
this.opacity = percent;
int opacity = (int) (this.opacity * 2.55);
Log.d("TEST", "OPACITY = " + Integer.toString(percent) + " : " + Integer.toString(opacity));
adjustOpacity(opacity);
}
private void adjustOpacity(int opacity)
{
Log.d("TEST", "OPACITY = " + Integer.toString(opacity));
Log.d("TEST", this.bmp.getConfig().toString());
Log.d("TEST", "hasAlpha: " + Boolean.toString(this.bmp.hasAlpha()));
Log.d("TEST", "isMutable: " + Boolean.toString(this.bmp.isMutable()));
Bitmap bmp2 = this.bmp.copy(Config.ARGB_8888, true);
Canvas canvas = new Canvas(bmp2);
Paint paint = new Paint();
paint.setAlpha(opacity);
canvas.drawBitmap(bmp2, 0, 0, paint);
this.bmp = bmp2;
Log.d("TEST", this.bmp.getConfig().toString());
Log.d("TEST", "hasAlpha: " + Boolean.toString(this.bmp.hasAlpha()));
Log.d("TEST", "isMutable: " + Boolean.toString(this.bmp.isMutable()));
Log.d("TEST", "DONE");
}
}
I think I figured this one out on my own, but I welcome any feedback if anyone can offer additional insight.
I was able to do what I wanted by using a BitmapDrawable. It’s a wrapper for a Bitmap. Editing the Bitmap itself is, indeed, permanent. BitmapDrawables allow you to change certain parameters without directly affecting the underlying Bitmap.