ste
05/05/2021, 8:12 AMCanvas + drawPath. Is there a legit way to optimize the whole thing? My idea was to save the `Path`(s) using staticCompositionLocalOf. More in threadste
05/05/2021, 8:15 AM@Composable
fun MyIcon() {
Canvas(modifier = Modifier.size(size = 50.dp)) {
drawPath(Path().apply {
// non expensive calculus, but they are repeated every time
}, color = Color.Red)
}
}ste
05/05/2021, 8:17 AMPath is created every time (but it's always the same), does make sense to store it somewhere (e.g using staticCompositionLocalOf)? There will be a performance gain?ste
05/05/2021, 8:20 AM@Composable
fun MyIcon() {
val path = LocalMyIconPath.current
Canvas(modifier = Modifier.size(size = 50.dp)) {
drawPath(path, color = Color.Red)
}
}nitrog42
05/05/2021, 8:21 AMBox(modifier = Modifier.drawWithCache {
val path = Path()//calculate path
onDrawWithContent {
drawPath(path)
}
})
but with a Canvas I don't think it's the way, and storing in a state might be wrong if the size change for any reason (I do understand that it shouldn't)ste
05/05/2021, 8:24 AMCompositionLocalProvider, so it shouldn't be a problem if the size changes, because the path would be rebuilt as wellste
05/05/2021, 8:25 AMdrawWithCache is fine if I need to draw just once (one icon).
Alternatively, I could just remember the Modifier.drawWithCache and reuse itAlbert Chang
05/06/2021, 12:36 AMPath is enough.