【问题标题】:Resizing a bitmap调整位图大小
【发布时间】:2012-03-23 22:26:09
【问题描述】:

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

【问题讨论】:

    标签: java android image bitmap resize


    【解决方案1】:

    我真的不这么认为,我做了几乎和你一样的方法,并且从android开发者页面中得到了这个想法......

    【讨论】:

      【解决方案2】:

      取决于您使用图像的方式。如果您只想在视图中显示位图,只需调用myImageView.setImageBitmap(bitmap) 并让框架调整它的大小。但是,如果您担心内存,首先扩展可能是一个好方法。

      【讨论】:

        【解决方案3】:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-06-06
          • 1970-01-01
          • 2015-06-04
          • 2021-10-12
          • 2012-10-02
          • 1970-01-01
          相关资源
          最近更新 更多