如何根据它的父视图大小设置ImageView的绘制尺寸?

问题描述:

我有一个相对布局内的ImageView。当我创建活动我运行线程从互联网虎视眈眈的位图,并设置ImageView的。

I have imageView inside a relative layout. And when i create the activity i run thread to grap bitmap from the internet and set the imageView.

我要的是调整位图以规模基础上的相对视图大小,同时保持宽高比。在code为调整图像的工作,但我做的是我通过调用获得相对视图大小 myRelativeView.getWidth()&安培; myRelativeView.getHeight(),我觉得我不应该这样做,因为文档说这可能返回0。那么,什么是检索相对视图的大小的最佳方式在这种情况下?

What i want is to resize the bitmap to scale based on relative view size while keeping the aspect ratio. The code for resizing the image is working but what i do is i get the relative view size by calling myRelativeView.getWidth() & myRelativeView.getHeight() and i feel that i should not do that because the docs say it could return 0. So what is the best way to retrieve the size of the relative view in this case ?

FIX您的要求:]

FIX for your request :]

如果你不想改变大小
设置您的ImageView scaleType以黑客帝国

If you don't want to change size set your ImageView scaleType to "matrix"

如果你想改变的大小,但保持宽高比
您的ImageView scaleType设置为fitCenter(或fitStart fitEnd)

if you want to change size but keep aspect ration set your ImageView scaleType to "fitCenter" (or fitStart fitEnd)

要更改code

ImageView image;

image = (ImageView)findViewById(R.id.yourImageView1);

image.setScaleType(ImageView.ScaleType.FIT_CENTER);

要更改XML
示例

To Change in XML Example

<ImageView
    android:layout_width="match_parent"
    android:layout_width="match_parent"
    android:scaleType="fitCenter"
    android:src="ic_launcher"/>