How Android Draws Views(
http://developer.android.com/guide/topics/ui/how-android-draws.html
当一个activity获取到焦点之后,他就会被要求重新绘制他的布局。Android framework 将会接受并处理重新绘制,但是activity必须提供其布局层次结构的根节点。
绘图从root节点开始。他需要测量和绘制布局树。绘画是由遍历树和呈现交叉无效区域的每个视图。反过来,每个ViewGroup负责要求他们的每个孩子被绘制(通过draw()方法),每个View被要求绘制他们自己本身。因为tree被顺序遍历,这意味着父母被绘制之后于(或,之前于)他们的孩子,有兄弟姐妹在树上画它们出现的顺序
绘制布局是两个pass的过程:一个测量传递,一个布局传递。测量传递通过measure(int,int)实现,是个从View tree的自顶向上的遍历。每个视图将尺寸规格在递归树中传递。在测量传递的最后,每个View都会保持自己的测量值。第二个传递,发生在layout(int, int, int, int)中,也是自上而下传递的。在这个传递中,每个父视图都有责任用measure传递中测量后的尺寸,将他们的子视图放在适当的位置。
当一个View对象的measure()方法返回时,他的getMeasuredWidth()和getMeasuredHeight()方法的值必须被设置,该View的全部子节点也要一同被设置。一个视图对象的测量宽度和高度测量值必须尊重其父视图的限制。这可以保证的措施通过,所有的父母都接受所有孩子的测量值。父视图可以在子View上不止一次地调用measure()方法。举个例子,
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:
- an exact number
MATCH_PARENT
, which means theView
wants to be as big as its parent (minus padding)WRAP_CONTENT
, which means that theView
wants to be just big enough to enclose its content (plus padding).
There are subclasses of ViewGroup.LayoutParams
for different subclasses of ViewGroup
. For example,RelativeLayout
has its own subclass of ViewGroup.LayoutParams
, which includes the ability to center child View
objects horizontally and vertically.
MeasureSpec
objects are used to push requirements down the tree from parent to child. A MeasureSpec
can be in one of three modes:
UNSPECIFIED
: This is used by a parent to determine the desired dimension of a childView
. For example, aLinearLayout
may callmeasure()
on its child with the height set toUNSPECIFIED
and a width ofEXACTLY
240 to find out how tall the childView
wants to be given a width of 240 pixels.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.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.
View
android.view.View
extends Object
implements Drawable.Callback KeyEvent.Callback AccessibilityEventSource
java.lang.Object | |
↳ | android.view.View |
![]() |
![]()
AbsListView, AbsSeekBar, AbsSpinner, AbsoluteLayout, ActionMenuView, AdapterView<T extends Adapter>, AdapterViewAnimator,AdapterViewFlipper, and 76 others.
|
Class Overview
这个类代表了用户界面组建的基本构建模块。一个View占据了屏幕上的一个矩形区域,并负责绘图和时间处理。View是窗口小部件的基本类,用来创建交互界面组件(如俺就,文本输入框等等)。ViewGroup是layout的基本类,它是一个无形的容器包含其他的View或者ViewGroup,并且定义他们的layout properities。
Developer Guides
For information about using this class to develop your application's user interface, read the User Interface developer guide.
Using Views
在window中所有的View被安排在同一树中。你可以通过code添加view,或者在一个或多个xml文件中指定View的tree