代码之家  ›  专栏  ›  技术社区  ›  Muhammad Ahmed AbuTalib

在喷气背包组合中绘制集装箱外部边界

  •  0
  • Muhammad Ahmed AbuTalib  · 技术社区  · 2 年前

    我需要做一个圆角的容器,它有一个圆形,这样一半在容器外面,一半在里面。我用下面的代码实现了一个没有圆角的矩形 normal rectangle with no rounded corners

     Box(modifier = Modifier
        .fillMaxWidth()
        .height(100.dp)
        .padding(top = 20.dp)
        
        .background(ColorScheme.Quaternary.toColor().first)
        ) {
        Box(
            modifier = Modifier
                .layout { measurable, constraints ->
                    // Measure the child
                    val placeable = measurable.measure(constraints)
    
                    // Set the width and height of the layout as measured
                    layout(placeable.width, placeable.height) {
                        // Place the child such that half of it is outside the top boundary
                        placeable.place(0, -25.dp.roundToPx())  // Moves the circle up by 25dp
                    }
                }
                .size(50.dp)
                .clip(CircleShape)
                .background(Color.Red)
    
        )
    }
    

    当我试图绕过容器时,红色圆圈也会被夹住

    enter image description here

    这是我在修改器上调用clip的代码(注意,我也尝试过在修改器更改的不同位置调用clip,但效果是一样的)

    Box(modifier = Modifier
        .fillMaxWidth()
        .height(100.dp)
        .padding(top = 20.dp)
        .clip(RoundedCornerShape(10.dp))
        .background(ColorScheme.Quaternary.toColor().first)
        ) {
        Box(
            modifier = Modifier
                .layout { measurable, constraints ->
                    // Measure the child
                    val placeable = measurable.measure(constraints)
    
                    // Set the width and height of the layout as measured
                    layout(placeable.width, placeable.height) {
                        // Place the child such that half of it is outside the top boundary
                        placeable.place(0, -25.dp.roundToPx())  // Moves the circle up by 25dp
                    }
                }
                .size(50.dp)
                .clip(CircleShape)
                .background(Color.Red)
    
        )
    }
    

    有什么想法吗?

    1 回复  |  直到 2 年前
        1
  •  1
  •   Thracian    2 年前

    在Jetpack中,通过Modifier组合DrawScope内的任何组件或图形。drawWBehind/WithContent/W°thCache被剪裁,除非父调用 Modifier.clip() , Modifier.graphicsLayer{clip = true} 直接或在引擎盖下面。

    您可以删除 Modifier.clip(RoundedCornerShape(10.dp)) 从父组合和更改背景

    Modifier.background(ColorScheme.Quaternary.toColor().first, RoundedCornerShape(10.dp))
    

    你也可以Modifier.drawBehind在你的堆肥后面画一个圆形的矩形和圆形

    Box(modifier = Modifier
        .fillMaxWidth()
        .height(100.dp)
        .padding(top = 20.dp)
        .drawBehind {
            drawRoundRect(
                color = Color.Gray,
                cornerRadius = CornerRadius(10.dp.toPx(), 10.dp.toPx())
            )
            drawCircle(
                color = Color.Red,
                radius =  25.dp.toPx(),
                center = Offset(25.dp.toPx(), 0f)
            )
        }
    )