Skip to content

Instantly share code, notes, and snippets.

@fvilarino
Created December 4, 2022 01:20
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/d295c6466e2fb68427897f598aeb903f to your computer and use it in GitHub Desktop.
Save fvilarino/d295c6466e2fb68427897f598aeb903f to your computer and use it in GitHub Desktop.
Segmented Progress - Generalized
private fun DrawScope.drawSegments(
// 1
progress: Float,
color: Color,
strokeWidth: Float,
segments: Int,
segmentGap: Float,
) {
val width = size.width
val start = 0f
// 2
val end = width * progress
val gaps = (segments - 1) * segmentGap
val segmentWidth = (width - gaps) / segments
repeat(segments) { index ->
val offset = index * (segmentWidth + segmentGap)
// 3
if (offset < end) {
val segmentEnd = (offset + segmentWidth).coerceAtMost(end)
drawLine(
color,
Offset(start + offset, 0f),
Offset(segmentEnd, 0f),
strokeWidth
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment