How to get the width and height of an Image View in android?(如何在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中获取图像视图的宽度和高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在android中获取图像视图的宽度和高度?


- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01