Android ImageView尺寸未按源图像缩放

Rok*_*Rok 72 formatting layout android custom-view imageview

我在LinearLayout中有一些ImageView.我需要缩小ImageViews,以便它们保持纵横比,同时垂直安装在LinearLayout内部.水平地,我只需要它们彼此相邻.

我为此制作了一个简化的测试床,它嵌套了加权的布局,这样我就可以为ImageViews提供一个宽但不高的LinearLayout -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="5">
        <LinearLayout android:id="@+id/linearLayout3" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
            <ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/test_image" android:scaleType="fitStart"></ImageView>
            <ImageView android:id="@+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/test_image" android:scaleType="fitStart"></ImageView>
        </LinearLayout>
        <LinearLayout android:id="@+id/linearLayout4" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"></LinearLayout>
    </LinearLayout>
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"></LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

(在Eclipse布局编辑器中,图像被垂直剪切而根本没有缩放 - 但这只是我们学会爱的那些特性之一)

在硬件上运行时,图像会缩放到正确的高度,同时保留其纵横比.我的问题是他们不是彼此相邻的.每个ImageView的高度正确匹配LinearLayout的高度.每个ImageView的宽度是未缩放图像的宽度 - 实际按比例缩小的图像出现在其ImageView的左侧.因此,我在图像之间会有很大的差距.

理想情况下,我想通过XML来管理这个,如果这是不可能的,我理解使用自定义视图可能是最好的解决方案.

我尝试创建一个IconView(扩展ImageView)类来覆盖onMeasure,这样我就可以通过根据高度缩放宽度来创建所需大小的图像视图.但是传递给函数的parentWidth和parentHeight是屏幕的尺寸而不是LinearLayout容器的尺寸.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
  int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
  // Calculations followed by calls to setMeasuredDimension, setLayoutParams
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是 -

1)我可以通过某种方式修改XML以使其满足我的需求吗?

2)如何让我的自定义类获取LinearLayout的高度,以便我可以计算自定义图像的必要宽度?

谢谢,如果你已经成功阅读了这篇文章.非常感谢你能指出我的解决方案!

Mat*_*lis 263

如何更改要使用的ImageView android:layout_height="fill_parent"

编辑 - 花了一段时间才找到,但是查看源代码帮我查明了adjustViewBounds.您可能想尝试添加android:adjustViewBounds="true"到ImageViews.令人惊讶的是,这默认为false.

  • 将layout_height更改为fill_parent没有区别,但将adjustViewBounds设置为true就可以了!我很惊讶我已经用各种各样的观点做了很多工作,并且设法不注意那个参数!谢谢 :) (7认同)
  • 大!android:adjustViewBounds ="true"有效! (3认同)
  • 我也是。我原以为它会默认为 true,但不知何故从未遇到过这个问题。 (2认同)

use*_*657 11

奇怪android:layout_height="fill_parent"而且android:adjustViewBounds="true"没有帮助.我遇到的问题是图像显示,但顶部有块行,图像底部有黑色行.设置比例类型来"centerCrop"帮助我.我猜,原因是我的图像尺寸很小.


Mic*_*chK 6

我有类似的问题,我的解决方案是android:scaleType="fitEnd"在XML布局文件中设置ImageView的属性.

  • 这解决了我的问题..实际上`android:scaleType ="fitXY"`+1给你.. :) (6认同)