我的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 :(得分:0)
你可以这样做,
return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId, options), reqWidth, reqHeight, false);
您的图片将遵循您的reqWidth和reqHeight的大小。
答案 1 :(得分:0)
你可以这样使用......
Bitmap decodedBitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.myImage, 100, 100); //100x100 should be imageView display size.
imgView.setImageBitmap(decodedBitmap);