Created
December 4, 2022 01:24
-
-
Save fvilarino/4e0da4bd638462a1db29c385bd61a069 to your computer and use it in GitHub Desktop.
Segmented Progress - Final
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( | |
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