title: Android - Keep aspect ratio on background image on default Button
body: <p>Is there any way to maintain the aspect ratio of a drawable when applied as background on a <code>Button</code> (cannot use <code>ImageButton</code> (need possibility for text on the <code>Button</code>), cannot use <code>setCompoundDrawables()</code>, because that also doesn't meet my requirements (I made my own class to support adding backgrounds while still retaining the original android default button, plus I need to set te background in the middle of the button, not left, right, top or bottom)).</p>
  
  <p>I tried a solution with a <code>ScaleDrawable</code>, but either I'm doing something wrong or it doesn't work:</p>
  
  <pre><code>Bitmap scaled = Bitmap.createScaledBitmap(original, (int)newBmpWidth, (int)newBmpHeight, false);
  BitmapDrawable bd = new BitmapDrawable(scaled);
  ScaleDrawable sd = new ScaleDrawable(bd, Gravity.CENTER, a, b);
  //for a and b I tried lots of different values, ranging from 0.07f to 1000
  sd.setLevel(1);
  </code></pre>
  
  <p>Then I use the <code>ScaleDrawable</code> as background, but it get stretched anyway.</p>
  
  <p>I also tried a <code>InsetDrawable</code>, but again, either I'm doing something wrong or it doesn't work, expecting the first:</p>
  
  <pre><code>BitmapDrawable mBitmapDrawable = new BitmapDrawable(mBitmap);
  InsetDrawable mInsetDrawable = new InsetDrawable(mBitmapDrawable, mLeftOffset, 0, mRightOffset, 0);
  </code></pre>
  
  <p>Then I use the <code>InsetDrawable</code> as background, but this also get stretched anyway.</p>
  
  <p><strong>Is there any way to apply a background but maintain aspect ratio on a <code>Button</code>?</strong> I can find plenty answers for <code>ImageButtons</code>, but sadly a <code>Button</code> does not have a <code>setImageBitmap()</code> or <code>setScaleType()</code> method.</p>