如何获得在android的图像视图的宽度和高度? [英] How to get the width and height of an Image View in android?

查看:167
本文介绍了如何获得在android的图像视图的宽度和高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的code我有一个图片浏览在我的XML布局和我一直在改变这个源图像在我的code。现在我想知道生成的图像的每一次的宽度和高度。

In my code I have an Image View in my XML layout and I keep changing the source image for this in my code. Now I want to know the width and height of the generated image each time.

我试图用的getWidth()的getHeight() getMeasuredWidth() getMeasuredHeight(),但他们都返回0。

I tried using getWidth(), getHeight(), getMeasuredWidth(), and getMeasuredHeight(), but they all return 0.

我怎样才能得到呢?

推荐答案

如果你调用的getWidth()的getHeight()上ImageView的?如果从的onCreate调用()的活动,它不会工作。你需要等待活动的窗口,连接,然后调用的getWidth()和getHeight()上ImageView的。你可以尝试调用的getWidth()和你的活动的getHeight() onWindowFocusChanged()方法。

Where you calling getWidth() and getHeight() on ImageView? If you calling from onCreate() in activity, it won't work. You need to wait for activity window to attached and then call getWidth() and getHeight() on ImageView. You can try calling getWidth() and getHeight() from onWindowFocusChanged() method of your activity.

@Override
public void onWindowFocusChanged(boolean hasFocus){
    int width=imageView.getWidth();
    int height=imageView.getHeight();
}

这篇关于如何获得在android的图像视图的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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