Daniel Candeias
07/12/2021, 4:07 PMdetectTapGestures
how can i disallow user to pan/translate the content out of its bounding box?
I have scale working with translations as well but the user is able to translate the image out of the screen. How can I set some limits for translation?Daniel Candeias
07/12/2021, 4:08 PM@Composable
fun ZoomableImage(
modifier: Modifier = Modifier,
imageUrl: String
) {
Box(
modifier = modifier
.fillMaxSize()
.background(Color.Red)
) {
val scale = remember { mutableStateOf(1f) }
val translationX = remember { mutableStateOf(1f) }
val translationY = remember { mutableStateOf(1f) }
val painter =
rememberCoilPainter(imageLoader = LocalContext.current.imageLoader, request = imageUrl)
Box(
modifier = Modifier
.clip(RectangleShape)
.fillMaxSize()
.background(Color.Yellow)
.pointerInput(Unit) {
detectTransformGestures(true) { centroid, pan, zoom, rotation ->
scale.value *= zoom
translationX.value += pan.x
translationY.value += pan.y
}
}
.graphicsLayer(
// adding some zoom limits (min 100%, max 250%)
scaleX = maxOf(1f, minOf(2.5f, scale.value)),
scaleY = maxOf(1f, minOf(2.5f, scale.value)),
translationX = translationX.value,
translationY = translationY.value
),
contentAlignment = Alignment.Center
) {
Image(
painter = painter,
contentDescription = null,
modifier = Modifier
.aspectRatio(16 / 9f)
.align(Alignment.Center),
contentScale = ContentScale.Crop
)
}
}
}
Ravi
09/03/2021, 1:12 PMTornike Kikalishvili
09/17/2021, 10:42 AMTornike Kikalishvili
09/17/2021, 10:43 AMmodifier = Modifier
.layoutId("image")
.scale(maxOf(1f, scale))
.absoluteOffset {
IntOffset(offsetX.roundToInt(), offsetY.roundToInt())
}
.pointerInput(Unit) {
detectTransformGestures(
onGesture = { _, pan, gestureZoom, _ ->
scale *= gestureZoom
offsetX += pan.x
offsetY += pan.y
}
)
}
.fillMaxSize()
.border(2.dp, Color.Red, RectangleShape),
julioromano
06/21/2022, 12:23 PM