onDraw 被调用,但没有绘制任何东西

onDraw is called, but nothing is being drawn

没有绘制任何东西,甚至调用了函数。

我试过了:
1.将super.onDraw(canvas)移动到onDraw
的开始和结束 2. 将其替换为 draw(canvas)
3.动态和静态创建我的视图(我的意思是,在XML)
4. 添加 setWillNotDraw(false)


class GameView(context: Context) : View(context) {

    init {
        setWillNotDraw(false) // doesn't change anything
        setBackgroundColor( Color.DKGRAY ) // this actually works, but onDraw is not, why?
    }

    private val ballPaint = Paint(ANTI_ALIAS_FLAG).apply {
        color = 0xfefefe
        style = Paint.Style.FILL
    }

    override fun onDraw(canvas: Canvas?) {
        println("Test") // printed!
        canvas!!.apply {
            drawCircle((width/2).toFloat(), (height/2).toFloat(), 100.0f, ballPaint )
        }
        super.onDraw(canvas)
    }
}

我做错了什么?

感谢 pskink,这就是问题所在

color = 0xfffefefe.toInt()