Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在没有onActive的情况下启动自动启动动画,并设置AnimatedFloataState的初始值_Android_Android Jetpack Compose - Fatal编程技术网

Android 如何在没有onActive的情况下启动自动启动动画,并设置AnimatedFloataState的初始值

Android 如何在没有onActive的情况下启动自动启动动画,并设置AnimatedFloataState的初始值,android,android-jetpack-compose,Android,Android Jetpack Compose,在过去(alpha11之前),我可以在触发可组合函数时设置一个从0到1的值的动画,如下所示,在这里我可以设置初始值,也可以使用aniumateTo设置活动 val animatedProgress = animatedFloat(0f) onActive { animatedProgress.animateTo( targetValue = 1f, anim = infiniteRepeatable( animation =

在过去(alpha11之前),我可以在触发可组合函数时设置一个从0到1的值的动画,如下所示,在这里我可以设置
初始值
,也可以使用
aniumateTo设置
活动

val animatedProgress = animatedFloat(0f)
onActive {
    animatedProgress.animateTo(
        targetValue = 1f,
        anim = infiniteRepeatable(
            animation =
                tween(durationMillis = 2000, easing = LinearEasing),
        )
    )
}

val t = animatedProgress.value
然而,现在在alpha13中,我找不到一种方法来设置
初始值
,或者
动画设置为
。onActive的
现在也不推荐使用

我的代码如下

    val floatAnimation = animateFloatAsState(
        targetValue = 1f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 2000, easing = LinearEasing),
        )
    )
我怎么能

  • 将初始值设置为0
  • 启动动画(无需状态布尔值即可启动)
  • 重复设置从0到1的动画

看起来我必须使用布尔值来更改状态,并使用
LaunchEffect
启动并更改状态,如下所示

    var start by remember{ mutableStateOf(false) }

    val floatAnimation = animateFloatAsState(
        targetValue = if (start) 1f else 0f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 2000, easing = LinearEasing),
        )
    )
    LaunchedEffect(true) {
        start = true
    }

    val t = floatAnimation.value
不确定这是否是围绕它进行编码的最佳方式。

您可以使用API和
LaunchedEffect
组合。启动动画不需要布尔值

比如:

val animatedAlpha = remember { Animatable(0f) }

Box(
    Modifier
        .background(color = (Color.Blue.copy(alpha = animatedAlpha.value)))
        .size(100.dp,100.dp)

)

LaunchedEffect(animatedAlpha) {
    animatedAlpha.animateTo(1f,
        animationSpec = infiniteRepeatable(
            animation = tween(durationMillis = 2000, easing = LinearEasing)
        ))
}