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

How to Achieve Translate Animation in Jetpack Compose?

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

jetpack compose 中转换动画的选项是 OFFSET ANIMATION 是的,我能够通过偏移量实现这一点 animation.I 我正在详细分享下面的代码和评论,这样 reader 更容易理解它

// 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))}
   }

使用偏移量也是我的解决方案。但是使用 animateDpAsState 代替。在 x 轴上移动了一个制表符指示器:

val offsetState = animateDpAsState(targetValue = targetPositionDp)
Box(modifier = Modifier
        .offset(offsetState.value, 0.dp)
        .background(color = Color.Red)
        .size(tabWidth, tabHeight))