Android的View绘制过程,onMeasure()方法介绍
来源:互联网 发布:王思丽事件知乎 编辑:程序博客网 时间:2024/06/05 20:36
onMeasure方法
onMeasure方法是测量view和它的内容,决定measured width和measured height的,这个方法由 measure(int, int)
方法唤起,子类可以覆写onMeasure来提供更加准确和有效的测量。
有一个约定:在覆写onMeasure方法的时候,必须调用 setMeasuredDimension(int,int)
来存储这个View经过测量得到的measured width and height。
如果没有这么做,将会由measure(int, int)方法抛出一个IllegalStateException。
其中两个输入参数:
widthMeasureSpec
heightMeasureSpec
分别是parent提出的水平和垂直的空间要求。
这两个要求是按照View.MeasureSpec类来进行编码的。
参见View.MeasureSpec这个类的说明:这个类包装了从parent传递下来的布局要求,传递给这个child。
每一个MeasureSpec代表了对宽度或者高度的一个要求。
每一个MeasureSpec有一个尺寸(size)和一个模式(mode)构成。
MeasureSpecs这个类提供了把一个<size, mode>的元组包装进一个int型的方法,从而减少对象分配。当然也提供了逆向的解析方法,从int值中解出size和mode。
有三种模式:UNSPECIFIED
这说明parent没有对child强加任何限制,child可以是它想要的任何尺寸。
EXACTLY
Parent为child决定了一个绝对尺寸,child将会被赋予这些边界限制,不管child自己想要多大。
AT_MOST
Child可以是自己任意的大小,但是有个绝对尺寸的上限。
覆写onMeasure方法的时候,子类有责任确保measured height and width至少为这个View的最小height和width。
(getSuggestedMinimumHeight()
and getSuggestedMinimumWidth()
)。
onLayout
这个方法是在layout pass中被调用的,用于确定View的摆放位置和大小。方法声明:
protected void onLayout (boolean changed, int left, int top, int right, int bottom)@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); }private int measureWidth(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { //parent为child决定了一个绝对尺寸 // We were told how big to be result = specSize; } else { // Measure the text result = (int) mTextPaint.measureText(mText) + getPaddingLeft() + getPaddingRight(); if (specMode == MeasureSpec.AT_MOST) { // Respect AT_MOST value if that was what is called for by measureSpec result = Math.min(result, specSize); } } return result; } /** * Determines the height of this view * @param measureSpec A measureSpec packed into an int * @return The height of the view, honoring constraints from measureSpec */ private int measureHeight(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); mAscent = (int) mTextPaint.ascent(); if (specMode == MeasureSpec.EXACTLY) { // We were told how big to be result = specSize; } else { // Measure the text (beware: ascent is a negative number) result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop() + getPaddingBottom(); if (specMode == MeasureSpec.AT_MOST) { // Respect AT_MOST value if that was what is called for by measureSpec result = Math.min(result, specSize); } } return result; }
- Android的View绘制过程,onMeasure()方法介绍
- Android中View的绘制过程 onMeasure方法
- Android中View的绘制过程 onMeasure方法简述
- Android中View的绘制过程 onMeasure和onLayout()方法
- Android中View的绘制过程 onMeasure方法简述
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
- Android中自定义View的绘制过程的 onMeasure方法简述
- Android 自定义view 和 onMeasure方法介绍
- Android 自定义view 和 onMeasure方法介绍
- 【view】Android View.onMeasure方法的理解
- 比较好的 文章博客整理 持续更新。。。
- Java String对象的经典问题(new String())
- django--视图函数的包装(Wrapping view function)
- Vbox 未指定XXX网络名称 找不到网卡问题
- execute、executeQuery和executeUpdate之间的区别
- Android的View绘制过程,onMeasure()方法介绍
- UINavigationBar 自定义
- Cue:民用级别的医学数据监测设备
- android网络类型之2G-3G切换
- AjaxFileUploaderV2.1修改版
- hard link and soft link in Linux
- android--Manifest文件中allowTaskReparenting属性的含义
- Android 查看签名
- suse机子导库