Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create an ImageView that fills the parent height and displays an Image as big as possible?

I have an ImageView that is defined in the following way:

 <ImageView   android:id="@+id/cover_view"   android:layout_width="wrap_content"   android:layout_height="match_parent"   android:layout_below="@id/title"   android:layout_above="@id/divider"   android:adjustViewBounds="true"   android:src="@drawable/image_placeholder"   android:scaleType="fitStart"/> 

Now after downloading a new bitmap I change the drawable. The image now appears in the top left corner of the ImageView. Is there a way to have the image fill up the whole height that is possible and then adjust the width of the view to enable scaling the image without changing the ascpect ratio?

The image fills up all the space on a standard screen but on a WVGA Resolution the image takes only about half of the actual height of the ImageView.

like image 406
Janusz Avatar asked Nov 24 '10 10:11

Janusz


People also ask

What is the use of imageview in Java?

AnyClass.Application of ImageView is also in applying tints to an image in order to reuse a drawable resource and create overlays on background images. Moreover, ImageView is also used to control the size and movement of an image. Whenever ImageView is added to an activity, it means there is a requirement for an image resource.

How to add a drawable resource to the imageview class?

The following are the steps to add a drawable resource to the ImageView class. For adding an image from Android Studio, Drag the ImageView widget to the activity area of the application, a pop-up dialogue box will open choose from the wide range of drawable resources and click “ OK “.

How to add an image file to an imageview in Android?

Whenever ImageView is added to an activity, it means there is a requirement for an image resource. Thus it is oblivious to provide an Image file to that ImageView class. It can be done by adding an image file that is present in the Android Studio itself or we can add our own image file.

What is imageview in Android with Kotlin?

ImageView class or android.widget.ImageView inherits the android.view.View class which is the subclass of Kotlin. AnyClass.Application of ImageView is also in applying tints to an image in order to reuse a drawable resource and create overlays on background images. Moreover, ImageView is also used to control the size and movement of an image.


2 Answers

If I'm understanding you correctly, what you need to use is the centerCrop scaleType. fitStart scales the image proportionally, but neither the width nor height will exceed the size of the view, and the image will, as you said, have a top|left gravity.

Using centerCrop scales the image proportionally, but causes the shortest edge of the image to match the size of the view, and if there is additional data on the long side that does not fit, it is simply cropped off. The gravity is, of course, center. The below worked for me:

<ImageView         android:src="@drawable/image_placeholder"         android:id="@+id/cover_view"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:scaleType="centerCrop" /> 
like image 110
Kevin Coppock Avatar answered Oct 03 '22 04:10

Kevin Coppock


You can change scale type to fitXY via call to

imageView.setScaleType(ScaleType.FIT_XY); 
like image 39
Nikolay Ivanov Avatar answered Oct 03 '22 03:10

Nikolay Ivanov