我怎样才能在android compose kotlin中进行一键拖动和多点触摸拖动
我想移动 Box 一键拖动和多点触摸拖动,但是,它只能移动多点触摸。 我如何使用一个偏移变量来使用它们
@Composable
fun TransformableSample() {
// set up all transformation states
var scale by remember { mutableStateOf(1f) }
var rotation by remember { mutableStateOf(0f) }
var offset by remember { mutableStateOf(Offset.Zero) }
val state = rememberTransformableState { zoomChange, offsetChange, rotationChange ->
scale *= zoomChange
rotation += rotationChange
offset += offsetChange
}
Box(
Modifier
// apply other transformations like rotation and zoom
// on the pizza slice emoji
.graphicsLayer(
scaleX = scale,
scaleY = scale,
rotationZ = rotation,
translationX = offset.x,
translationY = offset.y
)
// add transformable to listen to multitouch transformation events
// after offset
.transformable(state = state)
.background(Color.Blue)
.fillMaxSize()
)
}
I want to move Box one touch drag and multi touch drag, however, it only moves multitouch.
How can I use both of them using one offset variable
@Composable
fun TransformableSample() {
// set up all transformation states
var scale by remember { mutableStateOf(1f) }
var rotation by remember { mutableStateOf(0f) }
var offset by remember { mutableStateOf(Offset.Zero) }
val state = rememberTransformableState { zoomChange, offsetChange, rotationChange ->
scale *= zoomChange
rotation += rotationChange
offset += offsetChange
}
Box(
Modifier
// apply other transformations like rotation and zoom
// on the pizza slice emoji
.graphicsLayer(
scaleX = scale,
scaleY = scale,
rotationZ = rotation,
translationX = offset.x,
translationY = offset.y
)
// add transformable to listen to multitouch transformation events
// after offset
.transformable(state = state)
.background(Color.Blue)
.fillMaxSize()
)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要使用 Modifier.pointerInput() 与Modifier.graphicsLayer()。对您来说重要的是,这将允许您用一根手指或两根手指拖动图像。它仍然会使用记住状态(尽管我通常保存这些值以及 ViewModel 中的所有数学)。这是从文档中复制的代码。
You're going to want to use a Modifier.pointerInput() combined with Modifier.graphicsLayer(). The important thing for you is that this will let you drag your image around with one finger or with two fingers. It'll still use the remember states (although I usually hold these values as well as all the math in my ViewModel). Here's the code copied from the documentation.