getWidth() 和 getHeight 在 onMeasure() 后返回零(特定设备) [英] getWidth() and getHeight return zero after onMeasure() (specific devices)

查看:28
本文介绍了getWidth() 和 getHeight 在 onMeasure() 后返回零(特定设备)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到我的应用程序视图在 onMeasure() 已经被调用后为 getWidth() 和 getHeight() 返回 0.这只发生在少数设备上,对于大多数 android 设备,以下代码工作正常.我的 checkViewAndLoad() 函数根据视图的大小加载一个缩放的位图.

I noticed that my application's view returns 0 for getWidth() and getHeight() after onMeasure() has already been called. This only happens on a handful of devices, for most android devices the following code works fine. My checkViewAndLoad() function loads a scaled bitmap depending on the size of the view.

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    Log.d("widthMeasureSpec", Integer.toString(MeasureSpec.getSize(widthMeasureSpec)));
    Log.d("heightMeasureSpec", Integer.toString(MeasureSpec.getSize(heightMeasureSpec)));
    Log.d("viewWidth", Integer.toString(getWidth()));
    Log.d("viewHeight", Integer.toString(getHeight()));

    checkViewAndLoad();
}

这是在 onMeasure() 之后为 getWidth()/getHeight() 返回零的设备 (Motorola Droid Razr Maxx) 的日志:

Here is a log of a device (Motorola Droid Razr Maxx) that returns zero for getWidth()/getHeight() after onMeasure():

09-03 20:55:58.359: D/widthMeasureSpec(29496): 540
09-03 20:55:58.359: D/heightMeasureSpec(29496): 720
09-03 20:55:58.359: D/viewWidth(29496): 0
09-03 20:55:58.359: D/viewHeight(29496): 0

我也尝试过手动 setMeasuredDimensions(),但那只手对日志没有影响.

I also tried to setMeasuredDimensions() manually, but that hand no affect on the logs.

有人可以告诉我我在这里做错了什么,或者如何在调用 onMeasure() 后获取 SurfaceView 的宽度/高度?

Can someone tell me what I'm doing wrong here, or how to get the width/height of a SurfaceView after onMeasure() has been called?

推荐答案

在此处使用 getMeasuredWidth/Height().getWidth/Height() 在布局之后才有效.

Use getMeasuredWidth/Height() here. getWidth/Height() aren't valid until after a layout.

这篇关于getWidth() 和 getHeight 在 onMeasure() 后返回零(特定设备)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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