Android ImageView 大小不随源图像缩放 [英] Android ImageView size not scaling with source image

查看:27
本文介绍了Android ImageView 大小不随源图像缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a few ImageViews inside a LinearLayout. I need to scale down the ImageViews so that they maintain their aspect ratios whilst fitting inside the LinearLayout vertically. Horizontally, I just need them to be adjacent to each other.

我为此制作了一个简化的测试台,它嵌套了加权布局,以便为 ImageViews 提供一个宽但不是很高的 LinearLayout -

I've made a simplified test bed for this which nests weighted Layouts so that I have a wide, but not very tall, LinearLayout for the ImageViews -

<?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>

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

(In the Eclipse layout editor the images are clipped vertically and not scaled at all - but that's just one of those idiosyncrasies that we learn to love)

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

When run on hardware the images are scaled to the correct height whilst preserving their aspect ratio. My problem is that they are not next to each other. The height of each ImageView correctly matches the height of the LinearLayout. The width of each ImageView is the width of the unscaled image - the actual scaled down images appearing at the left side of their ImageViews. Because of this, I'm getting a large gap between the images.

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

Ideally I would like to manage this through XML though, if this is not possible, I understand that using a custom view may be the best solution.

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

I tried creating an IconView (extends ImageView) class which overrides onMeasure so that I could create image views of the necessary size by scaling the width depending on the height. But the parentWidth and parentHeight being passed into the function were the dimensions of the screen and not of the LinearLayout container.

@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);
}

所以我的问题是 -

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

1) Can I amend the XML in some way to make it do what I need?

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

2) How do I make my custom class get the height of the LinearLayout so that I can calculate the necessary width for the custom images?

感谢您能读到这里.如果您能指出解决方案的方向,那就更感谢了!

Thanks if you've managed to read this far. Even more thanks if you can point me in the direction of a solution!

推荐答案

如何改变你的 ImageViews 以使用 android:layout_height="fill_parent"?

What about changing your ImageViews to use android:layout_height="fill_parent"?

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

Edit - took me a while to find, but looking at the source helped me pinpoint adjustViewBounds. You might want to try adding android:adjustViewBounds="true" to your ImageViews. Surprisingly, this defaults to false.

这篇关于Android ImageView 大小不随源图像缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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