title: Android how to apply mask on ImageView?
body: <p>So I tried the code from here: <a href="https://stackoverflow.com/questions/8657144/creating-an-imageview-with-a-mask">Creating an ImageView with a mask</a>. I'm using the following images as original and mask:</p>
  
  <p><img src="https://i.stack.imgur.com/6ebgf.png" alt="enter image description here"><img src="https://i.stack.imgur.com/W2C7t.png" alt="enter image description here"></p>
  
  <p>However, the result I get is this:</p>
  
  <p><img src="https://i.stack.imgur.com/2cwCm.png" alt="enter image description here"></p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>I get the expected result:</p>
  
  <p><img src="https://i.stack.imgur.com/FaEmY.png" alt="enter image description here"></p>
  
  <p>Note the fade is correctly applied. This is more evident when a selection is made.</p>
  
  <p>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:</p>
  
  <p><img src="https://i.stack.imgur.com/kalqp.png" alt="enter image description here"></p>
  
  <p>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.</p>