android - 如何在Jetpack Compose中实现翻译动画?

我正在尝试在 Jetpack compose 中实现翻译动画,但我无法为此找到合适的来源。任何人都可以帮助我在 Jetpack compose 中实现翻译动画,我可以在其中手动设置开始和编辑位置..

最佳答案

在 jetpack compose 中转换动画的选项是 OFFSET ANIMATION 是的,我能够通过偏移动画实现这一点。我将在下面分享代码并详细注释,以便读者更容易理解它

// Courtine Scope to Run the animation in thread
val coroutineScope = rememberCoroutineScope()
val offsetX = remember { Animatable(0f) }
val offsetY = remember { Animatable(0f) }

 Image(
            painter = rememberDrawablePainter(
                ContextCompat.getDrawable(
                    context,
                    R.drawable.image
                )
            ),
            contentDescription = "s", contentScale = ContentScale.Crop,
            modifier = Modifier
                .offset {
                    IntOffset(
                        offsetX.value.toInt(),
                        offsetY.value.toInt()
                    )
                }
                .width(300.dp)
                .height(300.dp)
        )
//Finally run the animation on the Click of your button or whenever you wants to start it...

  coroutineScope.launch {

       launch {
                    offsetXFirst.animateTo(
                    targetValue = targetValue,
                    animationSpec = tween(
                    durationMillis = 2000,
                    delayMillis = 0))}

                launch {
                offsetYFirst.animateTo(
                targetValue = size.height.toFloat(),
                animationSpec = tween(
                durationMillis = 2000,
                delayMillis = 0))}
   }

https://stackoverflow.com/questions/70076735/

相关文章:

c++ - `shared_ptr::use_count() == 0` 和 `shared_ptr

function - 关于在函数和宏定义中使用结构文字

c# - 如何从 C# 中使用具有另一个函数作为参数的 Rust 函数?

c++ - 从字节初始化一个由 trivially_copyable 但不是 default_con

ruby-on-rails - 如何一次为 Ruby 中的对象分配多个属性

javascript - 左关联二叉树折叠

ios - 如何获取 UTType 图像、音频和视频的所有扩展

java - 在java中对字符串进行排序时如何忽略空格?

apache-kafka - 如何触发Kafka再平衡?

r - 在 dplyr 中跨列过滤