2011-04-14 31 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