Will Kru
Will Kru

Reputation: 5212

Transparent Bitmap in ImageView

I'd like to draw something over an existing bitmap while keeping the Bitmaps seperated. So the idea is to have a RelativeLayout and two ImageViews stacked onto each other, the top one holding the bitmap to be drawn to and the bottom one holding the bitmap with the background picture.

layout.xml (only relevant part)

<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
    <ImageView 
        android:id="@+id/photo_mask"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent" />
    <ImageView 
        android:id="@+id/photo"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

layout.java (only relevant part)

setContentView(R.layout.layout);

ImageView image = (ImageView) findViewById(R.id.photo);
image.setImageBitmap(mSomeImage);

mMaskPaint = new Paint();
mMaskPaint.setColor(0xFF0000);
mMaskPaint.setAlpha(128);

mMaskBitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888);
mMaskBitmap.eraseColor(Color.TRANSPARENT);

mMaskCanvas = new Canvas(mMaskBitmap);
mMaskCanvas.drawCircle(64, 64, 10, mMaskPaint);

ImageView mask = (ImageView) findViewById(R.id.photo_mask);
image.setImageBitmap(mMaskBitmap);

Note that mSomeImage is a 128x128 Bitmap, so it will match the mask Bitmap. I'm drawing a red circle in the middle of the mask Bitmap, which shows perfectly. However, the mask Bitmap doesn't show the background image through, instead showing a black background.

So I tried:

none of which seem to work. When I do a eraseColor(Color.BLUE), the background is blue with the red circle in the middle. When I set the alpha of the mask ImageView, the background is still black. When I comment out the setImageBitmap(mMaskBitmap), the background image shows.

What am I missing here?

Upvotes: 2

Views: 4755

Answers (1)

bill
bill

Reputation: 36

Your background is wrong. Change

ImageView mask = (ImageView) findViewById(R.id.photo_mask);
image.setImageBitmap(mMaskBitmap);

to

ImageView mask = (ImageView) findViewById(R.id.photo_mask);
mask.setImageBitmap(mMaskBitmap);

Upvotes: 2

Related Questions