如何在jetpack compose中的drawLine中制作圆角

Viv*_*odi 2 android kotlin android-jetpack-compose android-jetpack-compose-canvas

我正在使用 Canvas Api 来画一些东西。我想画圆角线。画线没有任何问题。但我无法弄清楚圆角半径的属性。

val boxSize = 30.dp

Box(modifier = Modifier
            .background(Color.LightGray)
            .height(height = boxSize)
    ) {
        Canvas(
            modifier = Modifier
                .fillMaxWidth()
        ) {
            val canvasWidth = size.width
            drawLine(
                start = Offset(x = 0f, y = (boxSize / 2).toPx()),
                end = Offset(x = canvasWidth, y = (boxSize / 2).toPx()),
                color = Color.Black,
                strokeWidth = 8.dp.toPx()
            )

        }
    }
Run Code Online (Sandbox Code Playgroud)

我的观点很简单,没有圆角半径。

在此输入图像描述

我希望我的黑线是具有特定半径的每一侧的角。

Raf*_*ani 6

您需要添加cap参数drawLine并将其设置为StrokeCap.Round

 drawLine(
     start = Offset(x = 0f, y = (boxSize / 2).toPx()),
     end = Offset(x = canvasWidth, y = (boxSize / 2).toPx()),
     color = Color.Black,
     strokeWidth = 8.dp.toPx(),
     cap = StrokeCap.Round, //add this line for rounded edges
)
Run Code Online (Sandbox Code Playgroud)