2012-07-10 40 views
1

我有一個應用程序在Android上有一個imageflipper。問題是,大約8個圖像加載到內存後,出現內存不足錯誤。Android的png圖像大內存

那麼,我試圖做動態圖像加載,以便如果用戶翻轉2張圖像,我會加載下2到內存並刪除2個第一個。這種作品很有用,但它很醜,用戶翻轉圖像時遇到了麻煩(imageflipper.showprevious()),我無法真正移動所有圖像並將新圖像放到開頭。

我的問題是:
有沒有更好的方法來做這種東西?調整圖像大小並沒有真正的幫助。

回答

0

我使用下面的代碼片段來解決與內存有關的問題,我們可以通過isRecycled()方法的幫助來解決這個問題。添加該代碼與你,這裏finalImage是我BitmapDrawableR.id.image_viewer是我的ImageView,你可以改變它你

@Override 
     protected void onDestroy() { 

      finalImage.setCallback(null); 
      if (!((BitmapDrawable) finalImage).getBitmap().isRecycled()) { 
      ((BitmapDrawable) finalImage).getBitmap().recycle(); 
      } 
      finalImage = null; 
      unbindDrawables(findViewById(R.id.image_viewer)); 
      Runtime.getRuntime().gc(); 
      // scaledBitmap.recycle(); 
      System.gc(); 
      super.onDestroy(); 
     } 

     private void unbindDrawables(View view) { 
      if (view.getBackground() != null) { 
       view.getBackground().setCallback(null); 
      } 
      if (view instanceof ViewGroup) { 
       for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { 
        unbindDrawables(((ViewGroup) view).getChildAt(i)); 
       } 
       ((ViewGroup) view).removeAllViews(); 
      } 
     } 
0

使用BitmapFactory.Options

BitmapFactory.Options opts = new BitmapFactory.Options(); 
opt.inSampleSize = 2; 
//this will decrease bitmap size, 
// but also affect quality of the image, 
//so just play with this value to spot the good one; 
Bitmap b = BitmapFactory.decodeFile(fileName, opts);