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 ?

Finally i found the issue. The issue was setting the
android:backgroundof theConstraintLayoutthat we want to mask. For some reason this is not working in android 4. So to fix this i remove theandroid:backgroundof theConstraintLayoutand applied a mask color to the view ondisPatchDraw(), using thecanvas.drawColor(colorId), before thesuper.onDispatchDraw()was called.