I hope to drag using single touch and multi touch. And I also hope to my image rotate and zoom in zoom out. I write this code, however, it didn't follow exactly my finger touch and verbose. Most over, it throws null point error sometimes. How can I modify my code to solve this issue.
simplify code. prevent null point error. follow my finger naturally.
Thanks for reading.
@Composable
fun DraggableView(
) {
// 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 = Modifier
.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)
.fillMaxSize()
) {
var offsetX by remember { mutableStateOf(0f) }
var offsetY by remember { mutableStateOf(0f) }
// val coroutineScope = rememberCoroutineScope()
Box(
Modifier
.offset { IntOffset((offsetX * scale).roundToInt(), (offsetY * scale).roundToInt()) }
.background( Color.Blue)
.size(300.dp)
.pointerInput(Unit) {
detectDragGestures { change, dragAmount ->
change.consumeAllChanges()
offsetX += dragAmount.x //* scale
offsetY += dragAmount.y //* scale
}
}
) {
val image = loadPicture().value
image?.let { img ->
Image(
bitmap = img.asImageBitmap(),
contentDescription = "content",
modifier = Modifier
.fillMaxWidth(),
contentScale = ContentScale.Crop
)
}
}
}
}
Image zoom, pan and rotation can be done way simpler and robust way using detectTransformGestures
By using this sample code from official page you will have natural zoom which will be invoked from centroid(center of pointers) instead of center of screen
Rotate function
And apply this modifier to an image to zoom, pan and rotate