[英]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.