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會被系統(tǒng)請求繪制其自身的的layout吼拥。Android負責處理繪制的過程倚聚,但Activity必須提供其layout樹形結構中的根節(jié)點。
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.
繪制過程從布局的根節(jié)點開始凿可。系統(tǒng)要求根節(jié)點測量(measure)和繪制(draw)layout樹惑折。繪制處理的過程是遍歷整棵樹,并渲染每一個與無效區(qū)域(invalid region)相交的View。每個View group依次請求它的每一個子節(jié)點進行繪制(使用draw()方法)惨驶,每一個View負責繪制它自己白热。整棵樹按順序進行遍歷,因此父節(jié)點將在其子節(jié)點之前先被繪制(這意味著先繪制的更處于后方)粗卜。兄弟節(jié)點按他們在樹中出現(xiàn)的順序依次繪制屋确。
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()
不在無效區(qū)域中的View不會被繪制, 同時framework會為你管理處于后臺的View的繪制工作. 可以通過調(diào)用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)中實現(xiàn)休建,是一個自頂向下的對View樹的遍歷過程乍恐。在這個遞歸過程期間,每個View會將它的尺寸規(guī)格沿著樹向下傳遞测砂。在測量過程的最后, 每個View保存它們的尺寸.
第二個過程布局過程發(fā)生在layout(int,int,int,int),也是自頂向下的過程. 在此過程期間, 每個父節(jié)點根據(jù)在測量過程中的計算結果,負責它的所有子節(jié)點的安放和布置.
當View的 measure() 方法返回, 它的 getMeasureWidth() 和 getMeasureHeight() 方法必須能返回有意義的值, 根據(jù)這個規(guī)定,所有View的子節(jié)點也必須一致. View的測量后寬度和高度值, 必須遵守其父節(jié)點規(guī)定的約束值.這保證了在測量過程的最后, 所有的父節(jié)點能夠適應所有他們的子節(jié)點的尺寸. 父View可能會多次調(diào)用子節(jié)點measure()方法. 舉個例子, 在沒定下來尺寸的時候, 父節(jié)點可能會先測一次每個子節(jié)點來找出他們想要多大的尺寸, 如果所有子節(jié)點的未受限之前的尺寸總和太大或者太小,會使用實際的數(shù)字對他們再次調(diào)用measure().(即, 如果子節(jié)點不同意它們之間所分配獲得的空間大小, 那么父節(jié)點將進行調(diào)停,并在第二個過程中設定規(guī)矩).
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 類來告訴父節(jié)點如何測量和安放它們自己. 基礎的LayoutParams類描述View想要多大的寬度和高度. 對于每一個尺寸規(guī)格, 它可以指定如下值:
一個精確的數(shù)值.
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想要盡量和它的父節(jié)點一樣大.(減去填充值)
WRAP_CONTENT, View想要適應其內(nèi)容的大小(加上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, 調(diào)用 requestLayout()方法. 典型地,當View確認它不再適合其當前邊界時, 它會調(diào)用自身的此方法.
不同的ViewGroup子類有對應的LayoutParams子類. 例如, RelativeLayout有它自己對應的LayoutParams子類, 包含了可以設置子View水平居中和垂直居中的能力.
MeasureSpecs 用于自樹的父節(jié)點到子節(jié)點向下專遞需求.
MeasureSpec
objects are used to push requirements down the tree from parent to child. A
MeasureSpecs 用于自樹的父節(jié)點到子節(jié)點向下專遞需求.
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: 由父節(jié)點使用,決定子View所需的尺寸. 例如, 一個LinearLayout可能會調(diào)用子節(jié)點的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: 由父節(jié)點使用,為子節(jié)點強制指定一個實際的尺寸. 子節(jié)點必須使用這個大小, 并需要保證它的所有孩子都適應這個尺寸.
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: 由父節(jié)點使用,指定子節(jié)點的最大尺寸. 子節(jié)點必須保證它及它的所有孩子都不得超過此尺寸.