2012-01-28 115 views
0

我必須顯示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]

回答

1

它使用在重位圖時是一個常見的問題運行。希望這裏給出的一些解決方案可以爲你工作http://code.google.com/p/android/issues/detail?id=8488 你應該做的另一件事是在DDMS中監視應用程序的運行時內存。 要監控它,請選擇設備,然後選擇您的應用程序。然後選擇頂部顯示'更新堆'的綠色圖標。然後打開'Heap'窗口並選擇'Cause GC'。 在那裏您將看到可用於您的應用程序的內存以及應用程序使用的實際內存。

+0

如果您在仿真器上運行此代碼,則嘗試直接在設備上運行。這可能會奏效... – 2012-01-28 16:04:43

相關問題