我有一個安卓遊戲,加載6個圖像總計300kb。我已經閱讀並實施了這篇文章http://developer.android.com/training/displaying-bitmaps/load-bitmap.html,雖然它有一點幫助,但它並沒有解決我的問題100%。Android內存加載位圖
遊戲首次加載時運行得很好。沒有錯誤或任何東西。然後,當您關閉遊戲或按主屏幕按鈕,稍後再回到遊戲中時,會出現內存不足錯誤,但有時會出現。在我的簡歷中,我只是將遊戲線程備份起來。我沒有加載任何圖像或類似的東西。當然,我不能有唯一的加載6個或更多圖像的遊戲?
我的onResume
@Override
protected void onResume(){
try{
game.setIsPaused(false);
game.startSounds();
game.threadStart();
super.onResume();
}
加載我的圖片在自己的線程
bg = bt.resizeBitmapPercent(bt.createBitmap(R.drawable.bg, context, options), imgPercentWidth, imgPercentHeight);
overlay = bt.resizeBitmapPercent(bt.createBitmap(R.drawable.overlay, context, options), imgPercentWidth, imgPercentHeight);
reel = bt.resizeBitmapPercent(bt.createBitmap(R.drawable.reels, context, options), imgPercentWidth, imgPercentHeight);
payOutBG = bt.resizeBitmapPercent(bt.createBitmap(R.drawable.payout, context, options), imgPercentWidth, imgPercentHeight);
achievement = bt.resizeBitmapPercent(bt.createBitmap(R.drawable.achievement, context, options), imgPercentWidth, imgPercentHeight);
的方法來調整和關閉遊戲後創建位圖
public Bitmap createBitmap(int id, Context context, BitmapFactory.Options options){
return BitmapFactory.decodeResource(context.getResources(), id, options);
}
public Bitmap resizeBitmapPercent(Bitmap bitmap, float w, float h){
float newWidth = bitmap.getWidth()*w;
float newHeight = bitmap.getHeight()*h;
float scaleWidth = (float) newWidth/bitmap.getWidth();
float scaleHeight = (float) newHeight/bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(bitmap, 0, 0,bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
http://stackoverflow.com/questions/11418354/recycling-bitmaps。回收位圖可能會解決您的問題。檢查鏈接。 – Raghunandan