如何在 Android Jetpack Compose 中在画布上绘制单侧加厚描边?

Cin*_*com 3 android-canvas kotlin android-jetpack-compose android-jetpack-compose-canvas

这是我的代码;

Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ){
        Canvas(
            modifier = Modifier
                .fillMaxSize(0.5f)
                .border(1.dp, Color.Red)
        ){
            drawRect(
                color =Color.Black,
                size = size,
                style = Stroke(
                    width = 100f
                )
            )
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

结果;

在此输入图像描述

我希望厚度从红线的内侧或外侧增加,而不是两侧。我怎样才能做到这一点?

Gab*_*tti 5

您必须考虑矩形的偏移和宽度。

要在外面绘制,您可以使用:

        val width = 100f
        drawRect(
            topLeft = Offset(-width/2, -width/2),
            color =Color.Black,
            size = Size(size.width + width, size.height + width),
            style = Stroke(
                width = width
            )
        )
Run Code Online (Sandbox Code Playgroud)

在里面绘制:

        val width = 100f
        drawRect(
            topLeft = Offset(+ width/2, + width/2),
            color =Color.Black,
            size = Size(size.width - width, size.height - width),
            style = Stroke(
                width = width
            )
        )
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述 在此输入图像描述