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

标签 android android-layout drawable android-imageview

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

最佳答案

我知道您已经找到了解决方案,但有些人可能对这种使用文本设置 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);

-on your bitmap Image Draw Text On bitmap 然后你可以设置Bitmap到你的Imageview

-谢谢!

关于android - 如何在位图或绘图中添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9122999/

相关文章:

android - 工具栏 - 添加向上按钮

android - 如何在Android中的Mp android Bar Chart中将两位小数设置为Bar Entry

android-layout - DatePicker 内容与自定义对话框的标题重叠

Android EditText 文本的偏移量?

android - 需要将图像从 res 文件夹复制到画廊/文件夹

android - AndEngine 如何在图片上放置文字?

java - 安卓。 GradientDrawable 设置GradientCenter

android - 全屏 ActionBarSherlock

android - 将滑动标签与粘性标题相结合

Android:可绘制分辨率