简体   繁体   English

Jetpack Compose Crossfade 在 Alpha 中损坏

[英]Jetpack Compose Crossfade broken in Alpha

My crossfade animations are no longer working since the release of Compose Alpha and I would really appreciate some help getting them working again.自 Compose Alpha 发布以来,我的淡入淡出动画不再起作用,我非常感谢一些帮助,让它们再次起作用。 I am fairly new to Android/Compose.我对 Android/Compose 相当陌生。 I understand that Crossfade is looking for a state change in its targetState to trigger the crossfade animation, but I am confused how to incorporate this.我了解 Crossfade 正在寻找其 targetState 中的 state 更改以触发交叉淡入淡出 animation,但我很困惑如何合并它。 I am trying to wrap certain composables in the Crossfade animation.我正在尝试在 Crossfade animation 中包装某些组合。

Here are the official docs and helpful playground example, but I still cannot get it to work since the release of Alpha https://developer.android.com/reference/kotlin/androidx/compose/animation/package-summary#crossfade https://foso.github.io/Jetpack-Compose-Playground/animation/crossfade/ Here are the official docs and helpful playground example, but I still cannot get it to work since the release of Alpha https://developer.android.com/reference/kotlin/androidx/compose/animation/package-summary#crossfade https: //foso.github.io/Jetpack-Compose-Playground/animation/crossfade/

Here is my code, in this instance I was hoping to use the String current route itself as the targetState as a mutableStateOf object.这是我的代码,在这种情况下,我希望将字符串当前路由本身用作 targetState 作为 mutableStateOf object。 I'm willing to use whatever will work though.我愿意使用任何可行的方法。

@Composable
fun ExampleComposable() {

val navBackStackEntry by navController.currentBackStackEntryAsState()
val currentRoute: String? = navBackStackEntry?.arguments?.getString(KEY_ROUTE)

val exampleRouteTargetState = remember { mutableStateOf(currentRoute)}

Scaffold(
    ...
    NavHost(navController, startDestination = "Courses") {
    composable("Route") {
        Crossfade(targetState = exampleRouteTargetState, animationSpec = tween(2000)) {
            ExampleComposable1()
        }
    }
    composable("Other Route")
        ExampleComposable2()
    }
)
...

} }

Shouldn't navigation trigger a state change of the "exampleRouteTargetState" variable and then trigger crossfade?导航不应该触发“exampleRouteTargetState”变量的 state 更改然后触发交叉淡入淡出吗? I could also wrap the composable elsewhere if you think wrapping it inside the NavHost may create an issue.如果您认为将可组合物包装在 NavHost 中可能会产生问题,我也可以将其包装在其他地方。 Thanks so much for the help!!非常感谢你的帮忙!!

Lately Google Accompanist has added a library which provides Compose Animation support for Jetpack Navigation Compose.. Do check it out.最近Google Accompanist添加了一个库,该库为 Jetpack Navigation Compose 提供 Compose Animation 支持。请检查一下。

https://github.com/google/accompanist/tree/main/navigation-animation https://github.com/google/accompanist/tree/main/navigation-animation

Still haven't gotten Crossfade working again, but I was able to implement some transitions inside NavHost.仍然没有让 Crossfade 再次工作,但我能够在 NavHost 中实现一些过渡。 Hope this helps someone.希望这可以帮助某人。 Here are the docs if you want to fine tune these high level animations: https://developer.android.com/jetpack/compose/animation#animatedvisibility如果您想微调这些高级动画,请参考以下文档: https://developer.android.com/jetpack/compose/animation#animatedvisibility

@ExperimentalAnimationApi
@Composable
fun ExampleAnimation(content: @Composable () -> Unit) {
    AnimatedVisibility(
        visible = true,
        enter = fadeIn(initialAlpha = 0.3f),
        exit = fadeOut(),
        content = content,
        initiallyVisible = false
    )
}

And then simply wrap your NavHost composable declarations with your animation like so然后像这样简单地用你的 animation 包装你的 NavHost 可组合声明

NavHost(navController, startDestination = "A Route") {
composable(Screen.YourObject.Route) {
    ExampleAnimation {
        YourComposable()
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM