处理 java.lang.OutOfMemoryError 时遇到问题:位图大小超出 VM 预算错误。原始图片永远不会大于 250x250 像素。并从可绘制文件夹加载。我在互联网上找到了一些关于“inJustDecodeBounds”的解决方案,但我无法让它工作。关于如何解决这个问题的任何想法?头疼了两天了。。。
现在,我正在通过基于父宽度计算的因子重新缩放图像..
@Override
public View getView(int position, View v, ViewGroup parent) {
View mView = v;
this.parent = parent;
if (mView == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mView = vi.inflate(R.layout.caa_xml, null);
}
ImageView image = (ImageView) mView.findViewById(R.id.iv_caarow);
String name = getItem(position).getFile();
int resId = C.getResources().getIdentifier(name, "drawable",
"com.test.com");
int imageWidth = (int) calculateImageWidth();
// load the origial BitMap (250 x 250 px)
Bitmap bitmapOrg = BitmapFactory
.decodeResource(C.getResources(), resId);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = imageWidth;
int newHeight = imageWidth;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
image.setImageDrawable(bmd);
if (mView != null) {
//additional code here
}
return mView;
}
private float calculateImageWidth() {
// TODO Auto-generated method stub
int parentW = parent.getWidth() - parent.getPaddingLeft()
- parent.getPaddingRight();
Resources r = C.getResources();
float pxPaddingBetweenItem = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 2, r.getDisplayMetrics());
float pxPaddingInItem = TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10, r.getDisplayMetrics());
int totalImageWidth = (parentW - (int) (3 * pxPaddingBetweenItem) - (int) (8 * pxPaddingInItem)) / 4;
float imageWidth = (float) totalImageWidth;
return imageWidth;
}