https://kotlinlang.org logo
Title
e

elye

11/18/2021, 4:06 AM
Hi All, anyone know when I use coroutine on custom view onDraw (just for experiment purpose), it got drawn at weird coordinate?
override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        CoroutineScope(Dispatchers.Default).launch { // <-- add coroutine
            canvas.drawLine(
                0f, 0f,
                width.toFloat(), height.toFloat(),
                strokePaint
            )
        }
    }
More detail (with image) here
:not-kotlin: 1
n

Nick Allen

11/18/2021, 5:02 AM
Looks like you are calling
drawLine
while the "CUSTOM" text view is drawing. Every view draws to the same canvas. Translation and scale can be changed for every view that is drawn. See
Canvas
methods with descriptions that reference "matrix".
👍🏻 1
This is not Kotlin or coroutines related at all. You'd get similar weird behavior with Java code and/or any other background thread.
e

elye

11/18/2021, 5:24 AM
Makes sense. While it’s on the background, it’s drawn on the same canvas at a different coordinate measurement. Thanks! Sorry, I thought it was coroutine related, apparently it’s generic multithreading. 🙏 Thanks @Nick Allen