无法理解的onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Sodino 2012-01-13 12:00:49
自己继承ViewGroup实现一个视图。重写protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
发现执行该函数时widthMeasureSpec值为1073742304,heightMeasureSpec值为-2147482923。

这两个值是什么意思?

谢谢。
...全文
912 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sodino 2012-01-13
  • 打赏
  • 举报
回复
嗯,是的。只是View的宽高,与分辨率无直接关系。
BadPattern 2012-01-13
  • 打赏
  • 举报
回复
原来我搞错了,那是窗口的大小+MeasureSpec类中的specMode的值
BadPattern 2012-01-13
  • 打赏
  • 举报
回复
那是多少呐?
Sodino 2012-01-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 dr8737010 的回复:]

楼主得到的两个值其实就是屏幕的长宽+MeasureSpec类中的specMode的值.
由此推断楼主设备的屏幕分辨率为:725*480
[/Quote]

错!
BadPattern 2012-01-13
  • 打赏
  • 举报
回复
恭喜楼主解决问题
BadPattern 2012-01-13
  • 打赏
  • 举报
回复
楼主得到的两个值其实就是屏幕的长宽+MeasureSpec类中的specMode的值.
由此推断楼主设备的屏幕分辨率为:725*480
Sodino 2012-01-13
  • 打赏
  • 举报
回复
详见源代码:
http://www.google.com/codesearch/p?hl=en#uX1GffpyOZk/core/java/android/view/View.java&l=9019
Sodino 2012-01-13
  • 打赏
  • 举报
回复
已解决:


Log.d("ANDROID_LAB",
"onMeasure w_mode=" + MeasureSpec.getMode(widthMeasureSpec) + " w_size="
+ MeasureSpec.getSize(widthMeasureSpec) + " h_mode=" + MeasureSpec.getMode(heightMeasureSpec)
+ " h_size=" + MeasureSpec.getSize(heightMeasureSpec));


其中getMode()返回值为:EXACTLY。
BadPattern 2012-01-13
  • 打赏
  • 举报
回复
/**
* Measure specification mode: The parent has determined an exact size
* for the child. The child is going to be given those bounds regardless
* of how big it wants to be.
*/
public static final int EXACTLY = 1 << MODE_SHIFT;

/**
* Measure specification mode: The child can be as large as it wants up
* to the specified size.
*/
public static final int AT_MOST = 2 << MODE_SHIFT;
private static final int MODE_SHIFT = 30;

那两个值是出自这个函数:
       private int getRootMeasureSpec(int windowSize, int rootDimension) {
int measureSpec;
switch (rootDimension) {

case ViewGroup.LayoutParams.FILL_PARENT:
// Window can't resize. Force root view to be windowSize.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);
break;
case ViewGroup.LayoutParams.WRAP_CONTENT:
// Window can resize. Set max size for root view.
measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);
break;
default:
// Window wants to be an exact size. Force root view to be that size.
measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);
break;
}
return measureSpec;
}

具体啥意思,我也在研究....

80,350

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧