/** * 通过图片资源id获取图片对应的压缩后的Bitmap对象 */ publicstatic Bitmap getCompressedBitmap(Resources res, int resId, int requiredWidth, int requiredHeight){ BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resId, options);// 第一次解析 options.inSampleSize = calculateInSampleSize(options, requiredWidth, requiredHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(res, resId, options);// 第一次解析 }
/** * 计算压缩的inSampleSize的值,该值会在宽高上都进行压缩(也就是压缩前后比例是inSampleSize的平方倍) */ privatestaticintcalculateInSampleSize(BitmapFactory.Options options, int requireWidth, int requireHeight){ // 获取源图片的实际的宽度和高度 int realWidth = options.outWidth; int realHeight = options.outHeight;