title: Scaled Bitmap maintaining aspect ratio
body: <p>I would like to scale a <code>Bitmap</code> to a runtime dependant width and height, where the aspect ratio is maintained and the <code>Bitmap</code> fills the entire width and centers the image vertically, either cropping the excess or filling in the gap with 0 alpha pixels.</p>
  
  <p>I'm currently redrawing the bitmap myself by creating a <code>Bitmap</code> of all 0 alpha pixels and drawing the image <code>Bitmap</code> on top of it, scaling to the exact specified width and maintaining the aspect ratio, however, it ends up losing/screwing up the pixel data.</p>
  
  <p>Here is how I'm doing it:</p>
  
  <pre><code>Bitmap background = Bitmap.createBitmap((int)width, (int)height, Config.ARGB_8888);
  float originalWidth = originalImage.getWidth(), originalHeight = originalImage.getHeight();
  Canvas canvas = new Canvas(background);
  float scale = width/originalWidth;
  float xTranslation = 0.0f, yTranslation = (height - originalHeight * scale)/2.0f;
  Matrix transformation = new Matrix();
  transformation.postTranslate(xTranslation, yTranslation);
  transformation.preScale(scale, scale);
  canvas.drawBitmap(originalImage, transformation, null);
  return background;
  </code></pre>
  
  <p>Is there a library out there or some better code that can do this better? I would like the image to look as crisp as possible, but I knew that my function wouldn't provide a great result.</p>
  
  <p>I know I could have the image stay fine by using integer scaling, instead of float scaling, but I need the width to be 100% filled.</p>
  
  <p>Also, I know about an <code>ImageView</code>'s <code>Gravity.CENTER_CROP</code> capability, however, that also uses integer scaling, so it cuts off the width of the image when it shouldn't.</p>