View 源码学习

来源:互联网 发布:怎么使用网络云盘 编辑:程序博客网 时间:2024/05/16 15:40

View 源码学习

标签(空格分隔): 源码


measure height/width:大致理解为根据父容器的约束获取view 的大小
These dimensions define how big a view wants to be within its parent (see <a href="#Layout">Layout</a> for more details.)
The measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
and {@link #getMeasuredHeight()}.

width/height: 屏幕上view的实际大小
These dimensions define the actual size of the view on screen, at drawing time and
after layout. These values may, but do not have to, be different from the
measured width and height. The width and height can be obtained by calling
{@link #getWidth()} and {@link #getHeight()}.

margins: 单独的view可以定义padding,viewgroup才可以定义margins
Even though a view can define a padding, it does not provide any support for margins. However, view groups provide such a support.
Refer to{@link android.view.ViewGroup} and {@link android.view.ViewGroup.MarginLayoutParams} for further information.

layout:
layout分为两个过程:测量过程和陈列过程
* measure(int, int)
自上而下测量view tree, 在递归测量的过程中 每个view 都将尺寸规格向下push到view tree.测量结束时,每个view 都存储了他的测量值。
measure结束时 view 以及其子view的 measure width 与measure height 会被赋值。measure width 与measure height尊重父view的参数约束,这样可以保证在测量结束的时候父容器能够包含所有的子view的测量值。父容器可以多次调用measure 方法。
layout(int,int,int,int)
根据测量部分的测量值把每个view陈列在view tree
Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in {@link #measure(int, int)} and is a top-down traversal
of the view tree. Each view pushes dimension specifications down the tree during the recursion. At the end of the measure pass, every view has stored its measurements. The second pass happens in {@link #layout(int,int,int,int)} and is also top-down. During this pass each parent is responsible for positioning all of its children using the sizes computed in the measure pass.

When a view's measure() method returns, its {@link #getMeasuredWidth()} and {@link #getMeasuredHeight()} values must be set, along with those for all of that view's descendants. A view's measured width and measured height values must respect the constraints imposed by the view's parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent view may call measure() more than once on its children. For example, the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call measure() on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small.

requestLayout:
view认为自身不适合在当前位置时,由其本身调用requestLayout()。
To initiate a layout, call {@link #requestLayout}. This method is typically called by a view on itself when it believes that is can no longer fit within its current bounds.

invalidate():
To force a view to draw, call {@link #invalidate()}.

The entire view tree is single threaded
在UI线程更新UI
Note: The entire view tree is single threaded. You must always be on the UI thread when calling any method on any view.If you are doing work on other threads and want to update the state of a view from that thread, you should use a {@link Handler}.

0 0
原创粉丝点击