'Android canvas drawbitmap xfermode clear is drawing rectangle instead of bitmap

I'm making an app in Android using Kotlin. I have a layout that display a mask of a face shape. This is what I want to achieve:

target

This is my result right now:

result

In my code, I have a view which define a black background. This is the onDraw method that I override:

override fun onDraw(canvas: Canvas?) {

    if(canvas != null) {
        val bitmap = mDrawable!!.toBitmap()
        val marginV = 64

        val left = mDrawable!!.left.toFloat()
        val top = mDrawable!!.top.toFloat() + marginV

        val paint = Paint(Paint.ANTI_ALIAS_FLAG)
        paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)

        // draw hole
        canvas.drawBitmap(bitmap, left, top, paint)
        // draw shape
        canvas.drawBitmap(bitmap, left, top, null)
    }

    super.onDraw(canvas)
}

I really don't know what is the problem here. I already made something similar using a rectangle instead of a bitmap and it works well.

Maybe the problem is my vector drawable ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source