Daniel
02/27/2021, 10:56 PM@Composable
private fun Page(page: PageRenderer /* @Immutable data class, never changes */, turn: Dp = 0.dp /* changes frequently in animations */) {
Surface(
Modifier
.fillMaxSize()
.background(page.background)
.offset {
IntOffset(
-turn
.toPx()
.roundToInt(), 0
)
}
.drawWithCache {
onDrawWithContent {
drawContent()
drawIntoCanvas {
Timber.i("Drawing into canvas")
page.paint(it)
}
}
},
elevation = 15.dp
) {}
}
Based on my logs, I noticed that drawIntoCanvas
was called every time turn
changed, which I was very surprised by. (I thought the whole point of offset taking a lambda would prevent that)
I fixed the issue by adding another component, but I'm confused by why this was necessary
@Composable
private fun Page(page: PageRenderer, turn: Dp = 0.dp) {
Surface(
Modifier
.fillMaxSize()
.background(page.background)
.offset {
IntOffset(
-turn
.toPx()
.roundToInt(), 0
)
},
elevation = 15.dp
) {
Canvas(Modifier.fillMaxSize()) {
drawIntoCanvas {
Timber.i("Drawing into canvas")
page.paint(it)
}
}
}
Adam Powell
02/27/2021, 11:31 PM