【问题标题】:how to change the size of Bitmap?如何更改位图的大小?
【发布时间】:2015-12-28 10:15:24
【问题描述】:

我制作了一张图片并将其保存在前“ArrayList Bitmap”中,在将它们保存到卡片之前,我将它们放入“GridView”中。但由于大 'GridView' 中的图片 'Bitmap' 无法正确显示。在保存之前我已经尝试这样做了

bitmap.setHeight();
bitmap.setWidth();

但 Android Studio 强调并说我不能使用这些方法。如果我正确理解这与 API 相关。 请教如何制作 50 x 50 像素的图片?

【问题讨论】:

    标签: java android image bitmap camera


    【解决方案1】:

    您可以通过修改宽度和高度来更改位图的大小。就像这样:

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
    
        int width = bm.getWidth();
        int height = bm.getHeight();
    
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
    
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
    
        Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
    
        return resizedBitmap;
    
    }
    

    【讨论】:

    • 我用另一种方式 Bitmap.createScaledBitmap(yourBitmap, width, height, true)
    【解决方案2】:

    如果您已经有位图并想要调整它的大小..

    Bitmap bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length); //Original Initialization 
    try { 
     resizedBitmap = Bitmap.createScaledBitmap(bMap, 240, 320, false);// Here I have set it To 240 width and 320 Height
        }
    catch (Exception e){
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-10
      • 2014-10-31
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 2014-03-25
      • 1970-01-01
      相关资源
      最近更新 更多