public static Bitmap decodeSampledBitmapFromResource( Resources res,int resId,int reqWidth,int reqHeight){
//先通过inJustDecodeBounds=true获取图片尺寸
BitmapFactory.Option options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
BitmapFactory.decodeResource(res,resId,options);
//根据图片分辨率以及实际需要的展示尺寸,计算压缩率
options.inSampleSize=calculateInSampleSize(options,reqWidth,reqHeight);
//设置压缩率,并解码
options.inJustDecodeBounds=false;
return BitmapFactory.decodeResource(res,resId,options);
}
解码图片资源
本文介绍了一种从资源中解码并按指定尺寸压缩图片的方法。首先使用BitmapFactory.Options的inJustDecodeBounds属性获取图片原始尺寸,然后计算合适的压缩比例inSampleSize,最后设置inJustDecodeBounds为false并调用BitmapFactory.decodeResource完成图片解码。
527

被折叠的 条评论
为什么被折叠?



