Skip to content

Instantly share code, notes, and snippets.

@jefisu
Created April 3, 2022 18:55
Show Gist options
  • Save jefisu/f2b63a6f88618de2f9622157ec1fa13a to your computer and use it in GitHub Desktop.
Save jefisu/f2b63a6f88618de2f9622157ec1fa13a to your computer and use it in GitHub Desktop.
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.tween
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
@Composable
fun CustomProgressBar(
percentage: Float,
number: Int = 100,
color: Color = Color.Green,
strokeWidth: Dp = 8.dp,
radiusSize: Dp = 150.dp,
textStyle: TextStyle = MaterialTheme.typography.h3,
animDuration: Int = 1000
) {
val currentPercentage = remember { Animatable(0F) }
LaunchedEffect(key1 = true) {
currentPercentage.animateTo(
targetValue = percentage,
animationSpec = tween(
durationMillis = animDuration
)
)
}
Box(
contentAlignment = Alignment.Center,
modifier = Modifier
.size(radiusSize)
.drawBehind {
drawArc(
color = color,
startAngle = -90F,
sweepAngle = 360F * currentPercentage.value,
useCenter = false,
style = Stroke(
width = strokeWidth.toPx(),
cap = StrokeCap.Round
)
)
}
) {
Text(
text = (currentPercentage.value * number).toInt().toString(),
style = textStyle
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment