title: how to keep image aspect ratio on API < 18
body: <p>I have the next xml layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#fff"
  android:gravity="center_vertical"
  android:orientation="vertical" &gt;
  
  &lt;RelativeLayout
      android:id="@+id/my_frame"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="#fff"
      android:gravity="center_vertical" &gt;
  
      &lt;ImageView
          android:id="@+id/image_areas"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:adjustViewBounds="true"
          android:background="#9a05e7"
          android:scaleType="fitXY"
          android:src="@drawable/mapa_mask"
          android:visibility="invisible"
          tools:ignore="ContentDescription" /&gt;
  
      &lt;ImageView
          android:id="@+id/image"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentRight="true"
          android:adjustViewBounds="true"
          android:background="#fff"
          android:scaleType="fitXY"
          android:src="@drawable/mapa_default"
          android:visibility="visible"
          tools:ignore="ContentDescription" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>There are two images "overlayed".</p>
  
  <p>If I test the App on devices that have installed Android 4.3 or more ( >= API 18) the image aspect ratio remains perfect. But if I test the App on devices between API >= 14 API &lt;= 17 the image is showed little "height narrow".</p>
  
  <p>I used the next to set the correct sizes but it didn't work:</p>
  
  <pre><code>        // load the original BitMap (768 x 583 px)
      Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
              R.drawable.mapa_default);
              // iv is an ImageView referenced in onCreate (mapa_default)
      int width = bitmapOrg.getWidth();
      int height = bitmapOrg.getHeight();
      int newWidth = iv.getWidth();
      int newHeight = heightRealImg;
  
      // calculate the scale
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;
  
      // create a matrix for the manipulation
      Matrix matrix = new Matrix();
      // resize the bit map
      matrix.postScale(scaleWidth, scaleHeight);
  
      // recreate the new Bitmap
      Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
              height, matrix, true);
  
      // make a Drawable from Bitmap to allow to set the BitMap
      // to the ImageView, ImageButton or what ever
      BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
  
      // set the Drawable on the ImageView
      iv.setImageDrawable(bmd);
  
      // center the Image
      iv.setScaleType(ScaleType.FIT_XY);
      iv.setAdjustViewBounds(true);
  </code></pre>
  
  <p>This crash because the iv.getWidth returns 0. I think this is because the layout has not finished loading yet.</p>
  
  <p>How can I keep the image aspect ratio??
  Thanks</p>
  
  <p><strong>My solution:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/my_frame"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="#fff"
  android:gravity="center_vertical" &gt;
  
  &lt;ImageView
      android:id="@+id/image_areas"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:adjustViewBounds="true"
      android:background="#9a05e7"
      android:scaleType="fitXY"
      android:src="@drawable/mapacyl_mask"
      android:visibility="invisible"
      tools:ignore="ContentDescription" /&gt;
  
  &lt;ImageView
      android:id="@+id/image"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="#fff"
      android:scaleType="fitXY"
      android:src="@drawable/mapacyl"
      android:visibility="visible"
      tools:ignore="ContentDescription" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>In this case I manage the touch of "all" screen and I don't need get the width and height of the image or modify these values. And with fitXY the image expands and adapts to all screen sizes, ALTHOUGHT the ratio doesn't keep.</p>
  
  <p>Thanks a lot @Doctoror Drive</p>