title: Scaling image in a layout at runtime
body: <p>I need to know how to scale an image to fill a layout while respecting the images aspect ratio. I'm using the layout below. The ImageView that holds the drawable is "@+id/album_art". I originally thought I could programmatically get the width and height of "@+id/album_art_holder" at runtime and scale a drawable to the smaller dimension but I'm unable to obtain them. Any ideas?</p>
  
  <p>
  </p>
  
  <pre><code>&lt;LinearLayout
      android:id="@+id/player_actionbar"
      android:layout_width="fill_parent"
      android:layout_height="48dip"
      android:layout_alignParentTop="true"
      android:gravity="center_vertical"
      android:orientation="horizontal" &gt;
  
      &lt;ImageButton
          android:id="@+id/close"
          android:layout_width="56dip"
          android:layout_height="fill_parent"
          android:padding="4dip"
          android:scaleType="centerInside"
          android:background="@drawable/bg_actionbar_item"
          android:src="@drawable/icn_close" /&gt;
  
      &lt;View
          android:layout_width="1dip"
          android:layout_height="26dip"
          android:background="#ff444444" /&gt;
  
      &lt;TextView
          android:id="@+id/actionbar_title"
          android:layout_width="0dip"
          android:layout_height="fill_parent"
          android:layout_weight="1"
          android:textSize="18sp"
          android:textColor="#ffffffff"
          android:ellipsize="marquee"
          android:gravity="center_vertical"
          android:paddingLeft="6dip"
          android:paddingRight="6dip"
          android:singleLine="true"
          android:layout_marginLeft="8dip"
          android:text="@string/nowplaying_title" /&gt;
  
      &lt;ImageButton
          android:id="@+id/play_queue"
          android:layout_width="56dip"
          android:layout_height="fill_parent"
          android:padding="4dip"
          android:scaleType="centerInside"
          android:background="@drawable/bg_actionbar_item"
          android:src="@drawable/icn_play_queue" /&gt;
  
  &lt;/LinearLayout&gt;
  
  &lt;RelativeLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_above="@+id/player_controller"
      android:layout_below="@+id/player_actionbar" &gt;
  
      &lt;LinearLayout
          android:id="@+id/album_art_holder"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_above="@+id/player_info"
          android:padding="12dip"
          android:background="@null" &gt;
  
          &lt;ImageView
          android:id="@+id/album_art"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="@null" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:id="@+id/player_info"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_marginTop="10dip"
          android:gravity="center_horizontal"
          android:orientation="vertical" &gt;
  
          &lt;TextView
              android:id="@+id/track_number_text"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
              android:paddingTop="5dip"
              android:textSize="13dip"
              android:textStyle="normal"
              android:textColor="#ffa9a9a9"
              android:singleLine="true" /&gt;
  
          &lt;TextView 
              android:id="@+id/trackname"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="17dip"
              android:textStyle="bold"
              android:textColor="#ffffffff"
              android:ellipsize="marquee"
              android:singleLine="true" /&gt;
  
          &lt;TextView
              android:id="@+id/artist_and_album"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="13dip"
              android:textStyle="normal"
              android:textColor="#ffa9a9a9"
              android:ellipsize="marquee"
              android:singleLine="true" /&gt;
  
      &lt;/LinearLayout&gt;
  
  &lt;/RelativeLayout&gt;
  
  &lt;LinearLayout
      android:id="@+id/player_controller"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_alignParentBottom="true"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="40dip"
          android:paddingLeft="12dip"
          android:paddingRight="12dip"
          android:gravity="center_vertical"
          android:orientation="horizontal" &gt;
  
          &lt;TextView
              android:id="@+id/position_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:paddingRight="6dip" 
              android:textSize="12dip"
              android:textColor="#ffbbbbbb"
              android:gravity="right" /&gt;
  
          &lt;SeekBar
              android:id="@+id/seek_bar"
              android:layout_width="0dip"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:gravity="center_vertical"
              android:paddingLeft="9dip"
              android:paddingRight="9dip"
              android:progress="0"
              android:progressDrawable="@drawable/player_progress_drawable"
              android:thumb="@drawable/player_progress_thumb"
              android:thumbOffset="9dip"
              android:maxHeight="2dip"
              android:minHeight="2dip" /&gt;
  
          &lt;TextView
              android:id="@+id/duration_text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:paddingLeft="6dip"
              android:textSize="12dip"
              android:textColor="#ffbbbbbb"
              android:gravity="left" /&gt;
      &lt;/LinearLayout&gt;
  
      &lt;RelativeLayout
          android:layout_width="fill_parent"
          android:layout_height="60dip"
          android:orientation="horizontal" &gt;
  
          &lt;ImageButton
              android:id="@+id/shuffle_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:paddingBottom="20dip"
              android:paddingLeft="12dip"
              android:paddingRight="10dip"
              android:paddingTop="8dip"
              android:background="@null"
              android:src="@drawable/btn_player_shuffle_normal" /&gt;
  
          &lt;LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_toLeftOf="@+id/repeat_button"
              android:layout_toRightOf="@+id/shuffle_button"
              android:paddingLeft="10dip"
              android:paddingRight="10dip"
              android:gravity="center_vertical"
              android:orientation="horizontal" &gt;
  
              &lt;net.sourceforge.servestream.button.RepeatingImageButton
                  android:id="@+id/previous_button"
                  android:background="@null"
                  android:paddingBottom="20dip"
                  android:layout_width="0dip"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:src="@drawable/btn_player_prev" /&gt;
  
              &lt;ImageButton
                  android:id="@+id/play_pause_button"
                  android:layout_width="0dip"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:paddingBottom="20dip"
                  android:background="@null"
                  android:src="@drawable/btn_player_play" /&gt;
  
              &lt;net.sourceforge.servestream.button.RepeatingImageButton
                  android:id="@+id/next_button"
                  android:layout_width="0dip"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"
                  android:paddingBottom="20dip"
                  android:background="@null"
                  android:src="@drawable/btn_player_next" /&gt;
          &lt;/LinearLayout&gt;
  
          &lt;ImageButton
              android:id="@+id/repeat_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentRight="true"
              android:paddingBottom="20dip"
              android:paddingLeft="10dip"
              android:paddingRight="12dip"
              android:paddingTop="8dip"
              android:background="@null"
              android:src="@drawable/btn_player_repeat_normal" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>Here is a picture of the layout:</p>
  
  <p><img src="https://i.stack.imgur.com/DZ2uS.png" alt="enter image description here"></p>