public Bitmap getDownDimensionBitmap(String data) {
Bitmap bitmap = null;
if (data != null) {
BitmapFactory.Options options = new BitmapFactory.Options();
//1.2设置inJustDecodeBounds 来控制解码器,只会进行图片宽高的获取,不会获取图片
//不占用内存,当使用这个参数,代表BitmapFactory.decodexxx()不会返回bitmap
options.inJustDecodeBounds = true;
//解码,使用options参数 设置解码方式
BitmapFactory.decodeFile(data, options);
//2.步骤2 根据图片的真实尺寸,与当前需要显示的尺寸,进行计算,生成采样率
//2.1
int requestWidth = DensityUtils.getScreenWidth(this);
int requestHeight = DensityUtils.getScreenHeight(this);
//2.3计算 设置 图片采样率
options.inSampleSize = calculateInSampleSize(options, requestWidth, requestHeight);//宽度的1/32 高度的1/32
//2.4开放 解码,实际生成Bitmap
options.inJustDecodeBounds = false;
//2.4.1 Bitmap.Config的说明
//要求解码器对于每一个采样的像素,使用RGB_565 存储方式
//一个像素,占用两个字节,比ARGB_8888笑了一半
//如果解码器不能使用指定配置,就自动使用ARGB_8888
options.inPreferredConfig = Bitmap.Config.RGB_565;
//2.4.2是一个过时的设置,可以及时清除内存
options.inPurgeable = true;
//2.5使用设置采样的参数,来进行 解码,获取bitmap
bitmap = BitmapFactory.decodeFile(data, options);
}
return bitmap;
}
private 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;
//只有当请求的宽度、高度 > 0时,进行缩放
//否则,图片不进行缩放
if (reqHeight > 0 && reqWidth > 0) {
while ((height / inSampleSize) > reqHeight
&& (width / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}