https://kotlinlang.org logo
#compose
Title
# compose
l

Landry Norris

02/23/2023, 7:16 PM
Does Modifier.drawBehind run whenever any part of the canvas changes? I put a log statement both in the parent Composable and in the Modifier.drawBehind lambda. The parent is clickable, and whenever I click on it, I see that the drawBehind lambda gets called in the logs, but the parent does not. No state captured in drawBehind changed in this case.
example
Copy code
@Composable
fun Foo(width: Int, height: Int) {
    println("Composing Foo")
    val paint = remember { Paint() }
    val bitmap = remember(width, height) {
        createBitmap(IntSize(width, height))
    }
    Box(modifier = Modifier.fillMaxSize()
        .then(
            if(bitmap != null)
                Modifier.drawBehind {
                    println("drawBehind")     
                    drawIntoCanvas { canvas ->
                       ...
                    }
                }
            else Modifier
        )
    )
}

@Composable
fun ClickableFoo(width: Int, height: Int) {
    Box(modifier = Modifier.clickable(...)) {
        Foo(width, height)
    }
}
The paint and bitmap are never changed, so I wouldn’t expect it to need to run again.
c

Colton Idle

02/23/2023, 9:18 PM
Isn't there a
drawBehindCached
or something?
er...
drawWithCache
? ive never really used the draw apis. so im just regurgitating stuff that ive seen thrown around in this slack. 🙈
l

Landry Norris

02/23/2023, 9:38 PM
I'll have to try out drawWithCache tomorrow. It looks like what I need.
l

Loney Chou

02/24/2023, 1:13 AM
drawBehind
,
drawWithContent
,
drawWithCache
won't save their content. Add a
.graphicsLayer()
before
drawXxx
to enable caching.
c

Colton Idle

02/24/2023, 1:31 AM
this video was uploaded 8 days ago. it might help too.

https://www.youtube.com/watch?v=1yiuxWK74vI

15 Views