Android处理Bitmap使其能够不失真等比缩放裁剪后显示在ImageView上

时间:2021-07-13 00:23:24

Android开发过程中,我们有时需要动态得显示一些图片,并且这些图片的大小差距会十分大,如果需求并不是需要图片完整显示,但是需要不失真,并且要图片中间部分的情况下,我们需要做一系列处理,因为这个时候ImageView的各种scale type都不适用。具体步骤详见下面代码,大家也可以直接拷过去作为工具类使用

 /**
* 获取正确缩放裁剪适应IamgeView的Bitmap
* @param imageView
* @param bitmap
* @return
*/
public static Bitmap createFitBitmap(ImageView imageView, Bitmap bitmap) {
Log.i(TAG, "createFitBitmap<---------------------");
int widthTarget = imageView.getWidth();
int heightTarget = imageView.getHeight();
int widthBitmap = bitmap.getWidth();
int heightBitmap = bitmap.getHeight();
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
Bitmap result = null;
if( widthBitmap >= widthTarget && heightBitmap >= heightTarget ){
result = createLargeToSmallBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
}
else if( widthBitmap >= widthTarget && heightBitmap < heightTarget ){
Bitmap temp = createLargeWidthToEqualHeightBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
}
else if( widthBitmap < widthTarget && heightBitmap >= heightTarget ){
Bitmap temp = createLargeHeightToEqualWidthBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
}
else{
Bitmap temp = createSmallToEqualBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
result = createFitBitmap(imageView, temp);
}
Log.i(TAG, "createFitBitmap--------------------->");
return result;
} private static Bitmap createLargeToSmallBitmap( int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){
Log.i(TAG, "createLargeToSmallBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
int x = (widthBitmap - widthTarget) / 2;
int y = (heightBitmap - heightTarget) / 2;
Log.i(TAG, "createLargeToSmallBitmap--------------------->");
return Bitmap.createBitmap(bitmap, x, y, widthTarget, heightTarget);
} private static Bitmap createLargeWidthToEqualHeightBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){ Log.i(TAG, "createLargeWidthToEqualHeightBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
double scale = ( heightTarget * 1.0 ) / heightBitmap;
Log.i(TAG, "createLargeWidthToEqualHeightBitmap--------------------->");
return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale) , heightTarget, false);
} private static Bitmap createLargeHeightToEqualWidthBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){ Log.i(TAG, "createLargeHeightToEqualWidthBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
double scale = ( widthTarget * 1.0 ) / widthBitmap;
Log.i(TAG, "createLargeHeightToEqualWidthBitmap--------------------->");
return Bitmap.createScaledBitmap(bitmap, widthTarget , (int)(heightTarget * scale), false);
} private static Bitmap createSmallToEqualBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){ Log.i(TAG, "createSmallToEqualBitmap<---------------------");
Log.i(TAG, "widthTarget = " + widthTarget );
Log.i(TAG, "heightTarget = " + heightTarget );
Log.i(TAG, "widthBitmap = " + widthBitmap );
Log.i(TAG, "heightBitmap = " + heightBitmap );
double scaleWidth = ( widthTarget * 1.0 ) / widthBitmap;
double scaleHeight = ( heightTarget * 1.0 ) / heightBitmap;
double scale = Math.min(scaleWidth, scaleHeight);
Log.i(TAG, "createSmallToEqualBitmap--------------------->");
return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale), (int)(heightBitmap * scale), false);
}