Rafs
10/28/2022, 7:13 PMRafs
10/28/2022, 7:15 PM/**
* This icon fades in and out after a delay whenever [icon] is changed
* but disappears immediately [icon] is replaced in the middle of an animation
*/
@Composable
private fun DisappearingIcon(
icon: ImageVector,
modifier: Modifier = Modifier,
contentDescription: String,
delay: Long
) {
val alpha = Animatable(0f)
val animationSpec = tween<Float>(500)
LaunchedEffect(icon) {
alpha.animateTo(1f, animationSpec = animationSpec)
delay(delay)
alpha.animateTo(0f, animationSpec = animationSpec)
}
Icon(
imageVector = icon,
modifier = modifier
.graphicsLayer {
this.alpha = alpha.value
},
contentDescription = contentDescription
)
}
Francesc
10/28/2022, 7:30 PMRafs
10/28/2022, 7:39 PMmattinger
10/29/2022, 2:56 AMvar alpha by remember { mutableStateOf(0f) }
LaunchedEffect(icon) {
animate(
initialValue = 0f,
targetValue = 1f,
animationSpec = tween(500)
) { value: Float, _: Float ->
alpha = value
}
delay(delay)
animate(
initialValue = 1f,
targetValue = 0f,
animationSpec = tween(500)
) { value: Float, _: Float ->
alpha = value
}
}
But if you're going to use Animatable, you should probably put it in a remember.mattinger
10/29/2022, 3:00 AMRafs
10/29/2022, 7:09 AManimate
function new cos I’ve never seen it before. It looks very clean, I’ll try that out