data class Line(val start: Offset, val end: Offset, val color: Color = Color.Black, val strokeWidth: Dp = 5.dp)@Composablefun App(){ var lines = remember { mutableStateListOf<Line>() } Canvas( modifier = Modifier.fillMaxSize().pointerInput(Unit) { detectDragGestures { change, dragAmount -> change.consume() val line = Line( start = change.position - dragAmount, end = change.position, color = strokeColor, strokeWidth = strokeWidth ) lines.add(line) } } ) { lines.forEach { line -> drawLine( color = line.color, start = line.start, end = line.end, strokeWidth = line.strokeWidth.toPx(), cap = StrokeCap.Round ) } }}
This is the code for a painting app and the problem is straight forward, as my lines list grows as I draw, the UI starts lagging and hits performance bottle neck since I am iterating over lines which calls drawLine method on every recomposition.
While there's no single silver bullet solution to this I tried various methods to resolve this but to no avail. Any help would be nice since my app is facing ASO issues due to this bottleneck.
Note- Keeping a history of lines helps me in implementing a rewind functionality, so don't want to modify core structure of the codebase.