[android]代码库
public static Bitmap getBitmapBySimpleSize(Object object, float width,
float height) {
if (null == object) {
return null;
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 通过这个bitmap获取图片的宽和高
Bitmap bitmap = BitmapFactory.decodeFile(object.toString(), options);
if (options.outWidth > width) {
int scale = (int) (options.outWidth / width);
// 如果按照宽度缩放比例导致高度小于原定高度,则按照高度的缩放比例来缩放
if (options.outHeight / scale < height) {
scale = (int) (options.outHeight / height);
}
// Logger.e( "HongLi" , "options.outWidth1:" + options.outWidth +
// ";options.outHeight:" + options.outHeight );
scale = scale <= 0 ? 1 : scale;
options.inSampleSize = scale;
options.outWidth = options.outWidth / options.inSampleSize;
options.outHeight = options.outHeight / options.inSampleSize;
// Logger.e( "HongLi" , "options.outWidth2:" + options.outWidth +
// ";options.outHeight:" + options.outHeight );
}
setBitmapNormalOptions(options);
// 注意这次要把options.inJustDecodeBounds 设为 false,这次图片是要读取出来的。
bitmap = BitmapFactory.decodeFile(object.toString(), options);
return bitmap;
}
private static void setBitmapNormalOptions(Options options) {
if (null == options) {
return;
}
options.inJustDecodeBounds = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
}
by: 发表于:2018-01-02 10:00:13 顶(1) | 踩(1) 回复
??
回复评论