以下是我的代码;

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

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

推荐答案

你必须考虑矩形的偏移量和宽度.

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

        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相关问答推荐

使用数据存储首选项Kotlin Jetpack Compose

Kotlin编译器如何决定是否可以在任何给定点调用Suspend方法?

哪个更好? Integer.valueOf(readLine()) 或 readLine()!!.toInt()

扩展属性委托给实例属性

使用 Kotlin 的 Springboot 中缺少 ResponseEntity 正文属性

如何将 `when` 与 2 个密封类一起使用并获取内部值?

Kotlin 函数中接收者和参数的类型相同

Kotlin Path.useLines { } - 如何不获取 IOException("Stream closed")?

为什么这个 Kotlin 代码不起作用? (如果 str[index] 在列表中,则打印)

如何在 Hibernate Panache 中进行部分搜索

Kotlin RxJava 可空的错误

模拟异常 - 没有找到答案

Kotlin-Java 互操作不能与可变参数一起使用

将多个 Kotlin 流合并到一个列表中,而无需等待第一个值

Jetpack Compose State:修改类属性

在调用Kotlin数据类中的超类构造函数之前访问函数

Android EditText 协程go 抖操作符,如 RxJava

Kotlin中的嵌套let块

如何修复未解析的参考生命周期范围?

为什么在 Kotlin 中return可以返回一个return?