在Android画布上封装长文字 [英] Wrapping long text on an Android Canvas

查看:125
本文介绍了在Android画布上封装长文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义控件,可以直接在画布上进行大量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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆