How Android Draws Views,Android如何绘制View,
2016-12-29 16:38
459 查看
主要是了解为什么在自定义ViewGroup时onMeasure方法会调用两次。
When an
The Android framework will handle the procedure for drawing, but the
provide the root node of its layout hierarchy.
当一个Activity获得焦点后,会需要绘制Layout。Android framework将处理绘制流程,但是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
intersects the invalid region. In turn, each
for requesting each of its children to be drawn (with the
and each
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.
Layout将从根节点开始绘制,这需要测量绘制整棵layout树。绘制操作将遍历树,渲染与无效区域相交的区域。接着,每个ViewGroup负责要求每个子View被绘制,每个View负责自身绘制。由于整棵树按顺序遍历,意味着父View将在子View之前绘制,同辈则按照出现顺序绘制。
The framework will not draw
that are not in the invalid region, and also will take care of drawing the
for you.
You can force a
draw, by calling
Drawing the layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in
dimension specifications down the tree during the recursion. At the end of the measure pass, every
stored its measurements. The second pass happens in
Layout的绘制有两个过程:分别是Measure和Layout。Measure过程是measure(int, int)方法来实现,是自上而下的遍历View树。每个View在递归中都将尺寸规格传入View树。在Measure结束,每个view保存了他的测量值。第二步是layout(int, int, int, int),依旧是自上而下。在这个过程中每个父View负责使用在Measure过程中计算的尺寸绘制每个子View的具体位置。
When a
returns, its
must be set, along with those for all of that
measured width and measured height values must respect the constraints imposed by the
parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent
call
the parent may measure each child once with unspecified dimensions to find out how big they want to be, then call
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).
当一个view的
To initiate a layout, call
This method is typically called by a
it believes that is can no longer fit within its current bounds.
The measure pass uses two classes to communicate dimensions. The
is used by
and positioned. The base
describes how big the
dimension, it can specify one of:
an exact number
which means the
which means that the
(plus padding).
Measure过程使用两个类来进行计算尺寸。
确切数字
MATG_PARENT
WRAP_CONTENT
There are subclasses of
subclasses of
its own subclass of
the ability to center child
from parent to child. A
used by a parent to determine the desired dimension of a child
a
its child with the height set to
width of
to be given a width of 240 pixels.
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.
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.
EXACTLY:这个是父View指定子view具体值。子view
必须使用该尺寸,且保证所有后代view都配合这个尺寸。
AT
MOST:这个是父View指定最大尺寸给子View。子View必须使用该尺寸,且保证所有后代view都配合这个尺寸。
When an
Activityreceives focus, it will be requested to draw its layout.
The Android framework will handle the procedure for drawing, but the
Activitymust
provide the root node of its layout hierarchy.
当一个Activity获得焦点后,会需要绘制Layout。Android framework将处理绘制流程,但是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
Viewthat
intersects the invalid region. In turn, each
ViewGroupis responsible
for requesting each of its children to be drawn (with the
draw()method)
and each
Viewis 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.
Layout将从根节点开始绘制,这需要测量绘制整棵layout树。绘制操作将遍历树,渲染与无效区域相交的区域。接着,每个ViewGroup负责要求每个子View被绘制,每个View负责自身绘制。由于整棵树按顺序遍历,意味着父View将在子View之前绘制,同辈则按照出现顺序绘制。
The framework will not draw
Viewobjects
that are not in the invalid region, and also will take care of drawing the
Viewbackground
for you.
You can force a
Viewto
draw, by calling
invalidate().
Drawing the layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in
measure(int, int)and is a top-down traversal of the
Viewtree. Each
Viewpushes
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)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.
Layout的绘制有两个过程:分别是Measure和Layout。Measure过程是measure(int, int)方法来实现,是自上而下的遍历View树。每个View在递归中都将尺寸规格传入View树。在Measure结束,每个view保存了他的测量值。第二步是layout(int, int, int, int),依旧是自上而下。在这个过程中每个父View负责使用在Measure过程中计算的尺寸绘制每个子View的具体位置。
When a
Viewobject's
measure()method
returns, its
getMeasuredWidth()and
getMeasuredHeight()values
must be set, along with those for all of that
Viewobject's descendants. A
Viewobject's
measured width and measured height values must respect the constraints imposed by the
Viewobject's
parents. This guarantees that at the end of the measure pass, all parents accept all of their children's measurements. A parent
Viewmay
call
measure()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()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).
当一个view的
measure()返回,它的
getMeasuredWidth()和
getMeasuredHeight()值必须被设定过,后代也必须如此。一个View对象的测量的宽和高必须受到父veiw的强制约束。这保证了在Measure结束,所有的父View接受了所有View的测量值。一个父View会在它的子Veiw上执行measure()超过一次。例如:父view会先用unspecified尺寸方式Measure一次,探测所有子View的大小,然后再调用measure()一次,如果所有子View的非限制只存的和不是太大或太小,这次将使用确定值。(如果子View不满意被分派的空间大小,父view将会干预并在第二次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.LayoutParamsclass
is used by
Viewobjects to tell their parents how they want to be measured
and positioned. The base
ViewGroup.LayoutParamsclass just
describes how big the
Viewwants to be for both width and height. For each
dimension, it can specify one of:
an exact number
MATCH_PARENT,
which means the
Viewwants to be as big as its parent (minus padding)
WRAP_CONTENT,
which means that the
Viewwants to be just big enough to enclose its content
(plus padding).
Measure过程使用两个类来进行计算尺寸。
ViewGroup.LayoutParams类被view对象用来告知父view自身要如何测量和安置。
ViewGroup.LayoutParams的基本功能是描述view的宽和高。每个尺寸都可以按照如下形式描述:
确切数字
MATG_PARENT
WRAP_CONTENT
There are subclasses of
ViewGroup.LayoutParamsfor different
subclasses of
ViewGroup. For example,
RelativeLayouthas
its own subclass of
ViewGroup.LayoutParams, which includes
the ability to center child
Viewobjects horizontally and vertically.
MeasureSpecobjects are used to push requirements down the tree
from parent to child. A
MeasureSpeccan be in one of three modes:
UNSPECIFIED: This is
used by a parent to determine the desired dimension of a child
View. For example,
a
LinearLayoutmay call
measure()on
its child with the height set to
UNSPECIFIEDand a
width of
EXACTLY240 to find out how tall the child
Viewwants
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.
存在为不同ViewGroup子类服务对应的ViewGroup.LayoutParams的子类。例如:RelativeLayout拥有自己的ViewGroup.LayoutParams子类,它具有让子View横向或者竖向显示的功能。
MeasureSpec对象用来将需求从父view传入子view。一个MeasureSpec有三种模式:
UNSPECIFIED:这个是父View用来确认子View期望的尺寸大小。例如:一个LinearLayout会调用
measure(),设置子View的高为UNSPECIFIED,宽为具体值240,以此来确认子view以240像素宽能占据多高。
EXACTLY:这个是父View指定子view具体值。子view
必须使用该尺寸,且保证所有后代view都配合这个尺寸。
AT
MOST:这个是父View指定最大尺寸给子View。子View必须使用该尺寸,且保证所有后代view都配合这个尺寸。
相关文章推荐
- How Android Draws Views 安卓如何绘制view
- View 绘制机制 -- How Android Draws Views
- How Android Draws Views(Android 视图绘制机制)
- 【Android文档】Android界面绘制流程--------How Android Draws Views
- Android如何绘制Views
- Android是如何绘制View的
- 【Android】How Android Draws Views
- (翻译)API-How Android Draws Views
- android如何绘制view
- 安卓如何绘制视图-How Android Draws Views
- android 如何使用surfaceview绘制类似游戏手柄的摇杆?
- android:如何用一天时间,写出“飞机大战”这样的游戏!(无框架-SurfaceView绘制)
- Android 如何 绘制View
- Android Studio——Android View 如何绘制
- Android如何绘制View
- Android是如何绘制View的
- android 如何绘制view2
- How android draws views
- Android 之 TextView内部如何绘制Span样式
- android sdk 文档导读之一:android 如何绘制 Views