简体   繁体   English

使用 Jetpack Compose 仅​​在给定边界内拖动 Composable

[英]Drag Composable only inside given boundries with Jetpack Compose

so I have a black box (rectangle) inside another box (boundary) and the rectangle is set as draggable所以我在另一个盒子(边界)内有一个黑盒子(矩形),矩形设置为可拖动

But now I can drag the rectangle around the whole window, but I want that if the rectangle "leaves" the boundary it should disappear behind it.但是现在我可以在整个窗口周围拖动矩形,但我希望如果矩形“离开”边界它应该消失在它后面。 Is there another modifier I can use for it?我可以使用其他修饰符吗?

Here a bit context:这里有一点上下文:

My code looks like this:我的代码如下所示:

MaterialTheme {
        Box(modifier = Modifier
            .size(500f.dp)
            .border(2f.dp, Color.Black, RectangleShape)
        ){
            Box(modifier = Modifier
                .offset { IntOffset(offsetX.roundToInt(), 0) }
                .draggable(
                    orientation = Orientation.Horizontal,
                    state = rememberDraggableState { delta ->
                        offsetX += delta
                    }
                )
                .background(Color.Blue)
                .size(100f.dp)
            ){
                Text("Hello")
            }
        }
    }

When I drag the rectangle outside the boundary it looks like this:当我将矩形拖到边界外时,它看起来像这样:

不良行为

But it should more like this:但它应该更像这样:

通缉行为

On the composable whose contents you want to clip add the clipToBounds() modifier.在要剪辑其内容的可组合组件上,添加clipToBounds()修饰符。

Box(modifier = Modifier
    .size(500f.dp)
    .border(2f.dp, Color.Black, RectangleShape)
    .clipToBounds()
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jetpack Compose 可在另一个可组合物中使用更大尺寸的可组合物 - Jetpack Compose place composable with bigger size inside another composable 变量不使用jetpack compose在可组合内部更新 - Variable doesn't update inside composable using jetpack compose 如何在 Android Jetpack Compose 的可组合物中启动协程 - how to launch a coroutine inside a composable in Android Jetpack Compose Jetpack Compose 不会在 setValue 上重新组合可组合 - Jetpack Compose not recomposing composable on setValue 在 Jetpack Compose 中按百分比偏移 Composable - Offset Composable by percentage in Jetpack Compose Jetpack Compose AlertDialog 错误:“@Composable 调用只能在@Composable 函数的上下文中发生” - Jetpack Compose AlertDialog Error: "@Composable invocations can only happen from the context of a @Composable function" Jetpack Compose Column 可组合对齐 - Jetpack Compose Column Composable Alignment 使用 Jetpack Compose 的深层链接导航到可组合项 - Navigating to a composable using a deeplink with Jetpack Compose Jetpack Compose:列表更改时更新可组合 - Jetpack Compose: Update composable when list changes Jetpack Compose:如何用 AndroidView 覆盖 Composable? - Jetpack Compose : How to overlay Composable with AndroidView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM