美文网首页Android知识
How Android Draws Views(译)

How Android Draws Views(译)

作者: Johnsonxu | 来源:发表于2017-02-15 13:43 被阅读47次

When an Activity receives focus, it will be requested to draw its layout. The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy.

当Activity得到焦点后,Activity会被系统请求绘制其自身的的layout。Android负责处理绘制的过程,但Activity必须提供其layout树形结构中的根节点。

Drawing begins with the root node of the layout. It is requested to measure and draw the layout tree. Drawing is handled by walking the tree and rendering each View that intersects the invalid region. In turn, each ViewGroup is responsible for requesting each of its children to be drawn (with the draw()
method) and each View is responsible for drawing itself. Because the tree is traversed in-order, this means that parents will be drawn before (i.e., behind) their children, with siblings drawn in the order they appear in the tree.

绘制过程从布局的根节点开始。系统要求根节点测量(measure)和绘制(draw)layout树。绘制处理的过程是遍历整棵树,并渲染每一个与无效区域(invalid region)相交的View。每个View group依次请求它的每一个子节点进行绘制(使用draw()方法),每一个View负责绘制它自己。整棵树按顺序进行遍历,因此父节点将在其子节点之前先被绘制(这意味着先绘制的更处于后方)。兄弟节点按他们在树中出现的顺序依次绘制。

The framework will not draw View objects that are not in the invalid region, and also will take care of drawing the View background for you.You can force a View to draw, by calling invalidate()
不在无效区域中的View不会被绘制, 同时framework会为你管理处于后台的View的绘制工作. 可以通过调用invalidate()强制一个View进行绘制.

Drawing the layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in [measure(int, int)](https://developer.android.com/reference/android/view/View.html#mesure(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 Viewhas stored its measurements. The second pass happens in [layout(int, int, int, int)](https://developer.android.com/reference/android/view/View.html#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 object's [measure()](https://developer.android.com/reference/android/view/View.html#mesure(int, int)) method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View object's descendants. A View object's measured width and measured height values must respect the constraints imposed by the View object'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()](https://developer.android.com/reference/android/view/View.html#mesure(int, int)) 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()](https://developer.android.com/reference/android/view/View.html#measure(int, int)) on them again with actual numbers if the sum of all the children's unconstrained sizes is too big or too small (that is, if the children don't agree among themselves as to how much space they each get, the parent will intervene and set the rules on the second pass).

layout的绘制有2个过程:测量过程和布局过程。
测量过程在measure(int,int)中实现,是一个自顶向下的对View树的遍历过程。在这个递归过程期间,每个View会将它的尺寸规格沿着树向下传递。在测量过程的最后, 每个View保存它们的尺寸.
第二个过程布局过程发生在layout(int,int,int,int),也是自顶向下的过程. 在此过程期间, 每个父节点根据在测量过程中的计算结果,负责它的所有子节点的安放和布置.
当View的 measure() 方法返回, 它的 getMeasureWidth() 和 getMeasureHeight() 方法必须能返回有意义的值, 根据这个规定,所有View的子节点也必须一致. View的测量后宽度和高度值, 必须遵守其父节点规定的约束值.这保证了在测量过程的最后, 所有的父节点能够适应所有他们的子节点的尺寸. 父View可能会多次调用子节点measure()方法. 举个例子, 在没定下来尺寸的时候, 父节点可能会先测一次每个子节点来找出他们想要多大的尺寸, 如果所有子节点的未受限之前的尺寸总和太大或者太小,会使用实际的数字对他们再次调用measure().(即, 如果子节点不同意它们之间所分配获得的空间大小, 那么父节点将进行调停,并在第二个过程中设定规矩).

To initiate a layout, call requestLayout(). This method is typically called by a Viewon itself when it believes that is can no longer fit within its current bounds.

The measure pass uses two classes to communicate dimensions. The ViewGroup.LayoutParams class is used by View objects to tell their parents how they want to be measured and positioned.The base ViewGroup.LayoutParams class just describes how big the View
wants to be for both width and height. For each dimension, it can specify one of:
测量过程使用2个类来负责尺寸的传递和通信. View 使用 View.MeasureSpec 类来告诉父节点如何测量和安放它们自己. 基础的LayoutParams类描述View想要多大的宽度和高度. 对于每一个尺寸规格, 它可以指定如下值:
一个精确的数值.

1, an exact number

2,MATCH_PARENT, which means the View wants to be as big as its parent (minus padding)
3,WRAP_CONTENT , which means that the View wants to be just big enough to enclose its content (plus padding).
FILL_PARENT, 意味着View想要尽量和它的父节点一样大.(减去填充值)
WRAP_CONTENT, View想要适应其内容的大小(加上padding值)

There are subclasses of ViewGroup.LayoutParams for different subclasses of ViewGroup. For example, RelativeLayout has its own subclass ofViewGroup.LayoutParams, which includes the ability to center child View
objects horizontally and vertically.
若要初始化或请求一个layout, 调用 requestLayout()方法. 典型地,当View确认它不再适合其当前边界时, 它会调用自身的此方法.
不同的ViewGroup子类有对应的LayoutParams子类. 例如, RelativeLayout有它自己对应的LayoutParams子类, 包含了可以设置子View水平居中和垂直居中的能力.

MeasureSpecs 用于自树的父节点到子节点向下专递需求.
MeasureSpec
objects are used to push requirements down the tree from parent to child. A
MeasureSpecs 用于自树的父节点到子节点向下专递需求.
MeasureSpec
can be in one of three modes:
MeasureSpec 可以为如下三种模式之一:

UNSPECIFIED This is used by a parent to determine the desired dimension of a child View. For example, a LinearLayout may call [measure()](https://developer.android.com/reference/android/view/View.html#measure(int, int))on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child View wants to be given a width of 240 pixels.
UNSPECIFIED: 由父节点使用,决定子View所需的尺寸. 例如, 一个LinearLayout可能会调用子节点的measure() 并设置height为UNSPECIFIED, width为EXACTLY 240, 来找出在给予240像素宽度的情况下,子View有多高.

EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.
EXACTLY: 由父节点使用,为子节点强制指定一个实际的尺寸. 子节点必须使用这个大小, 并需要保证它的所有孩子都适应这个尺寸.

AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.
AT_MOST: 由父节点使用,指定子节点的最大尺寸. 子节点必须保证它及它的所有孩子都不得超过此尺寸.

相关文章

网友评论

    本文标题:How Android Draws Views(译)

    本文链接:https://www.haomeiwen.com/subject/buhqwttx.html