2012-03-19 98 views
0

所以我得到了這個背景圖片爲我的活動。它是一個480x800 PNG。 它有一個漸變,所以有綁紮的危險,這就是爲什麼我99%的不透明強迫最好的顏色模式。背景圖像內存大小

在我的設備上,甚至在宏達魔法這是沒有問題的。

但是,在默認1.6模擬器上,出現內存不足錯誤。該怎麼辦? 背景被設置在代碼:

bgView.setImageResource(R.drawable.baby_pink_solid); 

最大VM堆設置爲192和設備RAM的大小爲256似乎不是一個解決辦法。

+0

看起來像在背景中的imageview,而不是一個圖像作爲背景。你爲什麼要使用imageview? – njzk2 2012-03-19 13:11:33

回答

0

嘗試訪問代碼中的位圖,然後通過setImageBitmap()設置它。如果您在代碼中解碼位圖時得到OOM,那麼這就是爲什麼您從setImageResource()獲得它。

我發現Bitmaps在Android上處理是一件棘手的事情,使用它們時必須小心!

也檢查@Sadeshkumar Periyasamy的答案,這對解碼位圖或更大尺寸的設備沒有像今天設備那麼強大的功能很有用。

0

試試這個代碼是按比例的任何位圖:

public class ImageScale 
{ 
/** 
* Decodes the path of the image to Bitmap Image. 
* @param imagePath : path of the image. 
* @return Bitmap image. 
*/ 
public Bitmap decodeImage(String imagePath) 
{ 
    Bitmap bitmap=null; 
    try 
    { 

     File file=new File(imagePath); 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 

     BitmapFactory.decodeStream(new FileInputStream(file),null,o); 
     final int REQUIRED_SIZE=200; 
     int width_tmp=o.outWidth, height_tmp=o.outHeight; 

     int scale=1; 
     while(true) 
     { 
      if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
      break; 
      width_tmp/=2; 
      height_tmp/=2; 
      scale*=2; 
     } 

     BitmapFactory.Options options=new BitmapFactory.Options(); 

     options.inSampleSize=scale; 
     bitmap=BitmapFactory.decodeStream(new FileInputStream(file), null, options); 

    } 
    catch(Exception e) 
    { 
     bitmap = null; 
    }  
    return bitmap; 
} 

/** 
    * Resizes the given Bitmap to Given size. 
    * @param bm : Bitmap to resize. 
    * @param newHeight : Height to resize. 
    * @param newWidth : Width to resize. 
    * @return Resized Bitmap. 
    */ 
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) 
{ 

    Bitmap resizedBitmap = null; 
    try 
    { 
     if(bm!=null) 
     { 
      int width = bm.getWidth(); 
      int height = bm.getHeight(); 
      float scaleWidth = ((float) newWidth)/width; 
      float scaleHeight = ((float) newHeight)/height; 
      // create a matrix for the manipulation 
      Matrix matrix = new Matrix(); 
      // resize the bit map 
      matrix.postScale(scaleWidth, scaleHeight); 
      // recreate the new Bitmap 
resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,  true); 
// resizedBitmap = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true); 
     } 
    } 
    catch(Exception e) 
    { 
     resizedBitmap = null; 
    } 

    return resizedBitmap; 
} 

}