How Android Draws Views(譯)

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é)點必須保證它及它的所有孩子都不得超過此尺寸.

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末茵烈,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子砌些,更是在濱河造成了極大的恐慌呜投,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,839評論 6 482
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件存璃,死亡現(xiàn)場離奇詭異仑荐,居然都是意外死亡,警方通過查閱死者的電腦和手機纵东,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,543評論 2 382
  • 文/潘曉璐 我一進店門粘招,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人偎球,你說我怎么就攤上這事洒扎。” “怎么了衰絮?”我有些...
    開封第一講書人閱讀 153,116評論 0 344
  • 文/不壞的土叔 我叫張陵袍冷,是天一觀的道長。 經(jīng)常有香客問我猫牡,道長胡诗,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,371評論 1 279
  • 正文 為了忘掉前任淌友,我火速辦了婚禮煌恢,結果婚禮上,老公的妹妹穿的比我還像新娘震庭。我一直安慰自己症虑,他們只是感情好,可當我...
    茶點故事閱讀 64,384評論 5 374
  • 文/花漫 我一把揭開白布归薛。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪主籍。 梳的紋絲不亂的頭發(fā)上习贫,一...
    開封第一講書人閱讀 49,111評論 1 285
  • 那天,我揣著相機與錄音千元,去河邊找鬼苫昌。 笑死,一個胖子當著我的面吹牛幸海,可吹牛的內(nèi)容都是我干的祟身。 我是一名探鬼主播,決...
    沈念sama閱讀 38,416評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼物独,長吁一口氣:“原來是場噩夢啊……” “哼袜硫!你這毒婦竟也來了?” 一聲冷哼從身側響起挡篓,我...
    開封第一講書人閱讀 37,053評論 0 259
  • 序言:老撾萬榮一對情侶失蹤婉陷,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后官研,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體秽澳,經(jīng)...
    沈念sama閱讀 43,558評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 36,007評論 2 325
  • 正文 我和宋清朗相戀三年戏羽,在試婚紗的時候發(fā)現(xiàn)自己被綠了担神。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片民假。...
    茶點故事閱讀 38,117評論 1 334
  • 序言:一個原本活蹦亂跳的男人離奇死亡叛溢,死狀恐怖廊移,靈堂內(nèi)的尸體忽然破棺而出啰挪,到底是詐尸還是另有隱情栅受,我是刑警寧澤矾屯,帶...
    沈念sama閱讀 33,756評論 4 324
  • 正文 年R本政府宣布滩届,位于F島的核電站诊沪,受9級特大地震影響忧吟,放射性物質(zhì)發(fā)生泄漏砌函。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,324評論 3 307
  • 文/蒙蒙 一溜族、第九天 我趴在偏房一處隱蔽的房頂上張望讹俊。 院中可真熱鬧,春花似錦煌抒、人聲如沸仍劈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,315評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽贩疙。三九已至讹弯,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間这溅,已是汗流浹背组民。 一陣腳步聲響...
    開封第一講書人閱讀 31,539評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留悲靴,地道東北人臭胜。 一個月前我還...
    沈念sama閱讀 45,578評論 2 355
  • 正文 我出身青樓,卻偏偏與公主長得像癞尚,于是被迫代替她去往敵國和親耸三。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,877評論 2 345

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,399評論 0 23
  • 1《新月集·仙人世界》感 他悄悄告訴我了我浇揩,他的仙人世界究竟在哪里仪壮。就在屋后花園的一角,那花兒開放的地方临燃。 他拉我...
    跬步堂閱讀 2,268評論 0 0
  • 我曾不止一次地設想過送兒子上大學時的惜別情景睛驳,情景里的我一定淚盈眼眶,心有不舍膜廊。沒想到在車站真的與兒子惜別時乏沸,卻找...
    詩意的云閱讀 702評論 3 6
  • (一) 寫英語作業(yè)的時候,才發(fā)現(xiàn)竟然忘記把筆記帶回來了爪瓜,考慮要不要重新寫筆記蹬跃,但是瞌睡蟲襲來,打敗了重新寫的想法铆铆,...
    龍眼花開的季節(jié)閱讀 265評論 0 0
  • 本人使用mvp很久了!很多意思自己也懂,但是有時候模糊現(xiàn)在我有幾個問題,大家看看時候也有類似疑惑 1,mvp 中P...
    liguiyun閱讀 161評論 0 0