Adding text to a bitmap in memory on Android

I am trying to extract a bitmap from resources, add a text message to it and return it to the calling method. It seemed that Canvas could be a 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? } 

Am I missing a step or is there a better method?

+2
source share
1 answer

I tried my code and crashed on the first line. since the bitmap is immutable, so I need to add a line to create a modified bitmap.

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

then your code will work fine. you do not specify textSize, but that is not the reason. I think there may be a coordinate of the beginning of the text outside the bitmap so that you cannot see the text.

+2
source

All Articles