开发者

Creating an empty bitmap and drawing though canvas in Android

开发者 https://www.devze.com 2023-02-25 08:53 出处:网络
I\'d like to create an empty bitmap and 开发者_如何学Goset a canvas to that bitmap and then draw any shape on the bitmap.This is probably simpler than you\'re thinking:

I'd like to create an empty bitmap and 开发者_如何学Goset a canvas to that bitmap and then draw any shape on the bitmap.


This is probably simpler 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 the official documentation on the topic: Custom Drawing


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.

0

精彩评论

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

关注公众号