简体   繁体   English

如何在 Jetpack Compose 中为截止区域的圆角添加圆弧?

[英]How to add arc for rounded corner for cut off area in jetpack compose?

I wanted the rounded curve around the corner of triangle in left and right.我想要左右三角形角周围的圆角曲线。 I tried to add arc but I don't know it's not working maybe the coordinate are wrong.我尝试添加圆弧,但我不知道它不起作用,也许坐标是错误的。 Or any other methods?或者还有其他方法吗?


@Composable
fun NavBarCustomShape() {
    Canvas(modifier = Modifier.fillMaxWidth().height(64.dp)){

        val rect = Rect(Offset.Zero, size)
        val trianglePath = Path().apply {
            // Moves to top center position
            moveTo(x = rect.topCenter.x, y = 27.dp.toPx())
            // Add line to bottom right corner
            lineTo(x = rect.bottomCenter.x - 37.dp.toPx(), y = rect.bottomLeft.y )
            // Add line to bottom left corner
            lineTo(x = rect.bottomCenter.x + 37.dp.toPx(), y = rect.bottomRight.y)

//            moveTo(x = rect.bottomCenter.x - 32.dp.toPx(), y = rect.bottomLeft.y )
//            addArc(
//                Rect(
//                    offset = Offset(rect.bottomCenter.x - 32.dp.toPx(),rect.bottomRight.y),
//                    size = Size(24.dp.toPx(),24.dp.toPx())
//                ),
//                startAngleDegrees = 0f,
//                sweepAngleDegrees = 90f,
//            )
            close()
        }


        rotate(180f)
        {
            clipPath(trianglePath, clipOp = ClipOp.Difference)
            {
                drawRect(color = purple)
            }
        }
    }
}

Result shape:-结果形状:-

输出

Required shape is:-所需的形状是:-

在此处输入图像描述

Here you go. When drawing arc the thing to consider is diameter or length of the rectangle it's in. Since you draw an arc with quarter size of this rectangle.给你 go。绘制圆弧时要考虑的是它所在矩形的直径或长度。因为你绘制的圆弧的尺寸是该矩形的四分之一。 Sweep angle is 90 degrees and consider that arc drawing 0 degrees starts from 3 o'clock and moves clockwise.扫角为90度,考虑从3点开始画0度弧,顺时针移动。

This will draw arcs with 16.dp radius, you can change this accordingly.这将绘制半径为 16.dp 的圆弧,您可以相应地更改它。

@Composable
fun ArcSample() {

    val path = Path()
    Canvas(modifier = Modifier
        .fillMaxWidth()
        .height(64.dp),
        onDraw = {
            val canvasWidth = size.width

            val arcDiameter = 32.dp.toPx()
            val shapeHeight = 27.dp.toPx()

            val horizontalCenter = canvasWidth / 2

            path.apply {
                lineTo(x = horizontalCenter - arcDiameter, y = 0f)

                // Left arc
                arcTo(
                    rect = Rect(
                        left = horizontalCenter - arcDiameter,
                        top = 0f,
                        right = horizontalCenter,
                        bottom = arcDiameter
                    ),
                    startAngleDegrees = -90.0f,
                    sweepAngleDegrees = 90.0f,
                    forceMoveTo = false
                )

                // Right arc
                arcTo(
                    rect = Rect(
                        left = horizontalCenter,
                        top = 0f,
                        right = horizontalCenter + arcDiameter,
                        bottom = arcDiameter
                    ),
                    startAngleDegrees = 180.0f,
                    sweepAngleDegrees = 90.0f,
                    forceMoveTo = false
                )

                lineTo(x = canvasWidth, y = 0f)
                lineTo(x = canvasWidth, y = shapeHeight)
                lineTo(x = 0f, y = shapeHeight)

            }

            drawPath(path, Color.Red)

        }
    )
}

Result结果

You can check here for drawing arcs.您可以在此处检查绘制弧线。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM