kotlin - 我如何在 Android Jetpack Compose 中的 Canvas 上绘制单侧加厚描边?

标签 kotlin android-jetpack-compose android-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
                )
            )
        }
    }
}

结果;

enter image description here

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

最佳答案

您必须考虑 Rect 的偏移量和宽度。

要在外面画画,你可以使用:

        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
            )
        )

绘制内部:

        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
            )
        )

enter image description here enter image description here

关于kotlin - 我如何在 Android Jetpack Compose 中的 Canvas 上绘制单侧加厚描边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73414363/

相关文章:

android - 编译我的 Room 数据库时出错

string - 在 kotlin 中,空对象的 toString() 方法返回空字符串而不是 "null"的最佳方法是什么

android - 如何将现有的全屏面板放入普通 Activity 的 SurfaceView 元素中

java - 如何找到相对文本大小

Android分页库LiveData<PagedList<T>>在api调用结束前触发

android - 如何将参数从 Flutter 传递给 Kotlin?

android - 如何在jetpack compose中通过lambda返回columnScope/RowScope

java - Canvas 中弧线的阴影

android - Jetpack Compose Vertical Grid 单项跨度大小

android - 为什么指示不适用于按钮或图标?