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

一世执手 提交于 2019-12-03 09:11:24

问题


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.


回答1:


Basically the answer here is that there is no predefined value for what you are trying to achieve. The solution is to create a Matrix that fits to your needs and call setImageMatrix(matrix) on your ImageView.




回答2:


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"
/>



回答3:


You can change scale type to fitXY via call to

imageView.setScaleType(ScaleType.FIT_XY);



回答4:


simply do it in xml like

android:scaleType="fitXY"


来源:https://stackoverflow.com/questions/4265804/how-to-create-an-imageview-that-fills-the-parent-height-and-displays-an-image-as

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!