调整位图大小

7
下面的代码调整位图大小并保持纵横比。我想知道是否有更有效的调整大小方式,因为我认为我正在编写已经在Android API中可用的代码。
private Bitmap resizeImage(Bitmap bitmap, int newSize){
    int width = bitmap.getWidth();
    int height = bitmap.getHeight(); 

    int newWidth = 0;
    int newHeight = 0;

    if(width > height){
        newWidth = newSize;
        newHeight = (newSize * height)/width;
    } else if(width < height){
        newHeight = newSize;
        newWidth = (newSize * width)/height;
    } else if (width == height){
        newHeight = newSize;
        newWidth = newSize;
    }

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
            width, height, matrix, true); 

    return resizedBitmap;
}
3个回答

17

0

我真的不这么认为,我做的方式几乎和你一样,并且从安卓开发者页面上得到了灵感...


0

这取决于您如何使用图像。如果您只想在视图中显示位图,请调用myImageView.setImageBitmap(bitmap)并让框架自动调整大小。但是,如果您关心内存问题,先进行缩放可能是一个不错的方法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接