简体   繁体   English

如何在 Jetpack Compose 中为图像设置动画?

[英]How to Animate An Image in Jetpack Compose?

I haven't seen any posts that are searching for what I want.我还没有看到任何帖子正在寻找我想要的东西。 I simply want to animate an icon to go across the screen.我只是想为一个图标设置动画以穿过屏幕。 Just an Image, to go left to right upon showing the composable.只是一个图像,在显示可组合件时从左到右。

So far I've only seen that Animations are launched with a button click or when something happens, but I want this Animation to launch when the Composable in first in view到目前为止,我只看到动画是通过单击按钮或发生某些事情时启动的,但我希望这个动画在 Composable 首次出现时启动

您可以为此使用 MotionLayout,对于动画的进度,您可以使用每隔几毫秒上升一次的计数器,具体取决于您希望动画持续多长时间。

I found an answer that is very simple, I couldn't use AnimateVisibility because you can't really modify the horizontal slide location of the object.我找到了一个非常简单的答案,我不能使用 AnimateVisibility 因为你不能真正修改对象的水平滑动位置。 Here is a very very simple solution to my problem:这是我的问题的一个非常简单的解决方案:

var visible by remember { mutableStateOf(false) }

val animationTime = 600
val animationDelayTime = 5

val arrowStartLocation = Offset(0F, 0F)
val arrowEndLocation = Offset(100F, 0F)

LaunchedEffect(Unit) {
        visible = true
    }
val arrowLocation by animateOffsetAsState(
    targetValue = if (visible) arrowEndLocation else arrowStartLocation,
    animationSpec = tween(animationTime, animationDelayTime, easing = LinearOutSlowInEasing)
)
ArrowImage(
            modifier = Modifier
                .offset(arrowLocation.x.dp, arrowLocation.y.dp)
        )

0 0

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

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