Android 画布上的文字换行 tex

发布于 2024-10-04 05:01:33 字数 84 浏览 0 评论 0原文

我正在尝试将一些字符串写入画布(动态壁纸),但是长字符串不会自动换行有什么方法可以做到吗?
我认为画布是我唯一可以使用的东西,因为它是动态壁纸。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别在捏我脸啦 2024-10-11 05:01:33

无需自己进行自动换行。

查看 android.text.Layout 及其子类 android.text.StaticLayout 和 android.text.DynamicLayout

如下所示:

StaticLayout layout = new StaticLayout(text, txtpaint, textW,Layout.Alignment.ALIGN_NORMAL, 1.3f, 0, false);
txtcanvas.translate(xoffs, yoffs); //position the text
layout.draw(txtcanvas);

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:

StaticLayout layout = new StaticLayout(text, txtpaint, textW,Layout.Alignment.ALIGN_NORMAL, 1.3f, 0, false);
txtcanvas.translate(xoffs, yoffs); //position the text
layout.draw(txtcanvas);
凤舞天涯 2024-10-11 05:01:33

不确定这是否适合您,但对于这些情况,我真的很喜欢简单地创建一个视图,对其进行布局,然后将其“屏幕截图”写入位图中。

该函数将从“just_a_textview”中的参数+ xml 布局创建一个位图。

private static Bitmap renderTextIntoBitmap(Context context, Bitmap.Config bitmapConfig, String text, int textColor, int maxTextWidth) {
    LayoutInflater inflater = LayoutInflater.from(context);
    TextView tv = (TextView) inflater.inflate(R.layout.just_a_textview, null);
    tv.setText(text);
    tv.setMaxWidth(maxTextWidth);

    int widthSpec = View.MeasureSpec.makeMeasureSpec(maxTextWidth, View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

    tv.measure(widthSpec, heightSpec);
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
    tv.setTextColor(textColor);

    Bitmap bitmap = Bitmap.createBitmap(tv.getWidth(), tv.getHeight(), bitmapConfig);
    Canvas canvas = new Canvas(bitmap);
    tv.draw(canvas);
    return bitmap;
}

您不想在每个帧中都执行此操作,但是一旦获得位图,在画布上绘制它的速度非常快。这为您提供了任何 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".

private static Bitmap renderTextIntoBitmap(Context context, Bitmap.Config bitmapConfig, String text, int textColor, int maxTextWidth) {
    LayoutInflater inflater = LayoutInflater.from(context);
    TextView tv = (TextView) inflater.inflate(R.layout.just_a_textview, null);
    tv.setText(text);
    tv.setMaxWidth(maxTextWidth);

    int widthSpec = View.MeasureSpec.makeMeasureSpec(maxTextWidth, View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

    tv.measure(widthSpec, heightSpec);
    tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight());
    tv.setTextColor(textColor);

    Bitmap bitmap = Bitmap.createBitmap(tv.getWidth(), tv.getHeight(), bitmapConfig);
    Canvas canvas = new Canvas(bitmap);
    tv.draw(canvas);
    return bitmap;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文