【问题标题】:How can I draw text on a path in Jetpack Compose?如何在 Jetpack Compose 的路径上绘制文本?
【发布时间】:2020-11-06 15:56:17
【问题描述】:

现在有没有办法使用 jetpack compose 在自定义路径上写入文本?

这是我想要实现的示例图像:

【问题讨论】:

    标签: android android-jetpack-compose


    【解决方案1】:

    我们使用 nativeCanvas 在 Compose 中使用 Path 绘制文本,就像我们在自定义视图中通常所做的那样。

    例如:

    @Composable
    fun ArcTextExample() {
        val paint = Paint().asFrameworkPaint()
        Canvas(modifier = Modifier.fillMaxSize()) {
            paint.apply {
                isAntiAlias = true
                textSize = 24f
                typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
            }
    
            drawIntoCanvas {
                val path = Path()
                path.addArc(RectF(0f, 100f, 200f, 300f), 270f, 180f)
                it.nativeCanvas.drawTextOnPath("Hello World Example", path, 0f, 0f, paint)
            }
        }
    }
    

    注意: 我们应该使用android.graphics.Path

    结果会是这样的:

    【讨论】:

    • 感谢您的解决方案!这绝对对我有很大帮助。我会对一个没有android.graphics 的解决方案感兴趣,只需要jetpack compose。你知道这是否可能与仅使用 jetpack compose 相关吗?
    • @fraherm,据我了解,DrawScope 中没有绘制文本的方法。
    猜你喜欢
    • 2022-01-10
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多