RitchyCZE
RitchyCZE

Reputation: 178

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.

Upvotes: 2

Views: 2100

Answers (1)

Thracian
Thracian

Reputation: 66674

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

Upvotes: 6

Related Questions