2012-03-06 190 views
5

下面的代码调整位图大小并保持宽高比。 我想知道是否有更有效的调整大小的方法,因为我的想法是我正在编写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; 
} 

回答

0

我真的不这么认为,我几乎做了同样的方式为你,并得到了来自Android开发者页面的想法...

0

取决于你如何使用图像。如果您只想在视图中显示位图,只需调用myImageView.setImageBitmap(bitmap)并让框架调整它的大小。但如果你关心记忆,首先进行缩放可能是一个好方法。