android - 如何在 Jetpack Compose 中勾勒文本

标签 android android-jetpack-compose android-jetpack-compose-text

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

最佳答案

您可以使用 Canvas drawIntoCanvas 功能。
就像是:

Canvas(
    modifier = Modifier.fillMaxSize(),
    onDraw = {
                drawIntoCanvas {
                    it.nativeCanvas.drawText(
                        "Sample",
                        0f,
                        120.dp.toPx(),
                        textPaintStroke
                    )
                    it.nativeCanvas.drawText(
                        "Sample",
                        0f,
                        120.dp.toPx(),
                        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

关于android - 如何在 Jetpack Compose 中勾勒文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66958260/

相关文章:

android - 如何自定义 IconButton 的颜色

android - 如何更新项目中的 Jetpack Compose 版本?

android - Jetpack Compose 相当于 InputFilter?

java - 如何在回收站 View 的单选组中显示多个选项

android - 调用 procrank 在真实设备上不起作用

android - 是否可以同时将文件添加到多个 git 仓库?

android - Jetpack Compose 中可调整大小的 BasicTextField

android - 更新到 Android Studio 北极狐后,我无法使用任何自定义主题

android-jetpack-compose - 禁用 BottomNavigationItem 的涟漪效应

android - Jetpack compose - 在文本后面绘制背景