Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在Jetpack Compose中的可拖动项上重置偏移动画_Android_Kotlin_Animation_Drag_Android Jetpack Compose - Fatal编程技术网

Android 在Jetpack Compose中的可拖动项上重置偏移动画

Android 在Jetpack Compose中的可拖动项上重置偏移动画,android,kotlin,animation,drag,android-jetpack-compose,Android,Kotlin,Animation,Drag,Android Jetpack Compose,我有一个可以垂直拖动的绿色正方形。但每当我停止拖动它时,我希望它用动画将偏移重置为开始。我这样试过,但我想不出来。有人知道怎么做吗 @Composable fun DraggableSquare() { var currentOffset by remember { mutableStateOf(0F) } val resetAnimation by animateIntOffsetAsState(targetValue = IntOffset(0, currentOffset.

我有一个可以垂直拖动的绿色正方形。但每当我停止拖动它时,我希望它用动画将偏移重置为开始。我这样试过,但我想不出来。有人知道怎么做吗

@Composable
fun DraggableSquare() {
    var currentOffset by remember { mutableStateOf(0F) }
    val resetAnimation by animateIntOffsetAsState(targetValue = IntOffset(0, currentOffset.roundToInt()))

    var shouldReset = false

    Box(contentAlignment = Alignment.TopCenter, modifier = Modifier.fillMaxSize()) {
        Surface(
            color = Color(0xFF34AB52),
            modifier = Modifier
                .size(100.dp)
                .offset {
                    when {
                        shouldReset -> resetAnimation
                        else -> IntOffset(0, currentOffset.roundToInt())
                    }
                }
                .draggable(
                    state = rememberDraggableState { delta -> currentOffset += delta },
                    orientation = Orientation.Vertical,
                    onDragStopped = {
                        shouldReset = true
                        currentOffset = 0F
                    }
                )
        ) {}
    }
}

您可以将偏移量定义为。
拖动时,使用方法
snapTo
将当前值更新为初始值,并使用
ondragstop
启动动画

val coroutineScope = rememberCoroutineScope()
val offsetY  =  remember { Animatable(0f) }

Box(contentAlignment = Alignment.TopCenter, modifier = Modifier.fillMaxSize()) {
    Surface(
        color = Color(0xFF34AB52),
        modifier = Modifier
            .size(100.dp)
            .offset {
                IntOffset(0, offsetY.value.roundToInt())
            }
            .draggable(
                state = rememberDraggableState { delta ->
                    coroutineScope.launch {
                        offsetY.snapTo(offsetY.value + delta)
                    }
                },
                orientation = Orientation.Vertical,
                onDragStopped = {
                    coroutineScope.launch {
                        offsetY.animateTo(
                            targetValue = 0f,
                            animationSpec = tween(
                                durationMillis = 3000,
                                delayMillis = 0
                            )
                        )
                    }
                }
            )
    ) {
    }
}

很好,这修复了重置部分,但我也希望重置过程中动画平滑。有可能吗?@DonnyRozendal我已经更新了答案,添加了动画。先生,您是一位真正的英雄,谢谢!非常清晰易读。我的另一个解决方案涉及创建竞赛条件的多个状态,因此将所有内容放在一个可设置动画的状态中是可行的。我也有这个想法,但不想设置拖动的动画。但是我看到你用
snapTo
解决了这个问题,这是天才。而且,在
snapTo
函数中,应该是offsetY而不是offsetX。但我不能要求编辑,因为字符不够。@DonnyRozendal感谢您的反馈。我已经更新了答案。