我必須顯示20個相同的球在不同位置以相同樣式跳轉。但我不斷收到錯誤:java.lang.OutOfMemoryError:位圖大小超過虛擬機預算 。請幫我弄過java.lang.OutOfMemoryError:位圖大小超過VM預算
起初我試着用anim文件夾中的xml文件。但我不能釋放任何在xml中聲明的資源。所以我改變了與BitmapFactory創建位圖,並調用回收函數。但我無法克服。
碼流是這樣的:
Bitmap[] ballBitmap;
int[] playOrder = new int[2]; //the balls will play in order of the definiens in playOrder
ImageView[] balls = new ImageView[20]; //it contains 20 balls
private void iniNewQuestion() {
passNum++;
if(passNum > 1) clearPic();
if(passNum < 20) playBall();
}
private void playBall() {
int ballIndex = playOrder[passNum - 1] - 1;
ballBitmap = new Bitmap[9];
ballBitmap[0] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_01);
ballBitmap[1] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_02);
ballBitmap[2] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_03);
ballBitmap[3] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_04);
ballBitmap[4] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_up_05);
ballBitmap[5] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_06);
ballBitmap[6] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_07);
ballBitmap[7] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_08);
ballBitmap[8] = BitmapFactory.decodeResource(context.getResources(), R.drawable.game_ball_flat_09);
AnimationDrawable ballAnimation = new AnimationDrawable();
for (int picIndex = 0; picIndex < 9; picIndex++)
{
ballAnimation.addFrame(new BitmapDrawable(ballBitmap[picIndex]), breakTime[picIndex]);
//if(picIndex > 0) ballBitmap[picIndex].recycle();
}
ballAnimation.setOneShot(true);
balls[ballIndex].setBackgroundDrawable(ballAnimation);
ballAnimation.start();
Handler toNextQuestionHandler = new Handler();
toNextQuestionHandler.postDelayed(runIniNewQuestion, 3000);
}
private Runnable runIniNewQuestion = new Runnable() { public void run() { iniNewQuestion(); } };
private void clearPic() {
int picIndex = 0;
for (picIndex = 0; picIndex < 9; picIndex++)
{
if(ballBitmap[picIndex] != null)
{
if(!ballBitmap[picIndex].isRecycled()) ballBitmap[picIndex].recycle();
}
}
}
當我玩球6總是發生。錯誤日誌是:
01-28 05:29:31.927:E/AndroidRuntime(1090):java.lang.OutOfMemoryError:位圖大小超過VM預算 01-28 05:29:31.927:E/AndroidRuntime 1090):at android.graphics.Bitmap.nativeCreate(Native Method) 01-28 05:29:31.927:E/AndroidRuntime(1090):at android.graphics.Bitmap.createBitmap(Bitmap.java:468) 01- 28 05:29:31.927:E/AndroidRuntime(1090):at android.graphics.Bitmap.createBitmap(Bitmap.java:435) 01-28 05:29:31.927:E/AndroidRuntime(1090):at android.graphics .Bitmap.createScaledBitmap(Bitmap.java:340) 01-28 05:29:31.927:E/AndroidRuntime(1090):at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488) 01-28 05:29 :31.927:E/AndroidRuntime(1090):在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:322) 01-28 05:29:31.927:E/AndroidRuntime(1090) 28 05:29:31.927:E/AndroidRuntime(1090):at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:346) 01-28 05:29:31.927:E/AndroidRuntime(1090):at android.graphics .BitmapFactory.decodeResource(BitmapFactory.java:372) 01-28 05:29:31.927:E/AndroidRuntime(1090):at com.gzconcern.animalaba.AbaGame。我想當我添加一個幀到ballAnimation,我有一個新的位圖從圖片,所以我應該馬上釋放圖片。但是當我做電循環,事實證明這樣的:
了java.lang.RuntimeException:畫布:試圖使用回收的位圖[email protected]
如果您在仿真器上運行此代碼,則嘗試直接在設備上運行。這可能會奏效... – 2012-01-28 16:04:43