Quantcast
Channel: Recent Questions - Stack Overflow
Viewing all articles
Browse latest Browse all 16420

Jetpack Compose - UI performance issues as lines grow in Canvas

$
0
0
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.


Viewing all articles
Browse latest Browse all 16420

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>