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

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

问题描述

我正在尝试从资源中获取位图,向其中添加文本消息并将其返回给方法的调用者.看起来 Canvas 可能是方法,但下面的代码不起作用.

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?

推荐答案

我尝试了你的代码,但在第一行崩溃了.由于位图是不可变的,所以我必须添加一行来创建一个可变位图.

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);

那么,您的代码就可以正常工作.您没有指定 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天全站免登陆