code: public static void drawMasked(Bitmap src, Drawable mask, Bitmap dest, int clearColor) {
          clearBitmap(dest, clearColor);
          Canvas canvas = new Canvas(dest);
  
          canvas.drawBitmap(src,
                  new Rect(0, 0, src.getWidth(), src.getHeight()),
                  new Rect(0, 0, dest.getWidth(), dest.getHeight()),
                  new Paint(Paint.FILTER_BITMAP_FLAG));
  
          if (mask instanceof BitmapDrawable) {
              ((BitmapDrawable) mask).getPaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
          } else if (mask instanceof NinePatchDrawable) {
              ((NinePatchDrawable) mask).getPaint().setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
          } else {
              throw new RuntimeException("Supported only BitmapDrawable or NinePatchDrawable");
          }
          mask.setBounds(0, 0, mask.getIntrinsicWidth(), mask.getIntrinsicHeight());
          mask.draw(canvas);
          canvas.setBitmap(null);
      }
docs: Drawing src bitmap to dest bitmap with applied mask.
  
  @param src        source bitmap
  @param mask       bitmap mask
  @param dest       destination bitmap
  @param clearColor clear color