如何根据它的父视图大小设置ImageView的绘制尺寸? [英] How to set ImageView drawable Size based on Its parent view size?

查看:443
本文介绍了如何根据它的父视图大小设置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"/>

这篇关于如何根据它的父视图大小设置ImageView的绘制尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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