android 如何使用Jetpack Compose和AnimatedVisibility仅为项目可见性设置一次动画?

0ve6wy6x  于 2023-05-12  发布在  Android
关注(0)|答案(1)|浏览(152)

所以当我加上

val state = remember {
    MutableTransitionState(false)
}
state.targetState = true
AnimatedVisibility(
    visibleState = state,
    enter = slideInHorizontally(
        animationSpec = tween(
            durationMillis = 250,
            delayMillis = animationDelay
        ),
        initialOffsetX = {
            screenWidthPx
        }
    )
) {
    content()
}

当我从另一个(popBackStack/naivateUp)返回到这个屏幕或在配置更改期间,动画一直被触发。
如何只设置一次动画?

mnemlml8

mnemlml81#

每次在remember范围之外,都将目标状态显式设置为true,现在目标状态也将被记住,并且除非将目标状态设置为false,否则它将不会动画

val state = remember {
    MutableTransitionState(false).apply {
      targetState = true
    }
}
AnimatedVisibility(
    visibleState = state,
    enter = slideInHorizontally(
        animationSpec = tween(
            durationMillis = 250,
            delayMillis = animationDelay
        ),
        initialOffsetX = {
            screenWidthPx
        }
    )
) {
    content()
}

相关问题