https://kotlinlang.org logo
#compose
Title
# compose
v

Vsevolod Ganin

02/11/2021, 12:00 PM
How can I use
FlingConfig
with new
animateDecay
? Can’t see where to apply
FlingConfig::adjustTarget
m

matvei

02/11/2021, 12:43 PM
Those two APIs went out of sync a but in alpha12. In the near future FlingConfig will be no more as you know it. There will be FlingBehavior class, but it won't have adjust target either. Basically the idea is to calculate it yourself, apply decay to your velocity, see the target value, and adjust it via your own adjustTarget lambda / function.
v

Vsevolod Ganin

02/11/2021, 12:47 PM
Got it, thanks!
👍 1
If anyone will stumble upon this issue and in need of quick dirty bridge between APIs, here is a little helper I came up with:
Copy code
suspend fun Animatable<Float, AnimationVector1D>.fling(
    initialVelocity: Float,
    flingConfig: FlingConfig,
    block: (Animatable<Float, AnimationVector1D>.() -> Unit)? = null,
): AnimationResult<Float, AnimationVector1D> {
    val decay = flingConfig.decayAnimation.generateDecayAnimationSpec<Float>()
    val targetValue = decay.calculateTargetValue(value, initialVelocity)
    val adjustedTarget = flingConfig.adjustTarget(targetValue)

    return if (adjustedTarget != null) {
        animateTo(
            targetValue = adjustedTarget.target,
            animationSpec = adjustedTarget.animation,
            initialVelocity = initialVelocity,
            block = block
        )
    } else {
        animateDecay(
            initialVelocity = initialVelocity,
            animationSpec = decay,
            block = block,
        )
    }
}
4 Views