2011-04-14 14 views

回答

159

这可能是更简单的比你想:

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 

这里有一系列的教程,我的话题发现:Drawing with Canvas Series

+0

如果我在一个单独的类中创建它,我将如何引用另一个类中的位图。例如:位图文本= BitmapFactory.decodeResource(mContext.getResources(),这里放什么?);我需要一个opengl动态壁纸内的textView。在此先感谢 – 2013-05-07 21:47:01

+0

您好@bigstones我在onSizeChanged()创建位图时,我正在跟随您的代码创建位图时我正在OutOfMemoryError请参阅http://stackoverflow.com/questions/24303759/outofmemoryerror-when-creatingbitmp – user123456 2014-06-23 05:25:06

+0

如何这可以在使用SurfaceView时在另一个线程中完成吗? – 2014-06-24 00:20:37

-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会将您置于OutOfMemory问题中。 或更好的是,尽量避免使用ARGB选项本身。

+0

ARGB_8888是Android位图源代码的默认值 – 2014-01-26 19:21:57

+0

嗨@userI创建位图onSizeChanged()与RGB_565,但是当我创建位图时,我得到OutOfMemoryError.Please看到这个http://stackoverflow.com/questions/24303759/ outofmemoryerror-when-creatingbitmp – user123456 2014-06-23 05:27:39

+9

ARGB_4444现已弃用(http://developer.android.com/reference/android/graphics/Bitmap.Config.html#ARGB_4444) – Allen 2014-09-21 23:36:49