Android ImageView 将较小的图像缩放到具有灵活高度的宽度,而不会裁剪或扭曲 [英] Android ImageView scale smaller image to width with flexible height without cropping or distortion

查看:12
本文介绍了Android ImageView 将较小的图像缩放到具有灵活高度的宽度,而不会裁剪或扭曲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经常被问到,从不回答(至少不是以可重复的方式).

Often asked, never answered (at least not in a reproducible way).

我有一个图像视图,其中的图像比视图.我想将图像缩放到屏幕的宽度并调整 ImageView 的高度以反映图像的比例正确高度.

I have an image view with an image that is smaller than the view. I want to scale the image to the width of the screen and adjust the height of the ImageView to reflect the proportionally correct height of the image.

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

这会导致图像以原始尺寸为中心(小于屏幕宽度),边距位于侧面.不好.

This results in the image centered at its original size (smaller then the screen width) with margins at the side. No good.

所以我加了

android:adjustViewBounds="true" 

同样的效果,不好.我加了

Same effect, no good. I added

android:scaleType="centerInside"

同样的效果,不好.我将 centerInside 更改为 fitCenter.一样的效果,不好.我将 centerInside 更改为 centerCrop.

Same effect, no good. I changed centerInside to fitCenter. Same effect, no good. I changed centerInside to centerCrop.

android:scaleType="centerCrop"

现在,最后,图像缩放到屏幕的宽度 - 但顶部和底部裁剪!所以我把 centerCrop 改成了 fitXY.

Now, finally, the image is scaled to the width of the screen - but cropped at top and bottom! So I changed centerCrop to fitXY.

android:scaleType="fitXY"

现在图像被缩放到屏幕的宽度,但没有在 y 轴上缩放,导致图像失真.

Now the image is scaled to the width of the screen but not scaled on the y-axis, resulting in a distorted image.

删除 android:adjustViewBounds="true" 无效.正如其他地方所建议的那样,添加 android:layout_gravity 再次无效.

Removing android:adjustViewBounds="true" has no effect. Adding an android:layout_gravity, as suggested elsewhere, has again no effect.

我尝试了其他组合 - 无济于事.所以,请问有谁知道:

I have tried other combinations -- to no avail. So, please does anyone know:

如何设置 ImageView 的 XML 以填充屏幕宽度、缩放较小的图像以填充整个视图、以纵横比显示图像而不失真或裁剪?

我还尝试设置任意数字高度.这仅对 centerCrop 设置有影响.它会根据视图高度垂直扭曲图像.

I also tried setting an arbitrary numeric height. This only has an effect with the centerCrop setting. It will distort the image vertically according to the view height.

推荐答案

我遇到了和你一样的问题.在尝试不同的变化 30 分钟后,我以这种方式解决了问题.它为您提供灵活的高度,以及调整到父宽度的宽度

I had the same problem, as yours. After 30 minutes of trying diffirent variations I solved the problem in this way. It gives you the flexible height, and width adjusted to the parent width

<ImageView
            android:id="@+id/imageview_company_logo"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:scaleType="fitCenter"
            android:src="@drawable/appicon" />

这篇关于Android ImageView 将较小的图像缩放到具有灵活高度的宽度,而不会裁剪或扭曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆