Skip to content

Instantly share code, notes, and snippets.

@dellisd
Created April 15, 2021 19:03
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 dellisd/da2ec46ffbea88109d339d080ece92b6 to your computer and use it in GitHub Desktop.
Save dellisd/da2ec46ffbea88109d339d080ece92b6 to your computer and use it in GitHub Desktop.
/**
* Like a progress indicator, but drawn as an arc.
*
* @param progress The progress to indicate, or how much of the arc should be shaded, as a percentage
* @param color The color of the arc
* @param width The width of the stroke used to draw the arc
*/
@Composable
fun ProgressArc(
progress: Float,
modifier: Modifier = Modifier,
color: Color = MaterialTheme.colors.primary,
width: Dp = 8.dp
) {
val transition = animateFloatAsState(targetValue = progress, animationSpec = tween())
Canvas(modifier = modifier) {
val offset = Offset(width.toPx() / 2, width.toPx() / 2)
val stroke = Stroke(width.toPx(), cap = StrokeCap.Square)
val size = Size(this.size.width - width.toPx(), this.size.height - width.toPx())
drawArc(
color.copy(alpha = ProgressIndicatorDefaults.IndicatorBackgroundOpacity),
startAngle = 135f,
sweepAngle = 270f,
useCenter = false,
style = stroke,
topLeft = offset,
size = size
)
drawArc(
color,
startAngle = 135f,
sweepAngle = 270f * transition.value,
useCenter = false,
style = stroke,
topLeft = offset,
size = size
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment