Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android how to apply mask on ImageView?

So I tried the code from here: Creating an ImageView with a mask. I'm using the following images as original and mask:

enter image description hereenter image description here

However, the result I get is this:

enter image description here

Note that the window background is not black, but holo light (which on the galaxy nexus looks like a very pale gray, not completely white). The second image is the result I get when an item is selected on a list view.

If instead I create a new Bitmap using the same algorithm and then pass it to the image view instead of overriding onDraw(), it draws correctly:

Canvas canvas = new Canvas(); Bitmap mainImage = //get original image Bitmap maskImage = //get mask image Bitmap result = Bitmap.createBitmap(mainImage.getWidth(), mainImage.getHeight(), Bitmap.Config.ARGB_8888);  canvas.setBitmap(result); Paint paint = new Paint(); paint.setFilterBitmap(false);  canvas.drawBitmap(mainImage, 0, 0, paint); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawBitmap(maskImage, 0, 0, paint); paint.setXfermode(null);  imageView.setImageBitmap(result); 

I get the expected result:

enter image description here

Note the fade is correctly applied. This is more evident when a selection is made.

So what's going on on ImageView's onDraw method to create this black backdrop instead of letting the window background show through? What's interesting is that if the original image itself has some transparency, that transparency is respected, for example:

enter image description here

I can't figure it out by myself. I'd rather be able to do it on onDraw instead of pre-creating the bitmap because it only works for bitmaps as source and mask. I want to be able to do it with other drawables like gradients and solid colours but on those cases the width and height are not set.

like image 665
AngraX Avatar asked Feb 10 '13 18:02

AngraX


2 Answers

I have found the perfect combination for creating masking without black border after researching through all the stackoverflow posts. It suits my purpose quite well.

Currently I'm creating a draggable view using one normal image and a masking image (a png with transparency), so I'll need to override the onDraw function.

private Bitmap mImage = ...; private Bitmap mMask = ...;  // png mask with transparency private int mPosX = 0; private int mPosY = 0;  private final Paint maskPaint; private final Paint imagePaint;  public CustomView (final Context context) {     maskPaint = new Paint();     maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));      imagePaint = new Paint();     imagePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER)); }  /* TODO  if you have more constructors, make sure you initialize maskPaint and imagePaint  Declaring these as final means that all your constructors have to initialize them.  Failure to do so = your code won't compile. */  @Override public void onDraw(Canvas canvas) {     super.onDraw(canvas);      canvas.save();           canvas.drawBitmap(mMask, 0, 0, maskPaint);     canvas.drawBitmap(mImage, mPosX, mPosY, imagePaint);     canvas.restore(); } 
like image 62
morph85 Avatar answered Sep 18 '22 13:09

morph85


Answering my own question. The Xfermode was working as intended. The paint was making the resulting are of the canvas transparent (which was the canvas used by the window activity). Since the canvas itself was being set transparent, the window was showing what was behind it: the black background.

To do it properly, indeed a new Bitmap has to be created to hold the result of the alpha mask. I updated the code to take into account drawables of all types.

like image 36
AngraX Avatar answered Sep 19 '22 13:09

AngraX