Home » Android » creating an empty bitmap and drawing though canvas in android

creating an empty bitmap and drawing though canvas in android

Posted by: admin November 30, 2017 Leave a comment

Questions:

I’d like to create an empty bitmap and set canvas to that bitmap and then draw any shape on bitmap.

Answers:

This is probably more simple than you’re thinking:

int w = WIDTH_PX, h = HEIGHT_PX;

Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);

// ready to draw on that bitmap through that canvas

Here’s a series of tutorial I’ve found on the topic: Drawing with Canvas Series

Questions:
Answers:

Do not use Bitmap.Config.ARGB_8888

Instead use
int w = WIDTH_PX, h = HEIGHT_PX;

Bitmap.Config conf = Bitmap.Config.ARGB_4444; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);

// ready to draw on that bitmap through that canvas

ARGB_8888 can land you in OutOfMemory issues when dealing with more bitmaps or large bitmaps.
Or better yet, try avoiding usage of ARGB option itself.