按HOME后在SurfaceViiew上绘制透明背景

时间:2020-03-06 06:15:03

标签: android surfaceview

这是我的演示 我想画出我按下的最后一点,同时保持背景透明

如果我不按HOME,就可以正常工作。

但是如果按HOME,则无法清除画布上的绘制。

首先按SurfaceView,如下图所示播放 First Press The SurfaceView

按HOME并返回到Application,然后再次按SurfaceView Press HOME and back to the Application , then Press the SurfaceView again

以前无法清除黑点!

我想要的是屏幕只显示一个黑点

那么如何正确清除背景?


class MyView : SurfaceView, SurfaceHolder.Callback {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    )

    var mHolder : SurfaceHolder? = null
    val mPaint  = Paint().apply {
        setColor(Color.BLACK)
        strokeWidth = 42f
    }

    init {
        holder.addCallback(this)
        setZOrderOnTop(true);
        setBackgroundColor(Color.TRANSPARENT)
    }

    override fun surfaceChanged(holder: SurfaceHolder?, format: Int, width: Int, height: Int) {
    }

    override fun surfaceDestroyed(holder: SurfaceHolder?) {
        mHolder = null
    }

    override fun surfaceCreated(holder: SurfaceHolder?) {
        mHolder = holder
        mHolder?.setFormat(PixelFormat.TRANSLUCENT)
        holder!!.lockCanvas().let {
            it!!.drawColor(Color.WHITE)
            holder!!.unlockCanvasAndPost(it)

        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
       {
            try {
                val canvas:Canvas? = holder?.lockCanvas()
                if (canvas!= null ){
                    canvas.drawColor(Color.TRANSPARENT,PorterDuff.Mode.CLEAR)
                    canvas.drawPoint(event?.x?:0F,event?.y?:0F,mPaint)
                    holder?.unlockCanvasAndPost(canvas)
                }
            } catch (e: Exception) {
            }
        }.invoke()
        return super.onTouchEvent(event)
    }

}


0 个答案:

没有答案