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

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

问题描述

我有一个的LinearLayout内几ImageViews。我需要按比例降低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.

我做了一个简单的测试床,这里面嵌套加权布局,使我有一个广泛的,但不是很高,LinearLayout中的ImageViews -

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)

在硬件上运行的图像缩放到合适的高度,而preserving的纵横比。我的问题是,它们不彼此相邻。每个ImageView的高度正确地匹配的LinearLayout的高度。每个ImageView的宽度是未缩放的图像的宽度 - 的实际比例缩小的图像出现在其ImageViews的左侧。正因为如此,我得到的图像之间有很大的差距。

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.

我试图创建一个图标型控件(延伸ImageView的)类,它覆盖onMeasure这样我就可以通过缩放根据高度宽度创造必要的尺寸图像视图。但上级宽度和上级高度被传递到函数是在屏幕的尺寸,而不是的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);
}

所以,我的问题是 -

So my questions are -

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使用安卓?layout_height =FILL_PARENT

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

修改 - 我花了一段时间来寻找,但查看源帮我精确<一个href="http://developer.android.com/reference/android/widget/ImageView.html#attr_android%3aadjustViewBounds">adjustViewBounds.你可能想尝试添加安卓adjustViewBounds =真正的您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天全站免登陆