开发者

write comment on bitmap

I have a bitmap displayed on ImageView now i want to give a facility to write comment typed by the user on that bitmap.

i tried using

 Canvas canva开发者_StackOverflows = new Canvas(srcBitmap); canvas.drawText("Hello", 100,100,null);

but this is giving me following error

java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor

later on i want to save this whole image a bitmap


Where did you get your bitmap from? From the exception it means that you are using a resource/asset directly which can not be modified (it is in the actual apk). To avoid this you need to make a copy of the bitmap and use it for the canvas. Here you got some examples to work with.


As Moss pointed out, the bitmap needs to be mutable. Here is some source code how you can do it:

//first, get bitmap and make it mutable
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
Bitmap mutableBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
//now, create canvas and paint as you like
Canvas canvas = new Canvas(mutableBitmap);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(5);
canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
//finally, convert back to icon
Drawable icon = new BitmapDrawable(context.getResources(), mutableBitmap);
getSupportActionBar().setIcon(icon);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜