【问题标题】:Android - Draw Text At The Bottom Of A Bitmap Using Static LayoutAndroid - 使用静态布局在位图底部绘制文本
【发布时间】:2018-08-25 04:13:16
【问题描述】:

我有以下代码将一些文本放在图像的顶部:

    val paint = TextPaint(Paint.ANTI_ALIAS_FLAG)
    paint.color = Color.WHITE
    paint.textSize = 40f //* context.resources.displayMetrics.density
    paint.typeface = Typeface.DEFAULT_BOLD
    //paint.textAlign = Paint.Align.
    paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY)

    val teststr = "Hello World Hello World Hello World Hello World"

    val canvas = Canvas(bitmap)
    val textLayout = StaticLayout(teststr, paint, canvas.width ,Layout.Alignment.ALIGN_CENTER, 1f, 0f, false)
    textLayout.draw(canvas)

但是,实际上,我希望文本位于图像的底部。如何将StaticLayoutBitmap 的底部对齐,使文本出现在图像的底部。

【问题讨论】:

  • Canvas#translate

标签: android text bitmap android-bitmap staticlayout


【解决方案1】:

事实证明这是正确的答案。从画布的高度减去StaticLayout 的高度。

    val paint = TextPaint(Paint.ANTI_ALIAS_FLAG)
    paint.color = Color.WHITE
    paint.textSize = 40f //* context.resources.displayMetrics.density
    paint.typeface = Typeface.DEFAULT_BOLD
    //paint.textAlign = Paint.Align.
    paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY)

    val teststr = "Hello World"

    val canvas = Canvas(bitmap)

    val textLayout = StaticLayout(teststr, paint, canvas.width ,Layout.Alignment.ALIGN_CENTER, 1f, 0f, false)
    canvas.save()
    canvas.translate(0f,canvas.height - textLayout.height - 0.0f)
    textLayout.draw(canvas)
    canvas.restore()

【讨论】:

  • 嗨!,这是一个很好的答案,但只是面临图像和文本之间的边距/填充问题。如何在不剪辑的情况下在图像和文本之间留出一些空间,提前致谢。
猜你喜欢
  • 1970-01-01
  • 2020-09-19
  • 2018-09-12
  • 1970-01-01
  • 1970-01-01
  • 2021-08-03
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多