Android PorterDuff masking with 9-patch is leaving a black square background in android 4.4

128 Views Asked by At

I'm trying to mask an ConstraintLayout with an 9-patch mask. In versions above Android 4.4 works normally. For Android 4.4 a black square appears on the screen like this: even with a different 9-patch.

This is the code where i mask with the 9-patch. The backgroundMask var is an NinePatchDrawable.

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
    backgroundMask?.setBounds(0, 0, w, h)

    super.onSizeChanged(w, h, oldw, oldh)
}

override fun dispatchDraw(canvas: Canvas) {
    super.dispatchDraw(canvas)

    backgroundMask?.let {
        it.paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_IN)
        it.draw(canvas)
    }
}

I also set the setLayerType(LAYER_TYPE_HARDWARE, null) and tried the setLayerType(LAYER_TYPE_SOFTWARE, null) in the constructor of the view, but also not works for android 4.4.

Is there anything that I can use to fix this in this Android version ?

1

There are 1 best solutions below

0
pcruz_17 On

Finally i found the issue. The issue was setting the android:background of the ConstraintLayout that we want to mask. For some reason this is not working in android 4. So to fix this i remove the android:background of the ConstraintLayout and applied a mask color to the view on disPatchDraw(), using the canvas.drawColor(colorId), before the super.onDispatchDraw() was called.