BitmapFactory.Options的属性解析

BitmapFactory.Options的属性解析:
inJustDecodeBounds=true,让解析方法禁止为bitmap分配内存,返回值不再是一个Bitmap对象,而是null,但是BitmapFactory.Options的outWidth和outHeight和outMimeType都会被赋值,
这个属性可以让我们在加载图片之前就能获取到图片的长宽和MIME类型,从而根据情况对图片进行压缩。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(),R.drawable.zhu,options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;


inSampleSize=4,实现对图片进行压缩,可以将像素为2048*1536的图片压缩成512*384像素
根据图片宽高和控件宽高计算出合适的inSampleSize值:
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
if(imageHeight > reqHeight || imageWidth > reqWidth){
 final int heightRatio = Math.round((float)imageHeight/(float)reqHeight);
 final int widthRation = Math.round((float)imageWidth/(float)reqWidth);
 inSampleSize = heightRatio < widthRation ? heightRatio : widthRation;
}


根据inSampleSize的值,先将inJustDecodeBounds=false,然后再解析一次图片就能得到压缩后的图片了。

猜你喜欢

转载自blog.csdn.net/u010015933/article/details/81014769