Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose run animation after recomposition

Is there any way how to run animation once recomposition (of screen, or some composable) is done? When animation is running and there is recomposition at the same time, animation has very bad performance (it is not smooth at all). I tried delay to delay animation for a while, but I don't think that's a good idea. There must be better way of doing this.

like image 714
RitchyCZE Avatar asked Jan 30 '26 00:01

RitchyCZE


1 Answers

Please provide producible example so you won't need to ask it again. You can run an animation with Animatable calling animatable.animateTo() inside LaunchedEffect after composition is completed. And instead of creating recomposition by using Modifier.offset(), Modifier.background() you can select Modifiers counterparts with lambda.

https://developer.android.com/jetpack/compose/performance/bestpractices#defer-reads

You can animate alpha for instance without triggering recomposition after composition is completed inside Draw phase of frame

val animatable = remember {
    Animatable(0f)
}

LaunchedEffect(key1 = Unit) {
    animatable.animateTo(1f)
}

Box(
    Modifier
        .size(100.dp)
        .drawBehind {
            drawRect(Color.Red.copy(alpha = animatable.value))
        }
)

https://stackoverflow.com/a/73274631/5457853

like image 124
Thracian Avatar answered Feb 01 '26 16:02

Thracian