Composable Function

animateValueAsState

Fire-and-forget animation function for any value.

ArbitraryValueTypeTransitionSample

@Composable
fun ArbitraryValueTypeTransitionSample() {
    @Composable
    fun ArbitraryValueTypeAnimation(enabled: Boolean) {
        // Sets up the different animation target values based on the [enabled] flag.
        val mySize =
            remember(enabled) {
                if (enabled) {
                    MySize(500.dp, 500.dp)
                } else {
                    MySize(100.dp, 100.dp)
                }
            }
        // Animates a custom type value to the given target value, using a [TwoWayConverter]. The
        // converter tells the animation system how to convert the custom type from and to
        // [AnimationVector], so that it can be animated.
        val animSize: MySize by
            animateValueAsState(
                mySize,
                TwoWayConverter<MySize, AnimationVector2D>(
                    convertToVector = { AnimationVector2D(it.width.value, it.height.value) },
                    convertFromVector = { MySize(it.v1.dp, it.v2.dp) },
                ),
            )
        Box(Modifier.size(animSize.width, animSize.height).background(color = Color.Red))
    }
}