同じ位置で20個の同じボールを同じスタイルで表示する必要があります。しかし、私はエラーを取得し続ける:java.lang.OutOfMemoryError:ビットマップサイズがVMの予算を超えて 。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();
}
}
}
:
コードの流れは次のようなものです。エラーログは、次のとおり
1月28日05:29:31.927:E/AndroidRuntime(1090):java.lang.OutOfMemoryErrorを:ビットマップのサイズは 01から28 05 VMの予算を超えた:29:31.927:E/AndroidRuntime( 1090):android.graphics.Bitmap.nativeCreate(ネイティブメソッド) 01-28 05:29:31.927:E/AndroidRuntime(1090):android.graphics.Bitmap.createBitmap(Bitmap.java:468) 01- 28 05:29:31.927:E/AndroidRuntime(1090):android.graphics.Bitmap.createBitmap(Bitmap.java:435) 01-28 05:29:31.927:E/AndroidRuntime(1090):android.graphics .Bitmap.createScaledBitmap(Bitmap.java:340) 01-28 05:29:31.927:E/AndroidRuntime(1090):android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:488) 01-28 05:29 :31.927:E/AndroidRuntime(1090): android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:462) 01-28 05:29:31.927:E/AndroidRuntime(1090):android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:323) 01- 28 05:29:31.927:E/AndroidRuntime(1090):android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:346) 01-28 05:29:31.927:E/AndroidRuntime(1090):android.graphics .BitmapFactory.decodeResource(BitmapFactory.java:372) 01-28 05:29:31.927:E/AndroidRuntime(1090):com.gzconcern.animalaba.AbaGame。 playBall(AbaGame.java:533)
私はballAnimationにフレームを追加するとき、私は絵から新しいビットマップを持っているので、私はすぐに絵を解放しなければならないと思いました。しかし、私は、リサイクルを呼び出す行うとき、それはこのことを判明:
java.lang.RuntimeException:キャンバス:で重いビットマップを使用している場合、リサイクルのビットマップを使用しようとしている[email protected]
このコードをエミュレータで実行している場合は、デバイス上で直接実行してみてください。これはうまくいくでしょう... –