Я пытаюсь смоделировать TorchView с фоновым изображением и изображением переднего плана. Он хорошо работает на API 27 и ниже, но рисует прямоугольник на API 28.
Есть идеи, почему он не работает на Android Pie?
Снимок экрана для API 27 и ниже
![API 27 and below](https://i.stack.imgur.com/m2PFS.png)
Снимок экрана по API 28
![API 28](https://i.stack.imgur.com/OMSGr.jpg)
Факел Посмотреть класс
class TorchView : View, OnTouchListener {
var mBitmapBackground: Bitmap? = null
var mBitmapForeground: Bitmap? = null
var mMask: Bitmap? = null
private var mPosX = 0f
private var mPosY = 0f
private lateinit var paintMask: Paint
private lateinit var paintBackground: Paint
private lateinit var paintForeground: Paint
private var radius = 150
constructor(context: Context) : super(context) {
init()
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
init()
}
fun initBitmaps(bitmapBackground: Bitmap, bitmapForeground: Bitmap, radius: Int){
this.radius = radius
mBitmapBackground = bitmapBackground
mBitmapForeground = bitmapForeground
mMask = makeRadGrad()
mPosX = (bitmapBackground.width/2 - radius).toFloat()
mPosY = (bitmapBackground.height/2 - radius).toFloat()
invalidate()
}
fun init() {
paintBackground = Paint()
paintMask = Paint()
paintMask.xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
paintForeground = Paint()
paintForeground.xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_OVER)
isFocusable = true
isFocusableInTouchMode = true
this.setOnTouchListener(this)
}
public override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val mask = mMask
val bitmapForeground = mBitmapBackground
val bitmapBackground = mBitmapForeground
if(mask != null && bitmapForeground != null && bitmapBackground != null){
canvas.save()
canvas.drawBitmap(bitmapBackground, 0f, 0f, paintBackground)
canvas.drawBitmap(mask, mPosX, mPosY, paintMask)
canvas.drawBitmap(bitmapForeground, 0f, 0f, paintForeground)
canvas.restore()
}
}
private fun makeRadGrad(): Bitmap {
val gradient = RadialGradient(
radius.toFloat(), radius.toFloat(), radius.toFloat(), -0xff0100,
0x00000000, android.graphics.Shader.TileMode.CLAMP
)
val p = Paint()
p.isDither = true
p.shader = gradient
val bitmap = Bitmap.createBitmap(radius*2, radius*2, Config.ARGB_8888)
val c = Canvas(bitmap)
c.drawCircle(radius.toFloat(), radius.toFloat(), radius.toFloat(), p)
return bitmap
}
override fun onTouch(v: View?, event: MotionEvent): Boolean {
mPosX = event.x - radius
mPosY = event.y - radius
invalidate()
return true
}
}