I am using a draggable content in which I want the image or the box in below code to stick within the boundaries of the box composable while dragging not move out to the parent Column composable as shown in the image below how to achieve it example image

  @Composable
 private fun 
  DraggableTextLowLevel() {
 
Column(Modifier.fillMaxSize()).   {
Text("Custom Edit Place")
Box(modifier = Modifier.hieght(200.dp).width(300.dp) {
    var offsetX by remember { mutableStateOf(0f) }
    var offsetY by remember { mutableStateOf(0f) }

    Box(
        Modifier
            .offset { IntOffset(offsetX.roundToInt(), offsetY.roundToInt()) }
            .background(Color.Blue)
            .size(50.dp)
            .pointerInput(Unit) {
                detectDragGestures { change, dragAmount ->
                    change.consume()
                    offsetX += dragAmount.x
                    offsetY += dragAmount.y
                }
            }
    )
}
  }
  }

推荐答案

要将可拖动内容保持在父布局边界内,可拖动内容本身的偏移量必须在布局边界内的范围内.

可以使用应用于父布局的onPlaced修改器来获取边界.此修改器提供布局放置后及其子布局之前的坐标.这允许子代根据父代的位置调整自己的位置.

// The size of the parent layout used later on to determine the
// offset of the draggable content inside the parent boundaries.

var parentSize by remember { mutableStateOf(Size.Zero) }

Box(
    modifier = Modifier
        .height(200.dp)
        .width(300.dp)
        .background(Color.LightGray)
        .onPlaced { layoutCoordinates ->

            // Set the [parentSize] to the size
            // of the layout in the local coordinates space.

            parentSize = layoutCoordinates.size.toSize()

        }
) ...

将可拖动内容保持在父布局边界内的一个示例是将offsetXoffsetY的值保持在相应边界内的范围内.

var draggableContentSize by remember { mutableStateOf(Size.Zero) }
var offsetX by remember { mutableFloatStateOf(0f) }
var offsetY by remember { mutableFloatStateOf(0f) }

Box(
    modifier = Modifier
        .onPlaced { draggableContentSize = it.size.toSize() }
        .offset { IntOffset(x = offsetX.roundToInt(), y = offsetY.roundToInt()) }
        .background(Color.Blue)
        .size(50.dp)
        .pointerInput(Unit) {
            detectDragGestures { change, dragAmount ->
                change.consume()

                // Keep the [offsetX] value inside the parent
                // width boundary by allowing it be in a range between
                // 0 and the parent width - draggableContentSize width.

                offsetX = (offsetX + dragAmount.x).coerceIn(
                    minimumValue = 0f,
                    maximumValue = parentSize.width - draggableContentSize.width
                )

                // Keep the [offsetY] value inside the parent
                // height boundary by allowing it be in a range between
                // 0 and the parent height - draggableContentSize height.

                offsetY = (offsetY + dragAmount.y).coerceIn(
                    minimumValue = 0f, 
                    maximumValue = parentSize.height - draggableContentSize.height
                )

            }
        }
)

以下是该示例的可视化演示.

A visual demonstration of the given example.

编码快乐!

Android相关问答推荐

如何自定义所选的NavigationBar项目?

如何正确增加LazyStream中的变量

替换- prop -中的值(adb shell getprop)

try 用Jetpack Compose理解视图模型和导航

如何在Jetpack Compose中实现前后动画?

如何显示具体的商品数量?

使用lazyColumn迁移paging3的旧代码

如何在jetpack compose中使可组合的屏幕zoom 到不同的手机(屏幕)尺寸?

如何在我的sqlite数据库中获取某个玩家的分数

为什么@PrimaryKey val id: Int? = null 在创建 Room 实体时有效吗?

每次在 Jetpack Compose 中调用导航

在 Google Play 中将用户从开放测试转移到生产的过程是怎样的?

使用 capacitor cordova 插件的 Android Studio 错误

无法解析依赖项'com.github.smarteist:autoimageslider:1.4.0-appcompat'

使用 Jetpack Compose 时,如何以简单的方式在 Color.kt 中定义 colored颜色 ?

在 Kotlin 客户端应用程序中发送 FCM 推送通知 - Firebase 云消息传递

Kotlin Compose forEach 中的负间距

MVVM - 这个逻辑的最佳层是什么?

升级到 android studio 花栗鼠后,应用程序未安装在模拟器中

如何将私有 mutableStateOf 分配给 Android Jetpack 中的 State 变量?