说明
对于图片的加载ImageView中有scaleType类型(matrix,fitXY,fitStart,fitCenter,fitEnd,center,centerCrop,centerInside),这些可以满足大部分场景,但还是会有些特殊要求,这时候就需要自定义了。
使用示例
public static Bitmap cropBitmap(Bitmap bitmap, int width, int height) {
if (width == 0 || height == 0) {
return bitmap;
}
// 原图的宽,高
int w = bitmap.getWidth();
int h = bitmap.getHeight();
// 先计算原图的缩放比例
double scale = Math.min(1.0 * width / w,1.0 * height / h);
// 把原图进行缩放
Matrix matrix = new Matrix();
matrix.preScale((float) scale, (float) scale);
Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, false);
// 计算裁剪的真是宽度和高度
int cropWidth = Math.min(width, scaleBitmap.getWidth());
int cropHeight = Math.min(height, scaleBitmap.getHeight());
//起始剪切位置
int x = 0;
int y = scaleBitmap.getHeight() - cropHeight;
return Bitmap.createBitmap(scaleBitmap, x, y, cropWidth, cropHeight, null, false);
}
可以根据自己的需求进行处理更改






网友评论