如何在位图或绘图中添加文本?

发布于 2025-01-02 05:03:04 字数 93 浏览 5 评论 0原文

是否可以将动态文本添加到 android 中的可绘制或位图?我正在尝试做一些事情,比如动态获取一些文本并将该文本放入图像中。或者至少我想给用户一种错觉,即文本位于该图像内。

Is that possible to add a dynamic text to drawable or bitmap in android? im trying do something like get some text dynamically and put that text into an image. Or at least i want to give a illusion to the user that the text is inside that image.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

半山落雨半山空 2025-01-09 05:03:04

我知道您已经找到了解决方案,但有些人可能对这种用文本设置 Imageview 的新方法感兴趣,

这是我的解决方案:-

Bitmap bitmap = BitmapFactory.decodeResource(
            getResources(), com.cinchvalet.app.client.R.drawable.slider_pop_counter);

    Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas c = new Canvas(bmp);
    String text = "Your text";
    Paint p = new Paint();
    p.setTypeface(Typeface.DEFAULT_BOLD);
    p.setTextSize(35);
    p.setColor(getResources().getColor(com.cinchvalet.app.client.R.color.Black));
    int width = (int) p.measureText(text);
    int yPos = (int) ((c.getHeight() / 2)
            - ((p.descent() + p.ascent()) / 2) - 10);
    c.drawText(text, (bmp.getWidth() - width) / 2, yPos, p);

-在您的位图图像上绘制文本位图,然后您可以设置位图到您的Imageview

-谢谢!

I know you have found the solution but some people may be interested in this new way of setting Imageview with Text

Here is my Solution:-

Bitmap bitmap = BitmapFactory.decodeResource(
            getResources(), com.cinchvalet.app.client.R.drawable.slider_pop_counter);

    Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas c = new Canvas(bmp);
    String text = "Your text";
    Paint p = new Paint();
    p.setTypeface(Typeface.DEFAULT_BOLD);
    p.setTextSize(35);
    p.setColor(getResources().getColor(com.cinchvalet.app.client.R.color.Black));
    int width = (int) p.measureText(text);
    int yPos = (int) ((c.getHeight() / 2)
            - ((p.descent() + p.ascent()) / 2) - 10);
    c.drawText(text, (bmp.getWidth() - width) / 2, yPos, p);

-on your bitmap Image Draw Text On bitmap and then you can set Bitmap to your Imageview

-THanks!

心意如水 2025-01-09 05:03:04

您是否考虑过使用相对布局简单地将 TextView 覆盖在 ImageView 上?

Have you considered simply overlaying a TextView over the ImageView using a relative layout?

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