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

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

问题描述

在我的代码中,我的 XML 布局中有一个 Image View,并且我在代码中不断更改源图像.现在想知道每次生成的图片的宽高.

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.

我怎样才能得到这个?

推荐答案

你在 ImageView 上哪里调用 getWidth()getHeight() ?如果您在活动中从 onCreate() 调用,它将不起作用.您需要等待活动窗口附加,然后在 ImageView 上调用 getWidth() 和 getHeight().您可以尝试从您的活动的 onWindowFocusChanged() 方法调用 getWidth() 和 getHeight().

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天全站免登陆