Blog Infos
Author
Published
Topics
, ,
Published
Open-sourcing this Jetpack Compose widget

Breath effect when reaching the last segment

“Kamehameha-like” animation

Drawing Custom Shapes in a Canvas With Compose
@Composable
fun SegmentedProgressBar() {
Canvas(
modifier = Modifier
onDraw = {
// Draw the segments
}
)
}
@Composable
fun SegmentedProgressBar(
segmentCount: Int,
modifier: Modifier = Modifier,
spacing: Dp = 0.dp,
angle: Float = 0f,
segmentColor: SegmentColor = SegmentColor(),
) {
val computer = remember { SegmentCoordinatesComputer() }
val spacingPx = LocalDensity.current.run { spacing.toPx() }
Canvas(
modifier = modifier.fillMaxWidth(),
onDraw = {
(0 until segmentCount).forEach { position ->
val segmentCoordinates = computer.segmentCoordinates(
position = position,
segmentCount = segmentCount,
width = size.width,
height = size.height,
spacing = spacingPx,
angle = angle
)
drawSegment(
coordinates = segmentCoordinates,
color = segmentColor
)
}
}
)
}

I’ve created an extension function to draw the segment as we’ll need it to draw the progress segment.

private fun DrawScope.drawSegment(coordinates: SegmentCoordinates, color: SegmentColor) {
val path = Path().apply {
reset()
moveTo(coordinates.topLeftX, 0f)
lineTo(coordinates.topRightX, 0f)
lineTo(coordinates.bottomRightX, size.height)
lineTo(coordinates.bottomLeftX, size.height)
close()
}
drawPath(
path = path,
color = color.color,
alpha = color.alpha,
)
}
view raw drawSegment.kt hosted with ❤ by GitHub

Finally, drawing the progress bar results in retrieving its coordinates from the SegmentedCoordinatesComputer and calling the drawSegment extension method.

@Composable
fun SegmentedProgressBar(
segmentCount: Int,
modifier: Modifier = Modifier,
progress: Float = 0f,
spacing: Dp = 0.dp,
angle: Float = 0f,
segmentColor: SegmentColor = SegmentColor(),
progressColor: SegmentColor = SegmentColor(),
) {
// Init computer
Canvas(
modifier = modifier.fillMaxWidth(),
onDraw = {
// Draw segments
val progressCoordinates = computer.progressCoordinates(
progress = progress.coerceIn(0f, segmentCount.toFloat()),
segmentCount = segmentCount,
width = size.width,
height = size.height,
spacing = spacingPx,
angle = angle
)
drawSegment(
coordinates = progressCoordinates,
color = progressColor
)
}
)
}

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

, , ,

Caution! App Work Ahead! Architecture Design for Design Libraries

A well-constructed design library connects design and feature teams in a meaningful and effective way.
Watch Video

Caution! App Work Ahead! Architecture Design for Design Libraries

R. Lee Creasy & Rhys Stever
Principal Engineer & Engineer
Comcast

Caution! App Work Ahead! Architecture Design for Design Libraries

R. Lee Creasy & Rh ...
Principal Engineer & ...
Comcast

Caution! App Work Ahead! Architecture Design for Design Libraries

R. Lee Creasy & ...
Principal Engineer & Engi ...
Comcast

Jobs

Animation with Compose
val animatedProgress by animateFloatAsState(targetValue = progress)
val progressCoordinates = computer.progressCoordinates(
progress = animatedProgress,
// Other arguments
)
Was migrating to Compose worth it?
Using This Widget in Your Project

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
It’s one of the common UX across apps to provide swipe to dismiss so…
READ MORE
blog
In this part of our series on introducing Jetpack Compose into an existing project,…
READ MORE
blog
In the world of Jetpack Compose, where designing reusable and customizable UI components is…
READ MORE
blog
Material3 is the newest iteration of Material Design, with dynamic theming, revised components and…
READ MORE
Menu