Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imageview height as the wrap_content of the shown image

I want the width of an ImageView to be set by the parent and the height should be aspect proportional. The reasons for this is that next is shown a TextView that I want to place just under the ImageView.

I can get image to show correctly using

android:layout_width="fill_parent" android:layout_height="fill_parent" 

however the ImageView height become the parent height which is much larger than that of the shown stretched image. One idea was to make parent smaller vertically, but.. there I don't yet know the stretched image size.

The following doesn't work because a small image is not filled up horizontally.

android:layout_width="fill_parent" android:layout_height="wrap_content" 

Messing around with

android:layout_height="wrap_content" 

for the RelativeLayout surrounding it all does not help. Also tried FrameLayout and LinearLayout and failed.

Any ideas?

like image 461
Scott Johansen Avatar asked Oct 29 '13 17:10

Scott Johansen


People also ask

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.

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.

What is the ImageView?

Displays image resources, for example Bitmap or Drawable resources. ImageView is also commonly used to apply tints to an image and handle image scaling.

Which attribute is used to set an image in ImageView?

src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.


2 Answers

You have to set adjustViewBounds to true.

imageView.setAdjustViewBounds(true); 
like image 54
Kit Avatar answered Oct 01 '22 02:10

Kit


There is two case if your actual image size is equal or grater than your ImageView width and heigh then you can use adjustViewBounds property and if your actual image size is less than ImageView width and height than use scaleType property to shown image in ImageView based on your requirement.

1.Actual image size is equal or grater than ImageView required width and height.

<ImageView     android:layout_width="match_parent"      android:layout_height="wrap_content"     android:adjustViewBounds="true"     android:src="@drawable/ic_launcher"/> 

2.Actual image size is less than ImageView required width and height.

<ImageView     android:layout_width="match_parent"      android:layout_height="wrap_content"     android:scaleType="fitXY"     android:src="@drawable/ic_launcher"/> 
like image 40
Haresh Chhelana Avatar answered Oct 01 '22 04:10

Haresh Chhelana