简体   繁体   中英

how to change the size of Bitmap?

I make a picture and save it in the pre 'ArrayList Bitmap' and before saving them to a card I bring them in 'GridView'. But due to the fact that the picture 'Bitmap' in the large 'GridView' they are not displayed correctly. I have tried to do so before saving

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

But Android Studio underlines and says that I can not use these methods. If I understand correctly that this is connected with the API. Advise how to make a picture 50 by 50 pixels?

You can change the size of your bitmap by just modifying the width and the height. Just like this :

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;

}

If you already have a bitmap and want to resize it..

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){
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM