Android 画布上的文字换行 tex
我正在尝试将一些字符串写入画布(动态壁纸),但是长字符串不会自动换行有什么方法可以做到吗?
我认为画布是我唯一可以使用的东西,因为它是动态壁纸。
I'm trying to write some strings to a canvas (live wallpaper) however long string don't get word-wraped is there any way to do it?
I think canvas is the only thing I can use for this, since it's a live wallpaper.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需自己进行自动换行。
查看 android.text.Layout 及其子类 android.text.StaticLayout 和 android.text.DynamicLayout
如下所示:
No need to do your own word wrapping.
Check out android.text.Layout and its subclasses android.text.StaticLayout and android.text.DynamicLayout
something like this:
不确定这是否适合您,但对于这些情况,我真的很喜欢简单地创建一个视图,对其进行布局,然后将其“屏幕截图”写入位图中。
该函数将从“just_a_textview”中的参数+ xml 布局创建一个位图。
您不想在每个帧中都执行此操作,但是一旦获得位图,在画布上绘制它的速度非常快。这为您提供了任何 Android 视图的所有灵活性。
我知道如果您的文本非常动态,这将不起作用,但您也许可以这样做,并使用画布矩阵来执行您可能想做的任何缩放/旋转。
显然,这可以进一步优化性能,例如通过拉出布局膨胀。
Not sure if this will work for you, but for these situations I really like to simply create a View, layout it, and then write a "screenshot" of it into a Bitmap.
This function will create a bitmap from the params + the xml layout in "just_a_textview".
You don't want to do this in every frame, but once you have the bitmap, drawing it on a canvas is very fast. And this gives you all the flexibility of ANY Android View.
I understand that this won't work if your text is very dynamic, but you can maybe do it like this and use the canvas matrix to do any scaling/rotation that you might want to do.
This can be further optimized for performance obviously, e.g. by pulling out the layout inflation.