title: Place Drawable on Imageview using PorterDuff.Mode.SRC_ATOP
body: <p>I have an ImageView:</p>
  
  <pre><code>ImageView imageView = findViewById(R.id.imageView);
  </code></pre>
  
  <p>I would like to achieve something like this (<em>Atop</em> figure)
  <a href="https://i.stack.imgur.com/B9syg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B9syg.png" alt="enter image description here"></a></p>
  
  <p><code>drawable1</code> is the original ImageView image, <code>drawable2</code> is the one I want to place over it:</p>
  
  <pre><code>Drawable drawable1 =  AppCompatResources.getDrawable(context, drawable1);
  Drawable drawable2 = AppCompatResources.getDrawable(context, drawable2);
  
  LD layerDrawable = new LD(new Drawable[]{
         drawable1,
         drawable2,
  });
  imageView.setImageDrawable(layerDrawable);
  </code></pre>
  
  <p>LD class:</p>
  
  <pre><code>public class LD extends LayerDrawable
  {
      private Paint p = new Paint();
  
      public LD(Drawable[] layers) {
          super(layers);
          p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
      }
  
      @Override
      public void draw(Canvas canvas) {
          super.draw(canvas);
          int count = canvas.saveLayer(null, null, Canvas.ALL_SAVE_FLAG);
  
          for (int i = 0, numberOfLayers = getNumberOfLayers(); i &lt; numberOfLayers; ++i) {
              this.getDrawable(i).draw(canvas);
              canvas.saveLayer(null, p, Canvas.ALL_SAVE_FLAG);
          }
          canvas.restoreToCount(count);
  
      }
  }
  </code></pre>
  
  <p>However this doesn't work, because the image I place over (drawable2) is placed over the whole imageView, not just over the non-transparent contents of the drawable1 image.</p>
  
  <p>How do I achieve Atop figure (placing drawable2 only over the 'filled content' of drawable1)?</p>
  
  <p>Edit:</p>
  
  <pre><code>p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
  canvas.drawBitmap(bitmap1, 0, 0, null);
  canvas.drawBitmap(bitmap2, 0, 0, p);
  </code></pre>