2014-02-24 107 views
1

我嘗試更改ImageView的背景,但由於OutOfMemoryError而導致錯誤。如何設置imageView的背景避免Android中的OutOfMemoryError?

我有使用Bitmap搜索更改背景,但我不知道如何使用它。

當我點擊按鈕時,背景將變爲另一張圖片。

的代碼是這樣的:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View view = inflater.inflate(R.layout.user_guide, container, false) ; 
     last = (Button) view.findViewById(R.id.last); 
     user = (ImageView) view.findViewById(R.id.image); 
     last.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
           user.setBackgroundResource(R.drawable.test1); 
      } 
     }); 

我有搜索下面的代碼,但我不知道燙到用它來改變Imageviewbackground。 有人可以教我如何使用它?

public static Bitmap readBitMap(Context context, int resId){ 
     BitmapFactory.Options opt = new BitmapFactory.Options(); 
     opt.inPreferredConfig = Bitmap.Config.RGB_565; 
     opt.inPurgeable = true; 
     opt.inInputShareable = true; 
     InputStream is = context.getResources().openRawResource(resId); 
     return BitmapFactory.decodeStream(is,null,opt); 
     } 

有人能教我怎麼用它來改變ImageView並沒有OutOfMemoryError背景?

在此先感謝。

+0

見本問題:[http://stackoverflow.com/questions/2220949/handling-large-bitmaps](http://stackoverflow.com/ques蒸發散/ 2220949 /處理 - 大位圖) –

回答

1

試試下面的代碼:

last.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Bitmap bitmap=readBitmap(this, R.drawable.test1); 
       image.setImageBitmap(bitmap); 
       bitmap.recycle(); 
       bitmap=null; 
      } 
     }); 

用你的方法:

public static Bitmap readBitMap(Context context, int resId){ 
     BitmapFactory.Options opt = new BitmapFactory.Options(); 
     opt.inPreferredConfig = Bitmap.Config.RGB_565; 
     opt.inPurgeable = true; 
     opt.inInputShareable = true; 
     InputStream is = context.getResources().openRawResource(resId); 
     return BitmapFactory.decodeStream(is,null,opt); 
     } 
0

用於載入位圖有效地將這兩種方法..... for efficient large bitmap loading

last.setOnClickListener(new OnClickListener() { 


     @Override 
     public void onClick(View v) { 
      WeakReference bitmapWeakReference=new WeakReference(decodeSampledBitmapFromResource(getApplicationContext().getRssource(),drawableId,reqWidth,reqHeight)); 
      user.setImageBitmap(bitmapWeakReference.get()); 
     } 
    }); 

public static int calculateInSampleSize(
      BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 
    if (height > reqHeight || width > reqWidth) { 
     final int halfHeight = height/2; 
     final int halfWidth = width/2; 
     while ((halfHeight/inSampleSize) > reqHeight 
       && (halfWidth/inSampleSize) > reqWidth) { 
      inSampleSize *= 2; 
     } 
    } 
    return inSampleSize; 
} 
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
     int reqWidth, int reqHeight) { 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
}