Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageView fills parent's width OR height, but maintains aspect ratio

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.

like image 423
garbagecollector Avatar asked May 31 '12 01:05

garbagecollector


People also ask

How do I resize an image in ImageView to keep the aspect ratio?

However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="..." . src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView.

How to determine the aspect ratio of an image?

Calculate the relationship between the width (the first number) and the height (the second number). For example: Images that are 1,600 pixels x 900 pixels or 3,200 pixels x 1,800 pixels are in the 16:9 aspect ratio. Images that are 1,600 pixels x 1,600 pixels or 3,200 pixels x 3,200 pixels are in the 1:1 aspect ratio.

How do I make an image fit in ImageView?

try adding android:scaleType="fitXY" to your ImageView . This will modify the aspect ratio if the original image is not squared. fitXY will almost always change the aspect ratio of the image.

What is adjust view bounds in android?

android:adjustViewBounds—If set to true, the attribute adjusts the bounds of the ImageView control to maintain the aspect ratio of the image displayed through it. android:resizeMode—The resizeMode attribute is used to make a control resizable so we can resize it horizontally, vertically, or around both axes.


2 Answers

These:

android:layout_height="wrap_content" android:scaleType="fitStart" android:adjustViewBounds="true" 

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.

like image 172
FoamyGuy Avatar answered Oct 11 '22 00:10

FoamyGuy


Use this code : android:scaleType="fitXY"

For more details about image scaling, look what's motioned in this article here

Summary:

  • center

Center the image in the view, but perform no scaling

enter image description here

  • centerCrop

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or larger than the corresponding dimension of the view (minus padding). The image is then centered in the view

enter image description here

  • centerInside

Scale the image uniformly (maintain the image's aspect ratio) so that both dimensions (width and height) of the image will be equal to or less than the corresponding dimension of the view (minus padding)

enter image description here

  • fitCenter

enter image description here

  • fitEnd

enter image description here

  • fitStart

enter image description here

  • fitXY

enter image description here

  • matrix

Scale using the image matrix when drawing

enter image description here

more details here new article

like image 23
Context Avatar answered Oct 11 '22 00:10

Context