Skip to content

Instantly share code, notes, and snippets.

@kamaravichow
Created March 7, 2022 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kamaravichow/0f5c0dca03d2e9469020fdcb83d64da3 to your computer and use it in GitHub Desktop.
Save kamaravichow/0f5c0dca03d2e9469020fdcb83d64da3 to your computer and use it in GitHub Desktop.
Composable function for circular progress bar with gradience and animation in Jetpack Compose
@Composable
fun CircularProgressBar(
percentage: Float,
radius: Dp = 80.dp,
animationDuration: Int = 1000,
) {
var animFinished by remember {
mutableStateOf(false)
}
val current_percent = animateFloatAsState(
targetValue = if (animFinished) percentage else 0f,
animationSpec = tween(
durationMillis = animationDuration,
)
)
LaunchedEffect(key1 = true) {
animFinished = true
}
Box(
contentAlignment = Alignment.Center,
modifier = Modifier.size(radius * 2) // diameter
) {
Canvas(modifier = Modifier.size(radius * 2)) {
drawArc(
brush = Brush.linearGradient(
colors = listOf(
Color(0xFF1A3194),
Color(0xFF1D41C5),
Color(0xFF7F98FF)
)
),
startAngle = -90f,
sweepAngle = 360 * current_percent.value,
useCenter = false,
style = Stroke(
7.dp.toPx(),
cap = StrokeCap.Round,
),
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment