Created
June 11, 2021 11:33
-
-
Save JustJerem/0f2f03f05b1b839316e6cf23bc4c9a28 to your computer and use it in GitHub Desktop.
Animate ProgressBar Compose
This file contains hidden or 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
import androidx.compose.animation.core.animateFloatAsState | |
import androidx.compose.animation.core.tween | |
import androidx.compose.foundation.background | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.shape.CircleShape | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.draw.clip | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.unit.Dp | |
import androidx.compose.ui.unit.dp | |
@Composable | |
fun ProgressLinearBar( | |
modifier: Modifier = Modifier, | |
value: Int, | |
maxValue: Int, | |
statColor: Color = MaterialTheme.colors.primary, | |
height: Dp = 8.dp, | |
animDuration: Int = 2000, | |
animDelay: Int = 500, | |
) { | |
var animationPlayed by remember { | |
mutableStateOf(false) | |
} | |
val curPercent = animateFloatAsState( | |
targetValue = if (animationPlayed) { | |
value / maxValue.toFloat() | |
} else 0f, | |
animationSpec = tween( | |
animDuration, | |
animDelay | |
) | |
) | |
LaunchedEffect(key1 = true) { | |
animationPlayed = true | |
} | |
Box( | |
modifier = modifier | |
.fillMaxWidth() | |
.height(height) | |
.clip(CircleShape) | |
.background( | |
Color(0xFFE9E9E9) | |
) | |
) { | |
Row( | |
horizontalArrangement = Arrangement.SpaceBetween, | |
verticalAlignment = Alignment.CenterVertically, | |
modifier = Modifier | |
.fillMaxHeight() | |
.fillMaxWidth(curPercent.value) | |
.clip(CircleShape) | |
.background(statColor) | |
.padding(horizontal = 8.dp) | |
) {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment