gpt4 book ai didi

android - 如何绘制内边框?

转载 作者:行者123 更新时间:2023-12-02 01:23:24 29 4
gpt4 key购买 nike

我想在列上绘制两个边框(1 个在里面 - 1 个在外面)。

预期结果:

Expected Result

Modifier.border(2.dp) // -> this creates inner border.

最佳答案

您有不同的选择。

  • 您可以简单地应用 border 修饰符,然后应用 padding

类似于:

    val shape = RoundedCornerShape(16.dp)

Box(modifier = Modifier
.size(60.dp , 100.dp)
.border(2.dp, Blue, shape)
.padding(4.dp)
.background(Blue, shape)
){
//content
}

enter image description here

  • 您可以两次应用 border 修饰符:

      val shape = RoundedCornerShape(16.dp)

    Box(modifier = Modifier
    .size(60.dp , 100.dp)
    .border(2.dp, Blue, shape)
    .border(4.dp, White, shape)
    .background(Blue, shape)
    ){
    //content
    }

enter image description here

  • 最后,您可以绘制一个内部边框。

类似于:

fun Modifier.innerBorder(
strokeWidth: Dp,
color: Color,
cornerRadiusDp: Dp,
offset : Offset = Offset.Zero
) = composed(
factory = {
val density = LocalDensity.current
val strokeWidthPx = density.run { strokeWidth.toPx() }
val cornerRadiusPx = density.run { cornerRadiusDp.toPx() }
val halfStroke = strokeWidthPx / 2
val topLeft = Offset(halfStroke + offset.x, halfStroke + offset.y)

Modifier.drawBehind {
val width = size.width - topLeft.x*2
val height = size.height - topLeft.y*2

drawRoundRect(
color = color,
topLeft = topLeft,
size = Size(width, height),
cornerRadius = CornerRadius(cornerRadiusPx, cornerRadiusPx).shrink(halfStroke),
style = Stroke(strokeWidthPx)
)

}
}
)

private fun CornerRadius.shrink(value: Float): CornerRadius = CornerRadius(
kotlin.math.max(0f, this.x - value),
kotlin.math.max(0f, this.y - value)
)

然后应用它:

   val shape = RoundedCornerShape(16.dp)

Column(modifier = Modifier
.size(60.dp , 100.dp)
.background( Blue , shape)
.innerBorder(
strokeWidth = 2.dp,
color = White,
cornerRadiusDp = 16.dp,
offset = Offset(4f,4f)
)
){
//....content
}

enter image description here

关于android - 如何绘制内边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75547963/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com