Android:位图缩小质量低

标签 android image bitmap scale

我正在缩小一些位图并得到低质量的结果。我尝试了 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/

相关文章:

java - 在 Netbeans 中使用位图字体

java - 创建 fragment 会导致应用程序崩溃

android - layout-sw600dp 不适用于模拟器吗?

android - 如何在 Android 4 中隐藏状态栏

image - 如何优化图像解码的Delphi代码?

html - css 图像不填充 div

android - ButterKnife 和 AnnotationProcessor

image - Matlab图像插值interp2()

c# - 字符转换为位图图像 C#

c - 如何在C中将QRcode保存为图像