Is there a way to delay a Spring AnimationSpec in Jetpack Compose Animation?

Viewed 632

In Jetpack Compose Animation, for tween AnimationSpec, we can have a delay

                val translationY by transition.animateFloat(
                    transitionSpec = {
                        tween(
                            delayMillis = 1000, // Delay here
                            durationMillis = 2000
                        )
                    },
                    label = ""
                ) { if (it) 0f else 180f }

However, for Spring AnimationSpec, we only have dampingRatio (0.25f shown below) and Stiffness (100f shown below). There's no way to add a delay.

                val translationY by transition.animateFloat(
                    transitionSpec = {
                        spring(0.25f, 100f)
                    },
                    label = ""
                ) { if (it) 0f else 180f }

How to add a delay for Animation using Spring AnimationSpec?

1 Answers

I managed to add the bouncing effect to tween that quite similar to spring. Just use

easing = { OvershootInterpolator().getInterpolation(it) }

OvershootInterpolator comes from android.view.animation package.

You can provide a tension argument into OvershootInterpolator by its constructor to change bounciness.

Related