【Android】getwidth和getmeasuredwidth的区别以及两者的使用场景
2015-12-08 21:43
411 查看
首先,看getWidth()的官方说明:
Added in API level 1
Return the width of the your view.
Returns
The width of your view, in pixels.
返回view的宽度,说的不详细,再看getWidth源码:
[java] view
plaincopy
<span style="font-size:18px;"> /**
* Return the width of the your view.
*
* @return The width of your view, in pixels.
*/
@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() {
return mRight - mLeft;
}</span>
从源码可以看出,getwidth返回的是右边坐标减轻坐标减去左边坐标,这要在布局之后才能确定它们的坐标,也就是说在布局后才能调用getwidth来获取。所以getWidth()获得的宽度是View在设定好布局后整个View的宽度。
而对于getmeasuredwidth,
Added in API level 11
Return the full width measurement information for this view as computed by the most recent call to
This should be used during measurement and layout calculations only. Use
see how wide a view is after layout.
Returns
The measured width of this view as a bit mask.
得到的是最近一次调用measure()方法测量后得到的是View的宽度,它应该仅仅用在测量和Layout的计算中。再看源码:
[java] view
plaincopy
/**
* Like {@link #getMeasuredWidthAndState()}, but only returns the
* raw width component (that is the result is masked by
* {@link #MEASURED_SIZE_MASK}).
*
* @return The raw measured width of this view.
*/
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
return The raw measured width of this view 获得的是原始的测量宽度。所以说getMeasuredWidth()是对View上的内容进行测量后得到的View内容占据的宽度。前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);(measure中的参数的值你自己可以定义),否则你得到的结果和getWidth()得到的结果是一样的。
两者的使用场合:
getMeasuredWidth:在自定义view重写onLayout时、在我们用layoutinflater动态加载view后想获得view的原始宽度时。
getWidth:一般在view已经布局后呈现出来了,想获取宽度时
public final int getWidth ()
Added in API level 1Return the width of the your view.
Returns
The width of your view, in pixels.
返回view的宽度,说的不详细,再看getWidth源码:
[java] view
plaincopy
<span style="font-size:18px;"> /**
* Return the width of the your view.
*
* @return The width of your view, in pixels.
*/
@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() {
return mRight - mLeft;
}</span>
从源码可以看出,getwidth返回的是右边坐标减轻坐标减去左边坐标,这要在布局之后才能确定它们的坐标,也就是说在布局后才能调用getwidth来获取。所以getWidth()获得的宽度是View在设定好布局后整个View的宽度。
而对于getmeasuredwidth,
public final int getMeasuredWidthAndState ()
Added in API level 11Return the full width measurement information for this view as computed by the most recent call to
measure(int, int). This result is a bit mask as defined by
MEASURED_SIZE_MASKand
MEASURED_STATE_TOO_SMALL.
This should be used during measurement and layout calculations only. Use
getWidth()to
see how wide a view is after layout.
Returns
The measured width of this view as a bit mask.
得到的是最近一次调用measure()方法测量后得到的是View的宽度,它应该仅仅用在测量和Layout的计算中。再看源码:
[java] view
plaincopy
/**
* Like {@link #getMeasuredWidthAndState()}, but only returns the
* raw width component (that is the result is masked by
* {@link #MEASURED_SIZE_MASK}).
*
* @return The raw measured width of this view.
*/
public final int getMeasuredWidth() {
return mMeasuredWidth & MEASURED_SIZE_MASK;
}
return The raw measured width of this view 获得的是原始的测量宽度。所以说getMeasuredWidth()是对View上的内容进行测量后得到的View内容占据的宽度。前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);(measure中的参数的值你自己可以定义),否则你得到的结果和getWidth()得到的结果是一样的。
两者的使用场合:
getMeasuredWidth:在自定义view重写onLayout时、在我们用layoutinflater动态加载view后想获得view的原始宽度时。
getWidth:一般在view已经布局后呈现出来了,想获取宽度时
相关文章推荐
- 【Android游戏开发二十一】Android os设备谎言分辨率的解决方案!以及简单阐述游戏引擎如何使用!
- “com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536”问题解决
- 【Android游戏开发二十】物理游戏之重力系统开发,让你的游戏变得有质有量!
- 安卓反编译:推荐一个最新反编译功能的工具
- Android多渠道打包
- 【Android游戏开发十八】解放手指,利用传感器开发游戏!(本文讲解在SurfaceView中用重力传感器控制圆球的各方向移动)
- 【Android游戏开发十七】让玩家自定义手势玩转Android游戏!—Android Gesture之【输入法手势技术】
- 【Android游戏开发十六】Android Gesture之【触摸屏手势识别】操作!利用触摸屏手势实现一个简单切换图片的功能!
- 【Android游戏开发十五】关于Android 游戏开发中 OnTouchEvent() 触屏事件的性能优化笔记!
- 【Android游戏开发十四】深入Animation,在SurfaceView中照样使用Android—Tween Animation!
- 【Android游戏开发十三】(保存游戏数据 [下文])详解SQLite存储方式,并把SQLite的数据库文件存储在SD卡中!!!
- [Android]Activity生命周期
- 【Android游戏开发十二】(保存游戏数据 [上文])详解SharedPreference 与 FIleInputStream/FileOutputStream将数据存储到SD卡中!
- 【Android游戏开发十一】手把手让你爱上Android sdk自带“9妹”(9patch 工具),让Android游戏开发更方便!
- AndroidStudio快速入门四:打造你的开发工具,settings必备
- Android Studio jni开发 串口通信
- AndroidStudio快速入门五:常见问题集合
- 【Android游戏开发之十】(优化处理)详细剖析Android Traceview效率检视工具,分析程序运行速度!并讲解两种创建SDcard方式!
- Gradle DSL method found: ‘android()’错误
- 【Android游戏开发之九】(细节处理)触屏事件中的Bug解决方案以及禁止横屏和竖屏切换!