Android:位图向下缩放低质量

ecd*_*lma 5 android image bitmap scale

我正在缩小一些位图并获得低质量的结果.我尝试了Bitmap#createScaledBitmap和Canvas#drawBitmap方法.在第一个中设置过滤器参数,在第二个的Paint对象中设置过滤器标志,没有显示出明显的差异.

我试过在这些问题中建立的解决方案

调整大小/缩放位图后图像质量不佳

在运行时调整图像大小时的质量问题

在SurfaceView上绘制缩放位图 - 无抗锯齿

还有一些.

正如某人所说的那样,createScaledBitmap或Paint对象中的过滤器标志启用了双三次插值算法.但是,它没有任何区别.使用GIMP cubic(实际上是双三次),得到的图像比Android实现要好得多.

这里的API级别是19.

Hra*_*mar 0

试试这个,希望对你有帮助。

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;
    }
Run Code Online (Sandbox Code Playgroud)