由源图像压缩为固定目标尺寸的图像。
1.求源宽,高为计算压缩尺寸做准备。
BitampFactory.Options.inSampleSize 控制BitmapFactory分配恰当的内存空间。
BitampFactory.Options.inJustDecodeBounds 为true可以在decodeFile并且不分配内存空间,但可以计算出源图像的width和height。
2.根据reqwidth,reqheight,求inSampleSize。
if width>reqwidth || height > reqheight
int heightRatio = height / reqheight;
int widthRatio = width /reqwidth;
inSampleSize = heightRatio < widthRratio ? heightRatio : widthRatio; // 保障最后imageview的dimension比request的大
// 对于图片很长或很宽的情况做判断
totalPixels = width * height;
reqtotalPixels = reqwidth * reqheight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > reqtotalPixels){
inSampleSize++;
}
return inSampleSize;
3.品质压缩 (android 原生的压缩)
Bitmap.Compress 参数阈值参考在75
当然质量要求高需要采用第三库了。