如何在 Jetpack Compose 中勾勒文本

ich*_*en2 5 android android-jetpack-compose android-jetpack-compose-text

我有一个 Jetpack Compose Text() 元素,我想像这样用黑色勾勒出轮廓 这个.
有人知道怎么做吗?我试过使用 border() 修饰符,但这只是在包含文本的矩形区域周围添加了一个边框。我也试过覆盖两个文本元素,但这也不太奏效。

Gab*_*tti 6

您可以使用 aCanvasdrawIntoCanvas函数。

就像是:

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

与这些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
}
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 除了在彼此上绘制两条相同的线条之外,还有其他选项可以返回填充颜色吗?例如,这不适用于大胆的风格和类似的情况。谢谢。 (2认同)