获取图片:
/**
* 解码 图片
*
* @param pathName 图片的绝对路径。
* @param reqWidth 期望输出的图片最小宽度。<strong>该值不能为0或者小于0。</strong>
* @param reqHeight 期望输出的图片最小高度。<strong>该值不能为0或者小于0。</strong>
*
* @return 加载成功将返回Bitmap,加载失败返回null。
*/
public static Bitmap decodeFile(String pathName, int reqWidth, int reqHeight) {
Bitmap bitmap = null;
try {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;//为true时为空解码只为获取opt
BitmapFactory.decodeFile(pathName, opt);
opt.inSampleSize = calculateInSampleSize(opt, reqWidth, reqHeight);//设置实际解码时的比例 ****为什么这样呢?为了避免OOM
opt.inJustDecodeBounds = false;//为false时真解码
bitmap = BitmapFactory.decodeFile(pathName, opt);
} catch (Throwable e) {
e.printStackTrace();
}
return bitmap;
}
计算压缩比:
/**
* 根据图片大小计算缩放比例
*
* @param options 图片参数
* @param reqWidth 输出宽度
* @param reqHeight 输出高度
*/
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {//取最小的压缩比
inSampleSize = Math.round((float) height / (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
图片裁剪:
/**
* 裁剪图片
*
* @param src 原始图片
* @param reqWidth 新的图片宽度
* @param reqHeight 新的图片高度
* @param imageScaleType 图片缩放模式
*
* @return 裁剪好的图片
*/
public static Bitmap scaleBitmap(Bitmap src, int reqWidth, int reqHeight, ImageScaleType imageScaleType)
throws Exception {
float scaleX;
float scaleY;
float srcWidth = src.getWidth();
float srcHeight = src.getHeight();
float newWidth;
float newHeight;
switch (imageScaleType) {
case CENTER_CROP://中心裁剪
if (srcWidth / srcHeight > (float) reqWidth / reqHeight) {//原图比要求图宽
scaleY = (float) reqHeight / srcHeight;//横向图 横向裁剪
} else {
scaleY = (float) reqWidth / srcWidth;//纵向图 纵向裁剪
}
if (scaleY > 1.0f) {//只裁剪大图,小图原样
newWidth = reqWidth / scaleY;//newWidth = srcWidth
newHeight = reqHeight / scaleY;//newWidth = srcHeight
scaleX = scaleY = 1.0f;
} else {
scaleX = scaleY;
newWidth = reqWidth;
newHeight = reqHeight;
}
break;
case CENTER_INSIDE://中心缩放
if (srcWidth / srcHeight < (float) reqWidth / reqHeight) {//原图比要求图高
scaleY = (float) reqHeight / srcHeight;//纵向图 纵向缩放
} else {
scaleY = (float) reqWidth / srcWidth;//横向图 横向缩放
}
// 不对图片进行放大,占用过多内存
if (scaleY > 1.0f)
scaleY = 1f;
scaleX = scaleY;
newWidth = (int) (srcWidth * scaleX);//newWidth = reqWidth
newHeight = (int) (srcHeight * scaleY);//newHeight = reqHeight
break;
case FIT_XY://横向纵向缩放
scaleX = reqWidth / srcWidth;
scaleY = reqHeight / srcHeight;
newWidth = reqWidth;
newHeight = reqHeight;
break;
default:
throw new Exception("不支持的缩放模式:" + imageScaleType);
}
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY);
matrix.postTranslate(-(srcWidth * scaleX - newWidth) / 2, -(srcHeight * scaleY - newHeight) / 2);//为了缩放后图片在显示的中心位置,所以缩放后要平移
Paint paint = new Paint();
paint.setAntiAlias(true);//去锯齿
paint.setFilterBitmap(true);//去锯齿加速显示
try {
Config config = src.getConfig();
if (config == null) {
config = Config.RGB_565;
}
Bitmap bitmap = Bitmap.createBitmap((int) newWidth, (int) newHeight, config);//新建空图
Canvas canvas = new Canvas(bitmap);//新建画布
canvas.drawBitmap(src, matrix, paint);//画画
return bitmap;
} catch (Throwable e) {
throw new Exception(e);
}
}