【问题标题】:android resize bitmap auto heightandroid调整位图自动高度
【发布时间】:2016-11-07 10:56:30
【问题描述】:

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

     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;
}

但我想调整位图的大小,例如宽度 800 像素和自动高度。是否有可能在 android 中解决这个问题? 谢谢

【问题讨论】:

  • refer this link 我也从这个链接得到了同样问题的解决方案

标签: android bitmap android-imageview android-bitmap


【解决方案1】:
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

【讨论】:

    【解决方案2】:
    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 2015-04-09
      • 1970-01-01
      • 2017-04-18
      • 2014-01-14
      • 2011-08-23
      • 2012-08-21
      相关资源
      最近更新 更多