Android的ImageView的规模更小的图像与灵活的高度宽度不裁剪或扭曲 [英] Android ImageView scale smaller image to width with flexible height without cropping or distortion
问题描述
常问,根本没有回答(至少不以可再现的方式)。
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.
删除安卓adjustViewBounds =真正的
不起作用。添加安卓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.
推荐答案
还有就是XML布局标准中没有可行的解决方案。
There is no viable solution within the XML layout standard.
的唯一可靠的方法来应对动态的图像尺寸是使用的LayoutParams
在code。
The only reliable way to react to a dynamic image size is to use LayoutParams
in code.
令人失望。
这篇关于Android的ImageView的规模更小的图像与灵活的高度宽度不裁剪或扭曲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!