title: How to align ImageView to the bottom, fill its width and height, and keep its aspect ratio?
body: <h2>Background</h2>
  
  <p>There are various XML attributes for ImageView to scale its content , and various layout views that allow to place views and set their sizes.</p>
  
  <p>However, I can't figure out how to scale an imageView nicely on some cases.</p>
  
  <p>An example of it is to put the ImageView on the bottom (of a frameLayout, for example) , scale its width as much as possible, and still keep the aspect ratio (without cropping).</p>
  
  <h2>The problem</h2>
  
  <p>None of the combinations I've found worked, including various ScaleType values, adjustViewBounds, gravity ,...</p>
  
  <p>It seems as if ImageView misses some important ScaleType values. </p>
  
  <h2>What I've tried</h2>
  
  <p>So far, the only solution that worked for me, is to set the imageView to the bottom (using the gravity set to bottom and center-horizontal), and use code, similar to this:</p>
  
  <pre><code>final ImageView logoBackgroundImageView = ...;
  final LayoutParams layoutParams = logoBackgroundImageView.getLayoutParams();
  final Options bitmapOptions = ImageService.getBitmapOptions(getResources(), R.drawable.splash_logo);
  final int screenWidth = getResources().getDisplayMetrics().widthPixels;
  layoutParams.height = bitmapOptions.outHeight * screenWidth / bitmapOptions.outWidth;
  logoBackgroundImageView.setLayoutParams(layoutParams);
  </code></pre>
  
  <p>This usually works , and doesn't need much changes to make it work for when it's not in full screen, but I wonder if there is a simpler way.</p>
  
  <p>It might not work in case the height gets to be too large, so you might want to change it so that if that's happening, you set the width to be smaller to allow everything fit the container.</p>
  
  <h2>The question</h2>
  
  <p>Is there a solution or a library that fixes the various issues related to the ImageView ? </p>
  
  <hr>
  
  <p>EDIT: here's a sample image of what I'm trying to do, this time, within a vertical LinearLayout that bounds the ImageView in the middle, between 2 other views:</p>
  
  <p><img src="https://i.stack.imgur.com/3oqgk.png" alt="enter image description here"></p>
  
  <p>As you can see, on some (or all?) of the previews, the middle image is aligned to the right instead of staying on the middle (horizontal).</p>
  
  <p>I want it to take all the space it got and scale (keeping aspect ratio), and yet be aligned to the bottom of the space that it has.</p>
  
  <p>here's a sample XML of one combination I've tried :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="#FF339AE2"&gt;
  
      &lt;View
          android:layout_width="match_parent"
          android:id="@+id/topView"
          android:layout_alignParentTop="true"
          android:layout_height="100dp"
          android:background="#FF00ff00"
          /&gt;
  
  
      &lt;FrameLayout
          android:layout_below="@+id/topView"
          android:layout_width="match_parent"
          android:layout_centerHorizontal="true"
          android:layout_above="@+id/bottomView"
          android:layout_height="match_parent"&gt;
  
          &lt;ImageView
              android:layout_width="match_parent"
              android:background="#FFffff00"
              android:layout_gravity="bottom|center_horizontal"
              android:src="@android:drawable/sym_def_app_icon"
              android:scaleType="fitEnd"
              android:layout_height="match_parent"
              /&gt;
      &lt;/FrameLayout&gt;
  
      &lt;View
          android:background="#FFff0000"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:id="@+id/bottomView"
          android:layout_width="match_parent"
          android:layout_height="100dp"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Again, note that I've tried multiple combinations, but none of them worked, so if you suggest something to change in the XML, please try it out first. Also note that the above is a POC (to make it easy to read).</p>
  
  <p>BTW, the workaround I've suggested works on some (or most?) cases, but not all. You can notice it by just rotating your device. Maybe there is a way to fix it, or extend from ImageView to provide the extra case.</p>