简体   繁体   English

调整位图大小时Android质量差

[英]Android poor quality when resize Bitmap

I have a problem when I resize one image bitmap in my application, the image quality deteriorates. 在我的应用程序中调整一个图像位图的大小时出现问题,图像质量下降。

My code for resize is as follows.. 我的大小调整代码如下。

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight,
            Config.ARGB_8888);

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

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.postScale(ratioX, ratioY, middleX, middleY);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);

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

Any good solution for resize bitmap ¿? 调整位图大小有什么好的办法吗?

Use this Code, I hope it will help you 使用此代码,希望对您有所帮助

    /***
 * This method is for aspect ratio means the image is set acc. to the aspect
 * ratio.
 * 
 * @param bmp
 *            bitmap passed
 * @param newWidth
 *            width you want to set for your image
 * @param newHeight
 *            hight you want to set for your image
 * @return bitmap
 */
public Bitmap resizeBitmap(Bitmap bmp, int newWidth, int newHeight) {
    Log.i(TAG,
            "height = " + bmp.getHeight() + "\nwidth = " + bmp.getWidth());
    if (bmp.getHeight() > newHeight || bmp.getWidth() > newWidth) {
        int originalWidth = bmp.getWidth();
        int originalHeight = bmp.getHeight();
        Log.i("TAG", "originalWidth = " + originalWidth
                + "\noriginalHeight = " + originalHeight);
        float inSampleSize;
        if (originalWidth > originalHeight) {
            inSampleSize = (float) newWidth / originalWidth;
        } else {
            inSampleSize = (float) newHeight / originalHeight;
        }
        newWidth = Math.round(originalWidth * inSampleSize);
        newHeight = Math.round(originalHeight * inSampleSize);
        Log.i("", "newWidth = " + newWidth + "\nnewHeight = " + newHeight);
        bitmap = Bitmap.createScaledBitmap(bmp, newWidth, newHeight, true);
    } else {
        bitmap = bmp;
        Log.i("", "bitmapWidth = " + bitmap.getWidth()
                + "\nbitmapHeight = " + bitmap.getHeight());
    }
    return bitmap;
}

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

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