微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Jetpack Compose 中勾勒文本

如何解决如何在 Jetpack Compose 中勾勒文本

我有一个 Jetpack Compose Text() 元素,我想用黑色勾勒出轮廓,例如

this


有人知道怎么做吗? 我试过使用 border() 修饰符,但这只是在包含文本的矩形区域周围添加一个边框。我也试过叠加两个文本元素,但这也不太奏效。

解决方法

您可以使用 CanvasdrawIntoCanvas 函数。

类似于:

Canvas(
    modifier = Modifier.fillMaxSize(),onDraw = {
                drawIntoCanvas {
                    it.nativeCanvas.drawText(
                        "Sample",0f,120.dp.toPx(),textPaintStroke
                    )
                    it.nativeCanvas.drawText(
                        "Sample",textPaint
                    )
                }
            }
)

使用这些Paint

val textPaintStroke = Paint().asFrameworkPaint().apply {
    isAntiAlias = true
    style = android.graphics.Paint.Style.STROKE
    textSize = 64f
    color = android.graphics.Color.BLACK
    strokeWidth = 12f
    strokeMiter= 10f
    strokeJoin = android.graphics.Paint.Join.ROUND
}

val textPaint = Paint().asFrameworkPaint().apply {
    isAntiAlias = true
    style = android.graphics.Paint.Style.FILL
    textSize = 64f
    color = android.graphics.Color.WHITE
}

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。