在Android中,我定义了一个ImageView的layout_width为fill_parent(它占用了手机的全宽度)。

如果我放入ImageView的图像大于layout_width, Android会缩放?那么高度呢?当Android缩放图像时,它会保持纵横比吗?

我发现在ImageView的顶部和底部有一些空白当Android缩放一个比ImageView大的图像时。这是真的吗?如果是,我如何消除空白?


当前回答

我有一个算法缩放位图,以最适合容器的尺寸,保持其纵横比。请在这里找到我的解决方案

希望这能帮助到路上的人!

其他回答

如果图像质量在以下情况下下降: 使用

android:adjustViewBounds="true"

而不是

android:adjustViewBounds="true"
android:scaleType="fitXY"

我用这个:

<ImageView
android:id="@+id/logo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
android:scaleType="centerInside"
android:src="@drawable/logo" />

我的图像比屏幕小。为了让它按比例拉伸到最大,并在视图中居中,我必须使用以下代码:

<ImageView
    android:id="@+id/my_image"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerInParent="true"
    android:adjustViewBounds="true"
    android:layout_weight="1"
    android:scaleType="fitCenter" />

记住,如果你有一个相对布局,有一些元素被设置在ImageView的上方或下方,它们很可能会被图像重叠。

这招对我很管用:

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="39dip"
android:scaleType="centerCrop"
android:adjustViewBounds ="true"

这解决了我的问题

android:adjustViewBounds="true"
android:scaleType="fitXY"