title: Android resize bitmap keeping aspect ratio
body: <p>I have a custom view (1066 x 738), and I am passing a bitmap image (720x343). I want to scale the bitmap to fit in the custom view without exceeding the bound of the parent.</p>
  
  <p><img src="https://i.stack.imgur.com/OLaY2.jpg" alt="enter image description here"></p>
  
  <p>I want to achieve something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/7HVgj.jpg" alt="enter image description here"></p>
  
  <p>How should I calculate the bitmap size?</p>
  
  <p>How I calculate the new width/height:</p>
  
  <pre><code>    public static Bitmap getScaledBitmap(Bitmap b, int reqWidth, int reqHeight)
      {
          int bWidth = b.getWidth();
          int bHeight = b.getHeight();
  
          int nWidth = reqWidth;
          int nHeight = reqHeight;
  
          float parentRatio = (float) reqHeight / reqWidth;
  
          nHeight = bHeight;
          nWidth = (int) (reqWidth * parentRatio);
  
          return Bitmap.createScaledBitmap(b, nWidth, nHeight, true);
      }
  </code></pre>
  
  <p>But all I am achieving is this:</p>
  
  <p><img src="https://i.stack.imgur.com/76dZg.jpg" alt="enter image description here"></p>