How to Resize a Bitmap by Keeping the Same Aspect Ratio – Android

In Android, we use Bitmap class to store images. This class contains many functions to apply different operations on images. But there is no builtin method to resize images by keeping the same aspect ratio. Here, I am sharing that utility function to resize a bitmap by keeping the same aspect ratio.

Following is the Java solution. Those who are looking for the Kotlin solution can visit Resize Bitmap by Keeping the Same Aspect Ratio in Kotlin.

Utility Method to Resize a Bitmap by Keeping the Same Aspect Ratio

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);
               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);
               return result;

           }
       }
       catch (Exception e)
       {
           return source;
       }
   }

How It Works?

This method takes 2 parameters. The first parameter is the Bitmap you want to resize. The second parameter is the maximum length (either width or height) you want in the resultant Bitmap. It works in a way that if the image height is greater than the width, it will resize the image to match height with the maxLength by keeping the same aspect ratio. If width is greater than the height, it resizes the image to match width with the maxLength by keeping the same aspect ratio. But if the image dimensions are under the Max Length parameter value, the same bitmap is returned without applying any operation on it.

That’s it. Enjoy 🙂

Don’t forget to visit our Coding Articles & Tutorials Knowledge Base for other helping code snippets.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

4 thoughts on “How to Resize a Bitmap by Keeping the Same Aspect Ratio – Android

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: