2013-11-27 53 views
0

我的xml文件中有很多位圖,並且出現內存不足異常。如何在添加到xml文件之前調整圖像

我有這段代碼來調整位圖大小來減少空間。

public static Bitmap decodeSampledBitmapFromResource(Resources res, 
      int resId, int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 

我如何可以使用此代碼來調整圖像在我繪製文件夾之前它們在XML文件中,所以我不出現內存不足的例外的加入?

回答

0

你可以做到這一點,

return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId, options), reqWidth, reqHeight, false); 

你的形象將按照您的reqWidth和reqHeight的大小。

0

您可以使用這樣的...

Bitmap decodedBitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.myImage, 100, 100); //100x100 should be imageView display size. 
imgView.setImageBitmap(decodedBitmap); 
相關問題