How Android Draws Views,Android如何绘制View,

主要是了解为什么在自定义ViewGroup时onMeasure方法会调用两次。

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获得焦点后,会需要绘制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 View that intersects the invalid region. In turn, each ViewGroup is responsible for requesting each of its children to be drawn (with thedraw() 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.

Layout将从根节点开始绘制,这需要测量绘制整棵layout树。绘制操作将遍历树,渲染与无效区域相交的区域。接着,每个ViewGroup负责要求每个子View被绘制,每个View负责自身绘制。由于整棵树按顺序遍历,意味着父View将在子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) 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) 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 View object's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of thatView 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 callmeasure() 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设置新的规则)

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 baseViewGroup.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 the View wants to be as big as its parent (minus padding)
  • WRAP_CONTENT, which means that the View wants 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.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.

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 child View. For example, a LinearLayout may call measure() 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.
  • 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都配合这个尺寸。

你可能感兴趣的:(Android学习)