如何在android中同步两个视图之间实现滑入和滑出动画 [英] How to achieve slide in and out animation in android compose between two Views Synchronized

查看:22
本文介绍了如何在android中同步两个视图之间实现滑入和滑出动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用 AnimatedVisibility 可组合函数并实现可见性动画的滑入动画,但我想要实现的是当一个布局进入动画时另一个布局在退出动画中,类似于下图.

I know I can use the AnimatedVisibility Composable function and achieve slide-in animation for the visibility animation, but what I want to achieve is when one layout is in entering animation the other in the exit animation, something similar to the image below.

NB :我知道我应该为不同的屏幕使用 Navigation compose 并且目的地之间的动画仍在开发中,但我想在屏幕的一部分内容上实现这一点,类似于淡入淡出动画.

NB : I know that I should use Navigation compose for different screens and that animation between destinations is still under development, but I want to achieve this on the content of a part of screen, similar to CrossFade Animation.

推荐答案

在深入研究 CrossFade 的代码后,我为横向滑动实现了一个类似的方法,并在按下 backButton 时启用反向动画

After digging in the code of CrossFade I implemented a similar one for the cross slide and it enables reverse animation for when pressing backButton

这里是:https://gist.github.com/DavidIbrahim/5f4c03867a252525a2572572587f4de976822c2a225

Here it is : https://gist.github.com/DavidIbrahim/5f4c0387b571f657f4de976822c2a225

使用示例

@Composable
fun CrossSlideExample(){
    var currentPage by remember { mutableStateOf("A") }
    CrossSlide(targetState = currentPage, reverseAnimation: Boolean = false) { screen ->
        when (screen) {
            "A" -> Text("Page A")
            "B" -> Text("Page B")
        }
    }
}

这篇关于如何在android中同步两个视图之间实现滑入和滑出动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆