Skip to content

Instantly share code, notes, and snippets.

@fvilarino
Created December 4, 2022 01:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvilarino/4e0da4bd638462a1db29c385bd61a069 to your computer and use it in GitHub Desktop.
Save fvilarino/4e0da4bd638462a1db29c385bd61a069 to your computer and use it in GitHub Desktop.
Segmented Progress - Final
private fun DrawScope.drawSegments(
progress: Float,
color: Color,
segmentHeight: Float,
segments: Int,
segmentGap: Float,
) {
val width = size.width
val start = 0f
val gaps = (segments - 1) * segmentGap
val segmentWidth = (width - gaps) / segments
val barsWidth = segmentWidth * segments
// 1
val end = barsWidth * progress + (progress * segments).toInt() * segmentGap
repeat(segments) { index ->
val offset = index * (segmentWidth + segmentGap)
if (offset < end) {
val segmentEnd = (offset + segmentWidth).coerceAtMost(end)
val segmentStart = start + offset
drawRect(
color,
Offset(segmentStart, 0f),
Size(segmentEnd - segmentStart, segmentHeight)
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment