How Android Draws Views、AndroidはどのようにViewを描きますか?

7170 ワード

主に、ViewGroupをカスタマイズするときにonMeasureメソッドが2回呼び出される理由を理解します.
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 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.
Layoutはルートノードから描画を開始し、layoutツリー全体の描画を測定する必要があります.ペイント操作は、ツリーを巡回し、無効な領域と交差する領域をレンダリングします.次に、各View Groupは、各サブViewが描画されるように要求し、各Viewは自己描画を担当する.木全体が順番に巡回するため、親Viewは子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() .
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  View has 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の描画には2つのプロセスがあります.それぞれMeasureとLayoutです.Measureプロシージャはmeasure(int,int)メソッドで実現され,上から下への遍歴Viewツリーである.各Viewは、再帰的に寸法仕様をViewツリーに転送します.Measureで終了し、各viewは彼の測定値を保存した.2つ目のステップは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 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()  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).
1つのviewのmeasure()が戻ると、そのgetMeasuredWidth()getMeasuredHeight() , 。 View veiw 。 Measure , View View 。 View Veiw measure() 。 : view unspecified Measure , View ,  measure()が に され、すべてのサブViewの が し、 きすぎたり さすぎたりしない 、 は が されます.( Viewが り てられたスペースサイズに していない 、 viewが し、2 のMeasureで しいルールを します)
To initiate a layout, call requestLayout() . This method is typically called by a  View  on 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:
  • 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プロセスでは、2つのクラスを して を します.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 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 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は のView Groupを っています.LayoutParamsサブクラスは、サブViewを または に する を えています.MeasureSpecオブジェクトは、 viewからサブviewに を するために される.MeasureSpecには3つのモードがあります.
  • UNSPECIFIED:これは Viewが Viewの の サイズを するために する.たとえば、LinearLayoutは、measure(), View UNSPECIFIEDを び し、 を の 240として、サブviewが240ピクセル でどれだけ いかを します.
  • EXACTLY:これは View サブview です.サブviewは、このサイズを し、すべての viewがこのサイズに することを する があります.
  • AT MOST:これは Viewが Viewに サイズを したものです.サブViewはこのサイズを し、すべての viewがこのサイズに することを する があります.