看如下代码段
/**
* 改变bitmap的大小
*
* @param mBitmamp
* @param newWidth
* @param newHeight
* @return
*/
private static Bitmap changeBitmapSize(Bitmap mBitmamp, int newWidth, int newHeight) {
int oldWidth = mBitmamp.getWidth();
int oldHeight = mBitmamp.getHeight();
//计算缩放比例
float scaleWidth = newWidth / oldWidth;
float scaleHeight = newHeight / oldHeight;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
//得到新的图片
mBitmamp = Bitmap.createBitmap(mBitmamp, 0, 0, oldWidth, oldHeight, matrix, true);
return mBitmamp;
}
程序运行时会报错,抛出 :Caused by: java.lang.IllegalArgumentException: width and height must be > 0
解决方案:强转为float类型,将上面两行代码替换成:
float scaleWidth = ((float)newWidth / oldWidth);
float scaleHeight = ((float)newHeight / oldHeight);
解决!