我正在缩小一些位图并得到低质量的结果。我尝试了 Bitmap#createScaledBitmap 和 Canvas#drawBitmap 方法。在第一个中设置过滤器参数和在第二个 Paint 对象中设置过滤器标志没有显示出明显的差异。
我尝试了基于这些问题的解决方案:
Bad image quality after resizing/scaling bitmap
Quality problems when resizing an image at runtime
Drawing scaled bitmaps on a SurfaceView -- no antialiasing
还有一些其他的。
正如某人在某处所说,createScaledBitmap 或 Paint 对象中的过滤器标志启用双三次插值算法。但是,这没有区别。使用 GIMP cubic(实际上是双三次)生成的图像比 Android 实现要好得多。
此处的 API 级别为 19。
最佳答案
Try this, May it help You.
public static Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth)
{
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
//get the resulting size after scaling
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
//figure out where we should translate to
float dx = (newWidth - scaledWidth) / 2;
float dy = (newHeight - scaledHeight) / 2;
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
matrix.postTranslate(dx, dy);
canvas.drawBitmap(source, matrix, null);
return dest;
}
关于Android:位图缩小质量低,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22617587/