3

I followed some other questions on the topic, and also API demos on XferModes, and attempted to create an ImageView that masks its content according to an alpha mask. The alpha bitmap contains 0 alpha for areas to be hidden and 255 alpha for areas to be shown.

This is the onDraw code:

@Override
protected void onDraw(Canvas canvas)
{
    super.onDraw(canvas);

    Paint paint = new Paint();
    paint.setFilterBitmap(false);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));

    canvas.drawBitmap(mMaskBitmap, 0, 0, paint);
    paint.setXfermode(null);

It doesn't work for me. If I put SRC_IN, the mask just gets drawn on top. If I put DST IN, I just see the original image.

Any advice?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
SirKnigget
  • 3,614
  • 2
  • 28
  • 61

1 Answers1

1

The problem was solved, I discovered that the ImageView and the mask bitmap's size had a difference of 4 pixels, which caused the whole thing to silently malfunction... Weird that there was no exception thrown.

Anyway, the above code works, provided that the mask size <= the ImageView size.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
SirKnigget
  • 3,614
  • 2
  • 28
  • 61
  • This didn't work for me as expected. So for you, this masks the content that the ImageView.onDraw() drew? – cottonBallPaws Sep 06 '12 at 18:10
  • I'd love to get some feedback from you. When I try the code above, the mask is correctly applied but instead of fading the image to show the background, it displays a black color behind it (as if the background was black). However, if I use the same technique to draw on a new canvas, the resulting bitmap is correctly rendered. What I'm doing now is creating the final bitmap I need to render (with the alpha bits correctly set with DST_IN) and then replace the original ImageView bitmap by the new one. Something on the ImageView#onDraw() is messing up the final rendering I think. – AngraX Feb 10 '13 at 17:18
  • Hmm can't think of anything like that... If I recall something about that I'll post here. – SirKnigget Feb 10 '13 at 23:12