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会被系统请求绘制其自身的的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: 由父节点使用,指定子节点的最大尺寸. 子节点必须保证它及它的所有孩子都不得超过此尺寸.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 156,423评论 4 359
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,339评论 1 289
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 106,241评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,503评论 0 203
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 51,824评论 3 285
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,262评论 1 207
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,615评论 2 309
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,337评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,989评论 1 238
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,300评论 2 240
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,829评论 1 256
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,193评论 2 250
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,753评论 3 230
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,970评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,708评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,295评论 2 267
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,207评论 2 258

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 8,530评论 0 23
  • 1《新月集·仙人世界》感 他悄悄告诉我了我,他的仙人世界究竟在哪里。就在屋后花园的一角,那花儿开放的地方。 他拉我...
    跬步堂阅读 1,936评论 0 0
  • 我曾不止一次地设想过送儿子上大学时的惜别情景,情景里的我一定泪盈眼眶,心有不舍。没想到在车站真的与儿子惜别时,却找...
    诗意的云阅读 688评论 3 6
  • (一) 写英语作业的时候,才发现竟然忘记把笔记带回来了,考虑要不要重新写笔记,但是瞌睡虫袭来,打败了重新写的想法,...
    龙眼花开的季节阅读 250评论 0 0
  • 本人使用mvp很久了!很多意思自己也懂,但是有时候模糊现在我有几个问题,大家看看时候也有类似疑惑 1,mvp 中P...
    liguiyun阅读 152评论 0 0