开发中有需求,类似百度贴吧发图时,图片的备选面板,对于特别长的图,如果不加以处理因图片太大,会出现加载不出来,解决策略是对于长图先根据宽度缩放比,等比缩放到期望尺寸,注意这样等比缩放,是防止图片变形。
缩放好后根据需要高度截取需要的高度图片。代码如下
/**
* 根据期望宽高度截取图片
*
* @param sourceBitmap
* @param targetWidth 目标宽度
* @param targetHeight 目标高度
* @return
*/
public static Bitmap scaledBitmapByWidth(Bitmap sourceBitmap, int targetWidth, int targetHeight) {
int bitmapWidth = sourceBitmap.getWidth();
int bitmapHeight = sourceBitmap.getHeight();
if (bitmapHeight == 0) {
return null;
}
float widthRatio = targetWidth * 1.0f / bitmapWidth * 1.0f;
Bitmap scaleBitmap = null;
try {
scaleBitmap = Bitmap.createScaledBitmap(sourceBitmap, targetWidth, (int) (bitmapHeight * widthRatio), true); //先根据宽度比,等比缩放,然后截取需要的部分
} catch (Exception e) {
LogUtil.e("--e is " + e);
return null;
}
int sourceWidth = scaleBitmap.getWidth();
int soureHeight = scaleBitmap.getHeight();
LogUtil.d("--width radio is " + widthRatio + " targeWidth is " + targetWidth + " bitmapWidth is " + bitmapWidth);
int needHeight = targetHeight > soureHeight ? soureHeight : targetHeight;//如果需要的高度比图片原图的高度大,则用原图高度,否则使用期望的高度
Bitmap resultBitmap = null;
try {
resultBitmap = Bitmap.createBitmap(scaleBitmap, 0, 0, sourceWidth, needHeight);
scaleBitmap.recycle();
} catch (Exception e) {
LogUtil.e("----e is " + e);
}
return resultBitmap;
}
探究下Bitmap.createScaledBitmap的源码。
/**
* Creates a new bitmap, scaled from an existing bitmap, when possible. If the
* specified width and height are the same as the current width and height of
* the source bitmap, the source bitmap is returned and no new bitmap is
* created.
*
* @param src The source bitmap.
* @param dstWidth The new bitmap's desired width.(目标宽度)
* @param dstHeight The new bitmap's desired height.(目标高度)
* @param filter true if the source should be filtered.
* @return The new scaled bitmap or the source bitmap if no scaling is required.
* @throws IllegalArgumentException if width is <= 0, or height is <= 0
*/
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight,
boolean filter) {
Matrix m;
synchronized (Bitmap.class) {
// small pool of just 1 matrix
m = sScaleMatrix;
sScaleMatrix = null;
}
if (m == null) {
m = new Matrix();
}
final int width = src.getWidth();
final int height = src.getHeight();
final float sx = dstWidth / (float)width;
final float sy = dstHeight / (float)height;
m.setScale(sx, sy);//(此处直接使用matrix.setScale)
Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
synchronized (Bitmap.class) {
// do we need to check for null? why not just assign everytime?
if (sScaleMatrix == null) {
sScaleMatrix = m;
}
}
return b;
}
Bitmap.createBitmap的参数含义
/
* Returns an immutable bitmap from the specified subset of the source
* bitmap. The new bitmap may be the same object as source, or a copy may
* have been made. It is initialized with the same density as the original
* bitmap.
*
* @param source The bitmap we are subsetting
* @param x The x coordinate of the first pixel in source(x方向上从哪个像素点开始截取图片)
* @param y The y coordinate of the first pixel in source(y方向上从哪个像素点开始截取图片)
* @param width The number of pixels in each row
* @param height The number of rows
* @return A copy of a subset of the source bitmap or the source bitmap itself.
* @throws IllegalArgumentException if the x, y, width, height values are
* outside of the dimensions of the source bitmap, or width is <= 0,
* or height is <= 0
*/
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height) {
return createBitmap(source, x, y, width, height, null, false);
}
width参数注意不应该超过sourceBitmap的getWidth大小,否则会报错。
height参数注意不应该超过sourceBitmap的getHeight大小,若height=sourece.getHeight,则截取整个bitmap的高度,若height=source.getHeight()/2,则截取图片高度的一半,截取到的bitmap只能展示原图的一半。