多点触控:平移、缩放、旋转
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如需检测用于平移、缩放和旋转的多点触控手势,您可以使用 transformable
修饰符。此修饰符本身不会转换元素,只会检测手势。
@Composable
private 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()
)
}
如果您需要将缩放、平移和旋转与其他手势结合使用,可以使用 PointerInputScope.detectTransformGestures
检测器。
为您推荐
- 注意:当 JavaScript 处于关闭状态时,系统会显示链接文字
- 了解手势
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-23。
[null,null,["最后更新时间 (UTC):2025-08-23。"],[],[],null,["# Multitouch: Panning, zooming, rotating\n\nTo detect multitouch gestures used for panning, zooming and rotating, you can\nuse the `transformable` modifier. This modifier does not transform elements by\nitself, it only detects the gestures.\n\n\n```kotlin\n@Composable\nprivate fun TransformableSample() {\n // set up all transformation states\n var scale by remember { mutableStateOf(1f) }\n var rotation by remember { mutableStateOf(0f) }\n var offset by remember { mutableStateOf(Offset.Zero) }\n val state = rememberTransformableState { zoomChange, offsetChange, rotationChange -\u003e\n scale *= zoomChange\n rotation += rotationChange\n offset += offsetChange\n }\n Box(\n Modifier\n // apply other transformations like rotation and zoom\n // on the pizza slice emoji\n .graphicsLayer(\n scaleX = scale,\n scaleY = scale,\n rotationZ = rotation,\n translationX = offset.x,\n translationY = offset.y\n )\n // add transformable to listen to multitouch transformation events\n // after offset\n .transformable(state = state)\n .background(Color.Blue)\n .fillMaxSize()\n )\n}https://github.com/android/snippets/blob/dd30aee903e8c247786c064faab1a9ca8d10b46e/compose/snippets/src/main/java/com/example/compose/snippets/touchinput/gestures/GesturesSnippets.kt#L324-L352\n```\n\n\u003cbr /\u003e\n\nIf you need to combine zooming, panning and rotation with other gestures, you\ncan use the\n[`PointerInputScope.detectTransformGestures`](/reference/kotlin/androidx/compose/foundation/gestures/package-summary#(androidx.compose.ui.input.pointer.PointerInputScope).detectTransformGestures(kotlin.Boolean,kotlin.Function4))\ndetector.\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Understand gestures](/develop/ui/compose/touch-input/pointer-input/understand-gestures)"]]