Created
December 4, 2022 01:20
-
-
Save fvilarino/d295c6466e2fb68427897f598aeb903f to your computer and use it in GitHub Desktop.
Segmented Progress - Generalized
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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