Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
550 views
in Technique[技术] by (71.8m points)

graphics - Adding text to a bitmap in memory in Android

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...