在Android中添加文本为位图内存 [英] Adding text to a bitmap in memory in Android

查看:69
本文介绍了在Android中添加文本为位图内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把位图从资源,添加一个文本消息,并将其返回到方法的调用者。这似乎是画布可能是方法,但在code以下无法正常工作。

I'm trying to take a bitmap from resources, add a text message to it and return it to the caller of the method. It seemed like Canvas might be the method but the code below does not work.

public Bitmap annotateBmp(String storyId) {
Bitmap b = BitmapFactory.decodeResource(m_Context.getResources(),     R.drawable.candle_android_pin_512);

    Canvas c = new Canvas(b);
    Paint p = new Paint();

    p.setColor(R.color.red);
    c.drawText("Do you see this?", 30, 210, p);

return b;   //Why does b not have the text?

}

我错过一个步骤或是否有更好的方法?

Did I miss a step or is there a better method?

推荐答案

我想你的code和坠毁在第一线。 由于位图是不变的,所以我必须添加一行以创建一个可变的位图。

I tried your code and crashed on the first line. since the bitmap is immutable, so i have to add a line to create a mutable bitmap.

 b = b.copy(Bitmap.Config.ARGB_8888, true);

那么,你的code只是正常工作。不指定的TEXTSIZE,但是这不是原因。我觉得可能是文字的坐标原点是出位的,所以你不能看到文字。

then, your code just work fine. you do not specify the textSize, but this is not the reason. I think may be the origin coordinate of the text is out of the bitmap so you cannot see the text.

这篇关于在Android中添加文本为位图内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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