android调整位图自动高度

时间:2016-11-07 10:56:31

标签: android bitmap android-imageview android-bitmap

我尝试从gallery重新调整位图。我写了一些代码,我只能调整位图的静态宽度和高度。这是源

     public Bitmap getResizedBitmap(Bitmap bm, int newWidth) {

    float aspectRatio = (float)bm.getWidth() / (float) bm.getHeight();
    int height = Math.round(newWidth / aspectRatio); //based on width it gives height
    Matrix matrix = new Matrix();

    matrix.postScale(newWidth, height);

    Bitmap resizedBitmap = Bitmap.createBitmap(
            bm, 0, 0, newWidth, height, matrix, false);
    return resizedBitmap;
}

但我想重新调整位图,例如宽度800px和自动高度。它有可能在android中解决这个问题吗? 感谢

2 个答案:

答案 0 :(得分:3)

float aspectRatio = (float)Image.getWidth() / (float) Image.getHeight();
int width = 200;    //your width
int height = Math.round(width / aspectRatio); //based on width it gives height

答案 1 :(得分:1)

public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap resizedBitmap;


try {
         resizedBitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight());
            }catch (Exception e){
                e.printStackTrace();
                boolean_image = true;
                return "";

            }

return resizedBitmap;
}