Hello, have you guys ever needed the opposite of `...
# compose
s
Hello, have you guys ever needed the opposite of
AnimateDecay
? E.g. accelerate until a target velocity is reached. I guess it shouldn't be that hard to implement 😅 Am I missing something?
d
What's your use case?
s
I'd like to animate an endless (but pausable) rotation of a composable. When pausing, the rotation should decelerate until velocity is 0 (achieved using animteDecay). When resuming, the rotation should accelerate before starting the endless, linear rotation
Copy code
val rotationAnimatable = remember {
    Animatable(0f)
}

LaunchedEffect(rotationAnimatable) {
    snapshotFlow { playerState.shouldBePlaying }
        .collectLatest { shouldBePlaying ->
            if (shouldBePlaying) {
                // Sudden resume
                rotationAnimatable.animateTo(
                    rotationAnimatable.value + 360f,
                    infiniteRepeatable(tween(1818, easing = LinearEasing), repeatMode = RepeatMode.Restart),
                )
            } else {
                // Smooth pause
                rotationAnimatable.animateDecay(200f, exponentialDecay())
            }
        }
}
The animation looks good when pausing - but I can't find a way to make it look the same when resuming 😅
k
have you tried tweaking
initialVelocity
in
animateTo
d
If you figure out what that speed is that you want to accelerate to, then a custom
animationSpec
could work. Then when resuming, you'd need to do in sequence:
Copy code
rotationAnimatable.animateTo(someTargetForAccelerating, yourCustomSpec)
rotationAnimatable.animateTo( rotationAnimatable.value + 360f,
                    infiniteRepeatable(tween(1818, easing = LinearEasing), repeatMode = RepeatMode.Restart))