做android开发最揪心的就是遇到OOM,为什么呢?因为它不是在编译期就报异常,是在运行期才报异常,而且它是积累性的,一次两次运行还不一定能重现,你要找出问题所在也不容易。但是基本可以肯定,在加载大量图片或一张分辨率很高的图片时,很容易出现OOM(内存溢出)。
我碰到的这个OOM是在验证手势密码的页面,因为这个页面会被经常打开,而不是只打开一两次,所以就满足了OOM发生的条件。在这个页面加载的背景图太大,在某些型号的手机上容易出现OOM。
这段代码是官方的解决方案,可以放心的用:
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
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;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
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);
}
使用的时候把屏幕的宽高传入(因为我的这张图片是填充屏幕的背景图,所以以屏幕宽高设置,其他情况根据imageView的宽高设定):
bgBitmap = ImageUtil.decodeSampledBitmapFromResource(getResources(), R.mipmap.gesture_bg,
ViewUtil.getScreenWidth(this), ViewUtil.getScreenHeight(this));
iv_bg.setImageBitmap(bgBitmap);
不用时注意销毁bitmap:
@Override
protected void onDestroy() {
super.onDestroy();
if(iv_bg != null){
iv_bg.setImageBitmap(null);
}
if(bgBitmap != null){
bgBitmap.recycle();
bgBitmap = null;
}
if (mGestureContentView != null) {
mGestureContentView.destroyBitmap();
}
}
所占用的内存前后对比:
改前:
改后: