Android开发这么多年,你真的懂得了UIView原理吗,搞定它能给你工作带来不少便利,提高效率
View的工作原理
1.初识ViewRoot和DecorView
ViewRoot
对应ViewRootImpl,它是连接WindowManager和DecorView的纽带,View的三大流程均通过ViewRoot来完成的。
Activity创建后,把DecorView添加到Window中,同时创建ViewRootImpl对象,将ViewRootImpl和DecorView关联起来。
root = new ViewRootImpl(view.getContext(),display);
root.setView(view, wparams, panelParentView);
View的绘制流程从ViewRoot#performTraversals开始,经过measure、layout、draw最终将一个View绘制出来:
- measure过程决定了View的宽/高,完成后可通过getMeasuredWidth/getMeasureHeight方法来获取View测量后的宽/高。
- Layout过程决定了View的四个顶点的坐标和实际View的宽高,完成后可通过getTop、getBotton、getLeft和getRight拿到View的四个定点坐标。
- Draw过程决定了View的显示,完成后View的内容才能呈现到屏幕上。
DecorView
作为顶层View,包含一个竖直LinearLayout,里面包含上面的标题栏,下面的内容栏 Activity中,setContentView就是拿到的内容栏。 DecorView其实是一个FrameLayout,View层的事件都先经过DecorView,然后才传递给我们的View。
2.理解MeasureSpec
2.1MeasureSpec
- MeasureSpec代表一个32位的int值,高2位为SpecMode,低30位为SpecSize,SpecMode是指测量模式,SpecSize是指在某种测量模式下的规格大小。 MpecMode有三类:
- UNSPECIFIED: 父容器不对View进行任何限制,要多大给多大,一般用于系统内部(parent不对child强加限制,child要多大给多大)
- EXACTLY:
父容器检测到View所需要的精确大小,这时候View的最终大小就是SpecSize所指定的值,对应LayoutParams中的match_parent和具体数值这两种模式。(parent已经决定child准确大小,child要依据这个大小)
- AT_MOST
父容器指定了一个可用大小即SpecSize,View的大小不能大于这个值,不同View实现不同,对应LayoutParams中的wrap_content。(child可以想要多大给多大,但是有一个上限,wrap_content)
2.2MeasureSpec和LayoutParams的对应关系
对于一个View,可以设置LayoutParams来指定宽高,系统会综合该LayoutParams和parent施加的MeasureSpec,得出最后应用于该View的MeasureSpec;
而对于DecorView,因为其没有parent,所以取而代之的是Window的size,结合自己的LayoutParams得出最后的MeasureSpec。
MeasureSpec一旦确定,onMeasure中就可以确定View的宽高。
2.2.1 DecorView的MeasureSpec计算过程:
在ViewRootImpl的measureHierarchy中,计算了DecorView的MeasureSpec。desiredWindow*为window的size:
childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);
childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);
performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
getRootMeasureSpec中根据window size和DecorView的LayoutParams计算出MeasureSpec。规则很简单,如果是MATCH_PARENT或者固定的值,则spec mode为EXACTLY,同时size设置为相应的值;如果是WRAP_CONTENT,则spec mode为AT_MOST,size为window size:
private static int getRootMeasureSpec(int windowSize, int rootDimension) {
int measureSpec;
switch (rootDimension) {
case ViewGroup.LayoutParams.MATCH_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;
}
2.2.2 普通View的MeasureSpec计算过程:
以ViewGroup的measureChildWithMargins为例,在该方法中会计算child的MeasureSpec。计算完成后,会直接对该view进行measure。计算时也会考虑parent的padding,child的margin:
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
具体的计算过程在getChildMeasureSpec中进行:
- child指定确定的size,则遵从child的这个size设置。
- child指定match_parent,如果parent表示可以exactly,则其size为parent size;如果parent表示atmost,即其size也不确定,则其atmost为parent size。
- child指定wrap_content,则此时size由child自己决定,所以只限制其atmost为parent size。
3.View的工作流程
View三大流程:
measure
:确定View的测量宽/高layout
: 确定View的最终宽/高和四个顶点的位置draw
:将View绘制到屏幕上
3.1measure过程
3.1.1measure过程
3.1.1.1View的measure过程
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));}
- setMeasuredDimension方法会设置View的宽/高的测量值
- getDefaultSize方法返回的大小就是measureSpec中的specSize,也就是View测量后的大小,绝大部分情况和View的最终大小(layout阶段确定)相同。
- getSuggestedMinimumWidth方法,作为getDefaultSize的第一个参数(建议宽度)
- 直接继承View的自定义控件,需要重写onMeasure方法并且设置 wrap_content时的自身大小,否则在布局中使用了wrap_content相当于使用了match_parent。 解决方法:在onMeasure时,给View指定一个内部宽/高,并在wrap_content时设置即可,其他情况沿用系统的测量值即可。
3.1.1.2ViewGroup的measure过程
- 对于ViewGroup来说,除了完成自己的measure过程之外,还会遍历去调用所有子元素的measure方法,个个子元素再递归去执行这个过程,和View不同的是,ViewGroup是一个抽象类,没有重写View的onMeasure方法,提供了measureChildren方法。
- measureChildren方法,遍历获取子元素,子元素调用measureChild方法
- measureChild方法,取出子元素的LayoutParams,再通过getChildMeasureSpec方法来创建子元素的MeasureSpec,接着将MeasureSpec传递给View的measure方法进行测量。
- ViewGroup没有定义其测量的具体过程,因为不同的ViewGroup子类有不同的布局特征,所以其测量过程的onMeasure方法需要各个子类去具体实现。
- measure完成之后,通过getMeasureWidth/Height方法就可以获取View的测量宽/高,需要注意的是,在某些极端情况下,系统可能要多次measure才能确定最终的测量宽/高,比较好的习惯是在onLayout方法中去获取测量宽/高或者最终宽/高。 如何在Activity中获取View的宽/高信息 因为View的measure过程和Activity的生命周期不是同步进行,如果View还没有测量完毕,那么获取到的宽/高就是0;所以在Activity的onCreate、onStart、onResume中均无法正确的获取到View的宽/高信息。下面给出4种解决方法。
- Activity/View#onWindowFocusChanged。 onWindowFocusChanged这个方法的含义是:VieW已经初始化完毕了,宽高已经准备好了,需要注意:它会被调用多次,当Activity的窗口得到焦点和失去焦点均会被调用。
- view.post(runnable)。 通过post将一个runnable投递到消息队列的尾部,当Looper调用此runnable的时候,View也初始化好了。
- ViewTreeObserver。 使用ViewTreeObserver的众多回调可以完成这个功能,比如OnGlobalLayoutListener这个接口,当View树的状态发送改变或View树内部的View的可见性发生改变时,onGlobalLayout方法会被回调。需要注意的是,伴随着View树状态的改变,onGlobalLayout会被回调多次。
- view.measure(int widthMeasureSpec,int heightMeasureSpec)。 (1). match_parent: 无法measure出具体的宽高,因为不知道父容器的剩余空间,无法测量出View的大小 (2). 具体的数值(dp/px):
int widthMeasureSpec = MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
view.measure(widthMeasureSpec,heightMeasureSpec);
(3). wrap_content:
int widthMeasureSpec = MeasureSpec.makeMeasureSpec((1<<30)-1,MeasureSpec.AT_MOST);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec((1<<30)-1,MeasureSpec.AT_MOST);
view.measure(widthMeasureSpec,heightMeasureSpec);
3.2layout过程
3.3draw过程
4.自定义View
4.1自定义View分类
4.1.1 继承View重写onDraw方法
主要用于实现一些不规则效果,不方便通过组合布局来实现,往往需要静态或者动态的现实一些不规则的图形。需要自己实现wrap_content,padding也需要自己处理
4.1.2 继承ViewGroup派生特殊的Layout
主要用于实现自定义的布局,我们重新定义一种新布局,效果看起来很像几种View组合在一起。方法稍微复杂一些,需要合适的处理ViewGroup的测量、布局这两个过程,并同事处理子元素的测量和布局过程。
4.1.2 继承特定View(比如TextView)
比较常见,一般用于扩展已有的View的功能。比如TextView,不需要自己支持wrap_content,padding等
4.1.2 继承特定ViewGroup(比如LinearLayout)
当某种效果看起来像几种View组合在一起。采用这种方法不需要自己处理ViewGroup的测量和布局这两个过程。方法2更接近View底层。
4.2自定义View须知
4.2.1 让View支持wrap_content
直接继承View或者ViewGroup的控件,如果不在onMeasure中对wrap_content做特殊处理,那么使用时无法达到预期效果
4.2.2 如果有必要,让你的View支持padding
直接继承View的控件,如果不在draw方法中处理padding,那么这个属性就无效。 直接继承ViewGroup的控件需要在onMeasure和onLayout中考虑padding和自元素的margin对其造成的影响,不然会导致失效。
4.2.3 尽量不要在View中使用Handler,没必要
View本身提供了post系列的方法
4.2.4 View中如果有线程或者动画,需要及时停止,参考View#onDetachedFromWindow
如果有线程或者动画要停止时,那么onDetachedFromWindow是个很好的时机。 包含此View的Activity推出或者当前View被remove时,View的onDetachedFromWindow方法会被调用(相对应方法onAttachedToWindow)。
4.2.5 View带有滑动嵌套情形时,需要处理好滑动冲突
如果有滑动冲突,会严重影响View的效果
4.3自定义View示例
4.4自定义View的思想
总结:
步骤 | 关键字 | 作用 |
---|---|---|
1 | 构造函数 | View初始化 |
2 | onMeasure | 测量View大小 |
3 | onSizeChanged | 确定View大小 |
4 | onLayout | 确定子View布局(自定义View包含子View时有用) |
5 | onDraw | 实际绘制内容 |
6 | 提供接口 | 控制View或监听View某些状态。 |
谢谢大家
转载自:https://juejin.cn/post/7126100434149703717