在Android中调整图像大小时质量低?

时间:2017-03-09 05:31:38

标签: android screen-size resize-image

这是我的代码。

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        img = (ImageView)findViewById(R.id.resizedView);
        BitmapFactory.Options options = new BitmapFactory.Options ();
        options.inScaled = false;
        Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.tutimage1, options);
        Bitmap bmp = getResizedBitmap(largeIcon, 200);
        img.setImageBitmap(bmp);
    }
 public Bitmap getResizedBitmap(Bitmap image, int maxSize)
    {
        int width = image.getWidth();
        int height = image.getHeight();

        float bitmapRatio = (float)width / (float) height;
        if (bitmapRatio > 1) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }

       // return getResizedBitmapWithQuality(image, width, height);
        return  Bitmap.createScaledBitmap(image, width, height, false);
    }

所以我尝试研究和测试但结果仍然相同。

enter image description here enter image description here

这是因为所需的尺寸太小了吗?

原始尺寸为1120 x 2048,结果尺寸为110 x 200。

1 个答案:

答案 0 :(得分:0)

使用此功能可提高图像位图质量

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

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}