[英]Android poor quality when resize Bitmap
在我的应用程序中调整一个图像位图的大小时出现问题,图像质量下降。
我的大小调整代码如下。
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight,
Config.ARGB_8888);
float ratioX = newWidth / (float) bm.getWidth();
float ratioY = newHeight / (float) bm.getHeight();
float middleX = newWidth / 2.0f;
float middleY = newHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.postScale(ratioX, ratioY, middleX, middleY);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bm, middleX - bm.getWidth() / 2,
middleY - bm.getHeight() / 2, paint);
return scaledBitmap;
}
调整位图大小有什么好的办法吗?
使用此代码,希望对您有所帮助
/***
* This method is for aspect ratio means the image is set acc. to the aspect
* ratio.
*
* @param bmp
* bitmap passed
* @param newWidth
* width you want to set for your image
* @param newHeight
* hight you want to set for your image
* @return bitmap
*/
public Bitmap resizeBitmap(Bitmap bmp, int newWidth, int newHeight) {
Log.i(TAG,
"height = " + bmp.getHeight() + "\nwidth = " + bmp.getWidth());
if (bmp.getHeight() > newHeight || bmp.getWidth() > newWidth) {
int originalWidth = bmp.getWidth();
int originalHeight = bmp.getHeight();
Log.i("TAG", "originalWidth = " + originalWidth
+ "\noriginalHeight = " + originalHeight);
float inSampleSize;
if (originalWidth > originalHeight) {
inSampleSize = (float) newWidth / originalWidth;
} else {
inSampleSize = (float) newHeight / originalHeight;
}
newWidth = Math.round(originalWidth * inSampleSize);
newHeight = Math.round(originalHeight * inSampleSize);
Log.i("", "newWidth = " + newWidth + "\nnewHeight = " + newHeight);
bitmap = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, true);
} else {
bitmap = bmp;
Log.i("", "bitmapWidth = " + bitmap.getWidth()
+ "\nbitmapHeight = " + bitmap.getHeight());
}
return bitmap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.