简体   繁体   English

缩小后 PNG 图像质量下降

[英]Png image losses quality after scale down

public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
    Paint paint=new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2, paint);

    return scaledBitmap;

}

    Bitmap largePngImage=BitmapFactory.decodeResource(getResources(),R.drawable.largeimage);
Bitmap scaledImage=resizeBitmap(largePngImage,266,80)

An original png image with dimension 2328 X 702. The scaled image has the desired width and height but the quality is bad.尺寸为 2328 X 702 的原始 png 图像。缩放后的图像具有所需的宽度和高度,但质量很差。

you can try with你可以试试

Bitmap bitmap;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(Bitmap, newWidth, newHeight, true);

You're currently just redrawing the bitmap in smaller size, which can sometimes 'destroy' the photo, while createScaledBitmap creates a new bitmap, scaled from an existing bitmap, when possible.您目前只是以较小的尺寸重新绘制位图,这有时会“破坏”照片,而createScaledBitmap会创建一个新位图,在可能的情况下从现有位图缩放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM