如何创建一个 ImageView 填充父级高度并显示尽可能大的 Image?

发布于 2024-10-04 00:09:00 字数 525 浏览 7 评论 0原文

我有一个按以下方式定义的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

左耳近心 2024-10-11 00:09:00

如果我理解正确的话,您需要使用的是 centerCrop scaleType。 fitStart 按比例缩放图像,但宽度和高度都不会超过视图的大小,并且正如您所说,图像将具有 top|left 重力。

使用centerCrop按比例缩放图像,但会导致图像的最短边与视图的大小相匹配,如果长边上有其他数据不适合,则简单地将其裁剪掉。当然,重力是中心。以下内容对我有用:

<ImageView
        android:src="@drawable/image_placeholder"
        android:id="@+id/cover_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="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 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"
/>
孤寂小茶 2024-10-11 00:09:00

您可以通过调用将比例类型更改为 fitXY

imageView.setScaleType(ScaleType.FIT_XY);

You can change scale type to fitXY via call to

imageView.setScaleType(ScaleType.FIT_XY);
怀中猫帐中妖 2024-10-11 00:09:00

只需在 xml 中执行即可,例如

android:scaleType="fitXY"

simply do it in xml like

android:scaleType="fitXY"
素染倾城色 2024-10-11 00:09:00

基本上,这里的答案是,您想要实现的目标没有预定义的值。解决方案是创建一个 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 call setImageMatrix(matrix) on your ImageView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文