这不是使用Modifier.graphicsLayer{}
旋转或平移图像的重复。我想在屏幕上移动、旋转和缩放一个Composable,而不是在固定的位置。
我想达到的是像这幅图中那样
的数据Modifier.graphicsLayer{}
和Modifier.pointerInput()
的顺序很重要。如果Modifier.pointerInput()
放置在Modifier.graphicsLayer{}
Composable的触控区域缩放、平移和旋转之后。
@Composable
private fun Test() {
var offsetLeft by remember { mutableStateOf(Offset.Zero) }
var offsetRight by remember { mutableStateOf(Offset.Zero) }
var rotationLeft by remember { mutableStateOf(0f) }
var rotationRight by remember { mutableStateOf(0f) }
val modifierLeft = Modifier
.border(3.dp, Color.Red)
.graphicsLayer {
translationX = offsetLeft.x
translationY = offsetLeft.y
rotationZ = rotationLeft
}
.border(2.dp, Color.Green)
.pointerInput(Unit) {
detectTransformGestures { centroid, pan, zoom, rotation ->
offsetLeft += pan
rotationLeft += rotation
}
}
val modifierRight = Modifier
.border(3.dp, Color.Red)
.pointerInput(Unit) {
detectTransformGestures { centroid, pan, zoom, rotation ->
offsetRight += pan
rotationRight += rotation
}
}
.border(2.dp, Color.Green)
.graphicsLayer {
translationX = offsetRight.x
translationY = offsetRight.y
rotationZ = rotationRight
}
Row(modifier = Modifier.fillMaxSize()) {
Image(
painter = painterResource(id = R.drawable.landscape1),
contentDescription = "",
modifierLeft.aspectRatio(1f).weight(1f)
)
Image(
painter = painterResource(id = R.drawable.landscape1),
contentDescription = "",
modifierRight.aspectRatio(1f).weight(1f)
)
}
}
字符串
我希望能够移动,旋转和一个像gif中的左图像从其当前的转换组合。“
的
到目前为止,我所做的是,通过旋转矩阵来旋转平移
/**
* Rotates the given offset around the origin by the given angle in degrees.
*
* A positive angle indicates a counterclockwise rotation around the right-handed 2D Cartesian
* coordinate system.
*
* See: [Rotation matrix](https://en.wikipedia.org/wiki/Rotation_matrix)
*/
fun Offset.rotateBy(
angle: Float
): Offset {
val angleInRadians = ROTATION_CONST * angle
val newX = x * cos(angleInRadians) - y * sin(angleInRadians)
val newY = x * sin(angleInRadians) + y * cos(angleInRadians)
return Offset(newX, newY)
}
internal const val ROTATION_CONST = (Math.PI / 180f).toFloat()
型
它适用于平移,平移工作正常,但随着从原始位置的平移增加,旋转和缩放似乎不工作。我不能正确地平移位置。
@Composable
private fun MyComposable() {
var offset by remember { mutableStateOf(Offset.Zero) }
var zoom by remember { mutableStateOf(1f) }
var rotation by remember { mutableStateOf(0f) }
var text by remember { mutableStateOf("") }
var centroid by remember { mutableStateOf(Offset.Zero) }
val modifier = Modifier
.border(3.dp, Color.Green)
.fillMaxWidth()
.aspectRatio(4 / 3f)
.graphicsLayer {
this.translationX = offset.x * zoom
this.translationY = offset.y * zoom
this.scaleX = zoom
this.scaleY = zoom
this.rotationZ = rotation
// TransformOrigin(0f, 0f)
}
.pointerInput(Unit) {
detectTransformGestures(
onGesture = { gestureCentroid, gesturePan, gestureZoom, gestureRotate ->
rotation += gestureRotate
zoom *= gestureZoom
offset += gesturePan.rotateBy(rotation)
centroid = gestureCentroid
}
)
}
.drawWithContent {
drawContent()
drawCircle(color = Color.Red, center = centroid, radius = 20f)
}
Image(
modifier = modifier,
painter = painterResource(id = R.drawable.landscape1),
contentDescription = null,
contentScale = ContentScale.FillBounds
)
}
型
的
我尝试更改TransformOrigin()。同时更新了适用于固定触摸位置的示例代码,这是developer.android页面上可用代码的修改版本
@Composable
private fun MyComposable3() {
var zoom by remember { mutableStateOf(1f) }
var offset by remember { mutableStateOf(Offset.Zero) }
var centroid by remember { mutableStateOf(Offset.Zero) }
var centroidOld by remember { mutableStateOf(Offset.Zero) }
var centroidNew by remember { mutableStateOf(Offset.Zero) }
var angle by remember { mutableStateOf(0f) }
val imageModifier = Modifier
.border(3.dp, Color.Green)
.graphicsLayer {
translationX = -offset.x * zoom
translationY = -offset.y * zoom
scaleX = zoom
scaleY = zoom
rotationZ = angle
TransformOrigin(0f, 0f).also { transformOrigin = it }
}
.pointerInput(Unit) {
detectTransformGestures(
onGesture = { gestureCentroid, gesturePan, gestureZoom, gestureRotate ->
val oldScale = zoom
val newScale = (zoom * gestureZoom).coerceIn(0.5f..5f)
angle += gestureRotate
// For natural zooming and rotating, the centroid of the gesture should
// be the fixed point where zooming and rotating occurs.
// We compute where the centroid was (in the pre-transformed coordinate
// space),
// and then compute where it will be after this delta.
// We then compute what the new offset should be to keep the centroid
// visually stationary for rotating and zooming, and also apply the pan.
centroidOld = (offset + gestureCentroid / oldScale).rotateBy(gestureRotate)
centroidNew =
(gestureCentroid / newScale + gesturePan.rotateBy(angle) / oldScale)
offset = centroidOld - centroidNew
zoom = newScale
centroid = gestureCentroid
}
)
}
.border(3.dp, Color.Red)
.drawWithContent {
drawContent()
drawCircle(color = Color.Red, center = centroid, radius = 20f)
drawCircle(color = Color.Green, center = centroidOld, radius = 20f)
drawCircle(color = Color.Blue, center = centroidNew, radius = 20f)
}
Image(
modifier = imageModifier
.fillMaxWidth()
.aspectRatio(4 / 3f),
painter = painterResource(id = R.drawable.landscape2),
contentDescription = null,
contentScale = ContentScale.FillBounds
)
}
型
这个也不行。
1条答案
按热度按时间laawzig21#
使用graphicLayer修改器修改可组合布局是不会成功的,文档说:
Modifier.graphicsLayer不会更改组合对象的测量大小或放置,因为它只影响绘制阶段。这意味着如果组合对象最终在其布局边界之外绘制,则可能会与其他组合对象重叠。
尝试使用布局修改器:
字符串
你可以找到更多的信息here。