Created
December 4, 2022 01:28
-
-
Save fvilarino/6783a678c58d28b5de73a41e011fda21 to your computer and use it in GitHub Desktop.
Segmented Progress _RTL
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 gaps = (segments - 1) * segmentGap | |
val segmentWidth = (width - gaps) / segments | |
val barsWidth = segmentWidth * segments | |
val start: Float | |
val end: Float | |
// 1 | |
val isLtr = layoutDirection == LayoutDirection.Ltr | |
// 2 | |
if (isLtr) { | |
start = 0f | |
end = barsWidth * progress + (progress * segments).toInt() * segmentGap | |
} else { | |
start = width | |
end = (width - (barsWidth * progress + (progress * (segments - 1)).toInt() * segmentGap)) | |
} | |
repeat(segments) { index -> | |
val offset = index * (segmentWidth + segmentGap) | |
val segmentStart: Float | |
val segmentEnd: Float | |
// 3 | |
if (isLtr) { | |
segmentStart = start + offset | |
segmentEnd = (segmentStart + segmentWidth).coerceAtMost(end) | |
} else { | |
segmentEnd = width - offset | |
segmentStart = (segmentEnd - segmentWidth).coerceAtLeast(end) | |
} | |
// 4 | |
if (isLtr && offset <= end || !isLtr && segmentEnd > end) { | |
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