在Android中创建一个空位图并通过画布进行绘制


Answers:


196

这可能比您想的要简单:

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

这是我在以下主题上找到的一系列教程:使用画布系列绘图


如果我在一个单独的类中创建该位图,我将如何在另一个类中引用该位图。例如:位图文本= BitmapFactory.decodeResource(mContext.getResources(),在这里放什么?);我需要在opengl动态壁纸中使用textView。在此先感谢
Steve C.

嗨,@bigstones,我在创建位图时遵循您的代码在onSizeChanged()中创建位图,但我收到OutOfMemoryError,请参阅此stackoverflow.com/questions/24303759/…–
user123456

使用SurfaceView时如何在另一个线程中完成此操作?
Zach H

-3

不要使用Bitmap.Config.ARGB_8888

而是使用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可使您陷入内存不足的问题。或者更好的方法是,尝试避免使用ARGB选项本身。


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.