How to Resize a Bitmap in Android?

后端 未结 16 1982
有刺的猬
有刺的猬 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:54

    Try this: This function resizes a bitmap proportionally. When the last parameter is set to "X" the newDimensionXorY is treated as s new width and when set to "Y" a new height.

    public Bitmap getProportionalBitmap(Bitmap bitmap, 
                                        int newDimensionXorY, 
                                        String XorY) {
        if (bitmap == null) {
            return null;
        }
    
        float xyRatio = 0;
        int newWidth = 0;
        int newHeight = 0;
    
        if (XorY.toLowerCase().equals("x")) {
            xyRatio = (float) newDimensionXorY / bitmap.getWidth();
            newHeight = (int) (bitmap.getHeight() * xyRatio);
            bitmap = Bitmap.createScaledBitmap(
                bitmap, newDimensionXorY, newHeight, true);
        } else if (XorY.toLowerCase().equals("y")) {
            xyRatio = (float) newDimensionXorY / bitmap.getHeight();
            newWidth = (int) (bitmap.getWidth() * xyRatio);
            bitmap = Bitmap.createScaledBitmap(
                bitmap, newWidth, newDimensionXorY, true);
        }
        return bitmap;
    }
    
    0 讨论(0)
  • 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);
    
    0 讨论(0)
  • 2020-11-22 03:58

    try this this code :

    BitmapDrawable drawable = (BitmapDrawable) imgview.getDrawable();
    Bitmap bmp = drawable.getBitmap();
    Bitmap b = Bitmap.createScaledBitmap(bmp, 120, 120, false);
    

    I hope it's useful.

    0 讨论(0)
  • 2020-11-22 04:01

    Although the accepted answer is correct, it doesn't resize Bitmap by keeping the same Aspect Ratio. If you are looking for a method to resize Bitmap by keeping the same aspect ratio you can use the following utility function. The usage details and explanation of the function are present at this link.

    public static Bitmap resizeBitmap(Bitmap source, int maxLength) {
           try {
               if (source.getHeight() >= source.getWidth()) {
                   int targetHeight = maxLength;
                   if (source.getHeight() <= targetHeight) { // if image already smaller than the required height
                       return source;
                   }
    
                   double aspectRatio = (double) source.getWidth() / (double) source.getHeight();
                   int targetWidth = (int) (targetHeight * aspectRatio);
    
                   Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                   if (result != source) {
                   }
                   return result;
               } else {
                   int targetWidth = maxLength;
    
                   if (source.getWidth() <= targetWidth) { // if image already smaller than the required height
                       return source;
                   }
    
                   double aspectRatio = ((double) source.getHeight()) / ((double) source.getWidth());
                   int targetHeight = (int) (targetWidth * aspectRatio);
    
                   Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                   if (result != source) {
                   }
                   return result;
    
               }
           }
           catch (Exception e)
           {
               return source;
           }
       }
    
    0 讨论(0)
提交回复
热议问题