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.

액티비티가 포커스를 얻을 때, 그것은 자신의 레이아웃을 그리도록 요청될 것이다. 안드로이드 프레임워크는 그리기를 위한 절차를 처리할 것이다. 하지만 액티비티는 그것의 레이아웃 계층구조의 루트root 노드를 제공해야 한다.

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 View group 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.

그리기는 레이아웃의 루트root 노드를 가지고 시작된다. 그것은 레이아웃 트리를 측정하고 그리도록 요청된다. 그리기는 트리를 이동해 가면서 무효화invalid 영역과 만나는 각각의 뷰를 렌더링하는 것에 의해 처리된다. 순서대로, 각각의 뷰그룹은 그것의 자식들children 각각이 (draw() 메쏘드를 가지고) 그려지도록 요청하는 것에 대해 책임을 진다. 그리고 각각의 뷰는 그것 스스로를 그리는 것에 책임을 진다. 트리가 순서대로in-order 경유되기traversed 때문에, 이것이 의미하는 것은 부모parent가 그것의 자식들children 이전에 그려질 것이고, 형제sibling들은 트리에서 나타나는 순서대로 그려진다는 것이다.

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.

레이아웃을 그리는 것은, 측정 패스measure pass와 레이아웃 패스layout pass라는 두 번의 통과 과정pass process이다. 측정 패스는 measure(int,int)에서 구현되며, 뷰 트리를 위에서 아래로 경유traversal해 가면서 진행된다. 각 뷰는 재귀recursion되는 동안 크기 명세dimension specifications을 트리 아래로 보낸다. 측정 패스가 끝나면, 모든 뷰는 그것의 치수measurement(역주 : 뷰에서 필요한 크기)에 대한 저장을 마치게 된다. 두 번째 패스는 layout(int,int, int,int)에서 일어나며, 역시 위에서 아래로 진행된다. 이 패스 동안 각각의 부모parent는 측정 패스에서 계산된 크기를 사용해 그것의 자식들children 모두의 위치를 정하는 책임을 진다.

When a View's measure() method returns, its getMeasuredWidth() and getMeasuredHeight() values must be set, along with those for all of that View's descendants. A View's measured width and measured height values must respect the constraints imposed by the View'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 (i.e., 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).

뷰의 measure() 메쏘드가 리턴될 때, 그것의 getMeasuredWidth()와 getMeasuredHeight() 값은 그 뷰의 모든 자손descendant들의 값들과 함께 설정되어져야 한다. 뷰의 측정된 너비measured width와 측정된 높이measured height 값은 뷰의 부모parent에 의해 부과된 제약을 존중해야 한다. 측정 패스measure pass가 끝나면, 모든 부모parent는 그들 자식들children의 크기measurement 전부를 수용한다는 것을 보장한다. 부모parent 뷰는 그들의 자식들children에 대해서 한 번 이상 measure()를 호출할 수도 있다. 예를 들어 부모parent는 자식들children 얼마나 커질 수 있는지 찾기 위해 크기를 지정하지 않고(역주 : MeasureSpecs에 UNSPECIFIED 값을 설정함.) 각각의 자식child을 일단 측정할 수도 있다. 그런 다음, 만약 모든 자식들children의 자발적인 크기의 합이 너무 크거나 또는 너무 작다면 실질적인 숫자를 사용해서 그들에 대한 measure()를 다시 호출한다(즉, 만약 자식들(children)이 그들 각자가 얻을 공간이 얼마나 큰 지에 대해 그들끼리 동의하지 않는다면, 부모(parent)가 개입할 것이고 두 번째 패스에서 규칙을 정할 것이다).

The measure pass uses two classes to communicate dimensions. The View.MeasureSpec class is used by Views to tell their parents how they want to be measured and positioned. The base LayoutParams class just describes how big the View wants to be for both width and height. For each dimension, it can specify one of:

측정 패스measure pass는 크기를 전달하기 위해 두 개의 클래스를 사용한다. View.MeasureSpec 클래스는 뷰가 어떻게 측정되고 배치하길 원하는 지 그들의 부모에게 알리기 위해 사용된다. 베이스 LayoutParams 클래스는 단지 뷰가 너비와 높이 모두에 대해 얼마나 크게 되고 싶은 지를 기술할 뿐이다. 각각의 크기에 대해, 그것은 다음 중 하나를 지정할 수 있다.

  • an exact number
  • FILL_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).
  • 정확한 숫자
  • 뷰가 그 부모(패딩 제외)만큼 크고 싶어하는 것을 의미하는 FILL_PARENT
  • 뷰가 단지 그것의 내용(패딩 추가)를 감쌀 만큼 충분히 크고 싶다는 것을 의미하는 WRAP_CONTENT.

There are subclasses of LayoutParams for different subclasses of ViewGroup. For example, RelativeLayout has its own subclass of LayoutParams, which includes the ability to center child Views horizontally and vertically.

뷰그룹의 다른 서브클래스에 대한 LayoutParams의 서브클래스가 있다. 예를 들어 RelativeLayout은 그것 자신의 LayoutParams의 서브클래스를 가진다. 그것은 자식child 뷰를 수평 및 수직으로 가운데에 놓는 능력을 포함한다.

MeasureSpecs are used to push requirements down the tree from parent to child. A MeasureSpec can be in one of three modes:

MeasureSpecs는 부모에서 자식child으로, 트리 아래로 요구조건을 보내기 위해 사용된다. MeasureSpec은 세 가지 모드 중 하나가 될 수 있다:

  • 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 gurantee that it and all of its descendants will fit within this size.
  • UNSPECIFIED: 이것은 자식child 뷰가 원하는 크기를 결정하기 위해 부모parent에 의해 사용된다. 예를 들어 LinearLayout은 240 픽셀의 너비가 주어진 자식child 뷰가 원하는 높이가 얼마인지 알기 위해, UNSPECIFIED로 설정된 높이와 EXACTLY 240으로 설정된 너비를 사용해서 그것의 자식child 뷰에 대한 measure()를 호출할 수 있다.
  • EXACTLY: 이것은 자식child에게 정확한 크기를 부과하기 위하여 부모parent에 의해서 사용된다. 자식child은 이 크기를 사용해야 하며, 그것의 모든 자손들decendants은 이 크기 안에서 맞춰져야 하는 것을 보장해야 한다.
  • AT_MOST: 이것은 자식child에게 최대 크기를 부과하기 위해 부모parent에 의해서 이용된다. 자식child은 그것과 그것의 자손들descendants 모두가 이 크기 안에서 맞춰질 것이라는 것을 보장해야 한다.
↑ Go to top

← Back to User Interface