在 Android Canvas 上包装长文本 [英] Wrapping long text on an Android Canvas

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

问题描述

我有一个自定义控件,它可以直接在 canvas 上进行大量 2D 绘图.

I have a custom control that is doing a lot of 2D drawing straight to the canvas.

此图有些是文本,所以我使用 Canvas.drawText() 方法.

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 类用于此;只需为所需的文本、对齐方式、宽度等创建一个 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 Canvas 上包装长文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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