I have 2 composable elements with a transparent background (simple box with border and transparent background and text in it). They can be moved around the screen. But it is necessary that when one crosses the other, the one that is now moving should be clipped with the borders of the other. That's about the result I expect.
我试图使用clipPath和Region.Op找到可能的解决方案,但我找不到一种方法来获取绘制元素的路径
通过在First下面单击来折叠第二个元素的示例代码.
enum class State {
Collapsed,
Expanded
}
@Composable
fun Main() {
val cardSize = 100.dp
var currentState by remember { mutableStateOf(State.Collapsed) }
val transition = updateTransition(targetState = currentState, label = "expand_transition")
val animateDpAsState by transition.animateDp(
targetValueByState = { state ->
when (state) {
State.Expanded -> cardSize + 8.dp
State.Collapsed -> 8.dp
}
},
label = ""
)
Box(
modifier = Modifier
.width(cardSize + animateDpAsState)
.clickable {
currentState = if (currentState == State.Collapsed) {
State.Expanded
} else {
State.Collapsed
}
}
) {
Element(i = 1)
Element(
modifier = Modifier
.offset(x = animateDpAsState + 4.dp)
.scale(0.90f),
i = 2
)
}
}
@Composable
fun Element(modifier: Modifier = Modifier, i: Int) {
Box(
modifier = modifier
.size(100.dp)
.padding(8.dp)
.border(2.dp, Color.Black),
contentAlignment = Alignment.Center
) {
Text(text = "ITEM $i")
}
}