Android Jetpack 撰写 : how to zoom a image in a "box"?

标签 android android-jetpack-compose

我将在一个框内构建一个可缩放的 ImageView ,就像第一个屏幕截图一样。但是当我放大图像时,它会开箱即用。有没有办法缩放图像,但保持大小?没有 viewfragment , 仅 box似乎还不够。我期待图像变大,但仍然停留在红色框内,但放大后我得到了第二张截图。
Before zoom in
After zoom in
感谢 nglauber 和 Amirhosein,我得到了最终的解决方案,即在“框”(固定区域)内同时具有缩放和拖动功能,并将以下代码作为新屏幕截图,如下所示。

        val imageBitmap = imageResource(id = R.drawable.android)
        Image(
            modifier = Modifier
                .preferredSize(400.dp, 300.dp)
                .clip(RectangleShape)
                .zoomable(onZoomDelta = { scale.value *= it })
                .rawDragGestureFilter(
                    object : DragObserver {
                        override fun onDrag(dragDistance: Offset): Offset {
                            translate.value = translate.value.plus(dragDistance)
                            return super.onDrag(dragDistance)
                        }
                    })
                .graphicsLayer(
                    scaleX = scale.value,
                    scaleY = scale.value,
                    translationX = translate.value.x,
                    translationY = translate.value.y
                ),
            contentDescription = null,
            bitmap = imageBitmap
        )
enter image description here

最佳答案

这是我的解决方案...可能对某人有帮助...

@Composable
fun ZoomableImage() {
    val scale = remember { mutableStateOf(1f) }
    val rotationState = remember { mutableStateOf(1f) }
    Box(
        modifier = Modifier
            .clip(RectangleShape) // Clip the box content
            .fillMaxSize() // Give the size you want...
            .background(Color.Gray)
            .pointerInput(Unit) {
                detectTransformGestures { centroid, pan, zoom, rotation ->
                    scale.value *= zoom
                    rotationState.value += rotation
                }
            }
    ) {
        Image(
            modifier = Modifier
                .align(Alignment.Center) // keep the image centralized into the Box
                .graphicsLayer(
                    // adding some zoom limits (min 50%, max 200%)
                    scaleX = maxOf(.5f, minOf(3f, scale.value)),
                    scaleY = maxOf(.5f, minOf(3f, scale.value)),
                    rotationZ = rotationState.value
                ),
            contentDescription = null,
            painter = painterResource(R.drawable.dog)
        )
    }
}

关于Android Jetpack 撰写 : how to zoom a image in a "box"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66005066/

相关文章:

android - (Vibrating TextView)为自定义 View 配置属性

java - Android ndk jni 自动生成函数

android - 将 flutter Painter 转换为 compose

android - Jetpack compose 在形状上绘图

android - 无法解析 : play-services-flags

android - 如何显示来自 Ionic native 图像选择器的图像?

java - 通过 Bundle 在 Activity 之间保留值

android - 如何在 Jetpack compose 中处理 Espresso 空闲资源......?

android - Jetpack Compose 滚动 LazyColumn with CoroutineScope 结果错误 A MonotonicFrameClock 在此 CoroutineContext 中不可用

android - jetpack compose 是否使用 drawable-night 文件夹?