在Android画布上封装长文字 [英] Wrapping long text on an Android Canvas
问题描述
我有一个自定义控件,可以直接在画布
上进行大量2D绘图。
I have a custom control that is doing a lot of 2D drawing straight to the canvas
.
Some of this drawing is text, so I am using the Canvas.drawText()
method.
我想在一些边界内绘制文本 - 左上,一定的最大宽度和最大行数。在绘制文本后,我想知道它需要多少行。
I want to draw the text within some bounds - a top-left, certain maximum width, and a maximum number of lines. After drawing the text, I want to know how many lines it took.
是否有一个内置函数在边界内绘制文本,明智地分割?
Is there a built-in function to draw text within bounds doing the splitting sensibly?
如果没有,是否有这样做的标准食谱?
If not, is there a standard recipe for doing so?
推荐答案
可以使用 android.text.StaticLayout
class for this;只需为所需的文本,对齐方式,宽度等创建 StaticLayout
,并调用其 draw(Canvas)
方法绘制到画布。
You can use the android.text.StaticLayout
class for this; simply create a StaticLayout
for the desired text, alignment, width, etc. and call its draw(Canvas)
method to draw to the canvas.
这篇关于在Android画布上封装长文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!