I have a square image (though this problem also applies to rectangular images). I want to display the image as large as possible, stretching them if necessary, to fill their parents, while still maintaining the aspect ratio. The image is smaller than the ImageView. The problem is, I can't stretch the image and "match" the height and width of the ImageView.
This is my XML layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_marginTop="10dp"/>
<TextView android:id="@+id/name"
android:layout_below="@id/image"
android:layout_alignLeft="@id/image"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18dp"/>
<TextView android:id="@+id/name2"
android:layout_below="@id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dp"/>
</RelativeLayout>
I have used many combinations of fill_parent
, wrap_content
with multiple scaleTypes: fitCenter
, fitStart
, fitEnd
, centerInside
, and they all draw the images in the right aspect ratio, but none of them actually scale the images up and the ImageView itself, resulting in either the TextViews get pushed all the way down off the screen, blank spaces inside the ImageView, image not scaled, or image cropped.
I can't quite figure the right combination for this.
I had the same issue, android:adjustViewBounds not doing its job and having a padding at the top of the image. Into a relative layout that had match_parent values for the width and the height, I had:
I changed the Relative layout to a Linear layout with wrap_content values for width and height and now it is ok:
Use this code with view layout parameters as wrapcontent
android:adjustViewBounds="true"
Hope this it will work.
This xml code will work!. If you are specifying that your apps width is always same as the window, then the
android:adjustViewBounds="true"
will set the height respective to the ration of the image.Did you try to adjust it programmaticaly? I think it works really well if you calculate the height of your
TextView
s and adjust the height and width of the image based on this.These:
should resize the image and change the size of the bounds to fit the new image size. If it does not do that on your device post the image you are using and what device you are testing on.
Use this code :
android:scaleType="fitXY"
For more details about image scaling, look what's motioned in this article here
Summary:
center
centerCrop
centerInside
fitCenter
fitEnd
fitStart
fitXY
matrix
more details here new article