原理是事先取得图片的长宽,直接读出缩略图.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888; // 默认是Bitmap.Config.ARGB_8888
// 下面两个字段需要组合使用
options.inPurgeable = true;
options.inInputShareable = true;
options.inJustDecodeBounds = true;//true和false之间获得长宽和缩放比例
BitmapFactory.decodeFile(filepath, options);
int wRatio = (int) Math.ceil(options.outWidth /(unit_width * PtsPerUnitPic));
int hRatio = (int) Math.ceil(options.outHeight /( unit_height * PtsPerUnitPic));
// 如果超出指定大小,则缩小相应的比例
if (wRatio > 1 && hRatio > 1) {
if (wRatio > hRatio) {
options.inSampleSize = wRatio;
System.out.println(wRatio);
} else {
options.inSampleSize = hRatio;
System.err.println(hRatio);
}
}else {
return;
}
options.inJustDecodeBounds = false;
temp_unit_bitmap = BitmapFactory.decodeFile(filepath, options);