如何创建一个 ImageView 填充父级高度并显示尽可能大的 Image?
我有一个按以下方式定义的 ImageView:
<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"/>
现在下载新的位图后,我更改了可绘制对象。图像现在出现在 ImageView 的左上角。有没有办法让图像填满可能的整个高度,然后调整视图的宽度以在不改变纵横比的情况下缩放图像?
图像填满了标准屏幕上的所有空间,但在 WVGA 分辨率下,图像仅占据 ImageView 实际高度的大约一半。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确的话,您需要使用的是
centerCrop
scaleType。fitStart
按比例缩放图像,但宽度和高度都不会超过视图的大小,并且正如您所说,图像将具有top|left
重力。使用centerCrop按比例缩放图像,但会导致图像的最短边与视图的大小相匹配,如果长边上有其他数据不适合,则简单地将其裁剪掉。当然,重力是
中心
。以下内容对我有用: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 atop|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:您可以通过调用将比例类型更改为 fitXY
You can change scale type to fitXY via call to
只需在 xml 中执行即可,例如
simply do it in xml like
基本上,这里的答案是,您想要实现的目标没有预定义的值。解决方案是创建一个
Matrix
适合您的需求并调用在
。ImageView
上设置 ImageMatrix(matrix)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 callsetImageMatrix(matrix)
on yourImageView
.