Colton Idle
01/20/2023, 3:43 AMFrancesc
01/20/2023, 5:54 AM@Composable
fun AnimatedBox(
modifier: Modifier = Modifier,
startColor: Color = Color.Transparent,
endColor: Color = MaterialTheme.colorScheme.primary,
) {
var trigger by remember {
mutableStateOf(false)
}
val color by animateColorAsState(
targetValue = if (trigger) endColor else startColor,
animationSpec = tween(
durationMillis = 1000,
delayMillis = 300
)
)
LaunchedEffect(key1 = Unit) {
trigger = true
}
Box(
modifier = modifier.background(color)
)
}
Francesc
01/20/2023, 6:21 AM@Composable
fun AnimatedBox(
modifier: Modifier = Modifier,
color: Color = MaterialTheme.colorScheme.primary,
) {
val animatable = remember { Animatable(0f) }
LaunchedEffect(key1 = Unit) {
animatable.animateTo(
targetValue = 1f,
animationSpec = tween(
durationMillis = 1000,
delayMillis = 300
),
)
}
Box(
modifier = modifier.graphicsLayer {
alpha = animatable.value
}.background(color)
)
}
Francesc
01/20/2023, 6:24 AManimateFloatAsState
to animate the alpha value using a trigger like in the 1st example, there are multiple ways to skin this catste
01/20/2023, 8:45 AMval whatever by produceState(initialValue) {
animate(initialValue, targetValue, animationSpec = tween(delayMillis = 300)) { value, _ ->
this.value = value
}
}
If you need a spring (or another animation spec that doesn't support delaying), you can just call delay(300
before animae(...)