标签:bitmap outofmemory 内存 溢出 性能
图片可能会有不同的大小。在许多情况下,图片的大小会超出我们需要的大小。例如,我们的Camera应用,我们所拍的照片的大小远大于屏幕显示的大小
假如你的应用被限制了内存使用,显而易见,你会选择加载一个低分辨率的图片。这张低分辨率的图片应该匹配屏幕的尺寸。更高分辨率的图像没有提供任何可见的好处,但仍占用宝贵的内存,而且由于额外的动态缩放,会带来额外的性能开销。
本篇文章教你通过加载一个小尺寸的图片样本,来修饰一张大图,并且没有超过应用的内存限制。
原文地址http://wear.techbrood.com/training/displaying-bitmaps/load-bitmap.html
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(), R.id.myimage, options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType;为了避免
java.lang.OutOfMemory exceptions,在解码图片之前,预先检查Bitmap的大小,除非你有绝对的把握你的bitmap有合适的大小,并且在内存限制大小范围之内。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;
}inJustDecodeBounds为true,然后我们计算inSample的值,然后设置inJustDecodeBounds为false,最后解码图片。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);
}mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));
BitmapFactory.decode* as
need。标签:bitmap outofmemory 内存 溢出 性能
原文地址:http://blog.csdn.net/liu8497/article/details/40786721