How to Resize a Bitmap in Android?

后端 未结 16 2326
有刺的猬
有刺的猬 2020-11-22 03:13

I have a bitmap taken of a Base64 String from my remote database, (encodedImage is the string representing the image with Base64):

profileImage          


        
16条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 03:56

    Scale a bitmap with a target maximum size and width, while maintaining aspect ratio:

    int maxHeight = 2000;
    int maxWidth = 2000;    
    float scale = Math.min(((float)maxHeight / bitmap.getWidth()), ((float)maxWidth / bitmap.getHeight()));
    
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);
    
    bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    

提交回复
热议问题