1 Basic properties of the View

Int mLeft: distance from the left of the View to the left of the parent View int mTop: Distance from the top of the View int mRight: Distance from the left of the View to the left of the parent View int mRight: distance from the left of the View to the left of the parent View Int mBottom int mBottom int mBottom int mBottom The bottom distance of the View, the distance between the bottom of the View and the bottom of the parent View, the top four properties, are all specific to the original position of the View, which means that if the View slides, these four properties are the same

Int translationX: translationX is the horizontal offset of the View relative to the original position. The default is 0. Int translationY: translationY is the vertical offset of the View relative to the original position

Int x: distance from the left of the View to the left of the parent View int y: distance from the top of the View to the top of the parent View Int y: Distance from the top of the parent View

int x = mLeft + translationX;

int y = mTop + translationY;

Int mScrollX: The distance between the left edge of the View and the left edge of the View content: Left edge of View – Left edge of View content, so when the left edge of the View is right of the content, that is, when the left edge of the View is sliding to the left, mScrollX is positive, and when the left edge of the View is sliding to the left, mScrollX is negative, because the left side is the negative direction of the X-axis, so it can be abbreviated: negative is positive

Int mScrollY: The distance between the upper edge of the View and the upper edge of the View content: The top edge of the View – the top edge of the View content, so when the top edge of the View is below the content of the View, it’s going up, mScroll is going to be positive, and vice versa, because the top side is going in the negative direction of the Y-axis, so we can just call it negative is positive

View.scroll(20,20) does not move the View itself, but just slides the contents of the View (not including the background) (-20,-20)

Here’s a picture:

The black rectangle is the initial position, the orange rectangle is the position after the slide, and (x,y) represents the point in the upper left corner of the rectangle

Int MeasureWidth: View measure width, available after onMesasure() callback

Int mMeasureHeight: The height of the View measure, which can be obtained after the onMeasure() callback

GetWidth (): mright-mleft: gets the actual width of the View, which can be obtained after onLayout(), since mLeft, mTop, mRight, and mBototm are all determined after onLayout()

GetHeight (): mbottom-mtop: get the actual height of the View, which can be obtained after onLayout(), for the same reason as above

Click properties

Class MotionEvent {getX()/getY(): gets coordinates relative to the upper-left corner of the parent container getRawX()/getRawY(): gets coordinates relative to the upper-left corner of the screen}Copy the code

2. Basic method principles of View

Each View has an mParent property that points to its parent View. The top mParent is ViewRootImpl(that is, the mParent of the decorView). Each ViewGroup has an mChildren property, which represents its own set of child views, so that each View can access its parent View up, and can access its own set of child views down, the whole View system can be understood as a multi-cross tree, the top node is ViewRootImpl.

Measure the process

  • 1. View is a frame on the Window. View is a drawing paper
  • 2 The View measurement is passed from the Window. The View measurement in the Activity is in super.onResume()
  • The process starts with the DecorView at the top and starts with each View
  • If a View updates its size, it will call the parent’s requestLayout(), which in turn will call its parent’s requestLayout() until it decorates the View, and then measure() down to the View
  • 5 After measure() is passed, you can obtain your own width and height by using getMeasureWidth()/getMeasureHeight()
  • 6 custom View inherits directly from View, measure() needs to pay attention to compute the padding property and handle wrap_content

The Layout process

  • 1 Layout () is similar to Measure(), which is also a top-down process
  • GetLeft ()/getTop()/getRight()/getBottom(
  • 3 custom View, directly inherited from View, need to calculate the margin attribute

The Draw process

  • 1. The draw process requires understanding two coordinate systems:

View coordinate system: the coordinate system with the top left corner of the View as the origin

  • 2 to draw a View, first draw the View background in the View coordinate system, then transform to the View content coordinate system to draw the View content, the transformation process is realized by Canvas. Translation (x,y)

  • 3. The drawing of View is also top-down. After the parent View is drawn, the coordinate system will be converted to the View coordinate system, and then teach the View to draw. The parent View continues to teach the next View to draw until the drawing is complete

So every time a View is updated, it fires a requestLayout(), and the View fires a requestLayout() up the View tree, adding tags along the way, until you get to ViewRootImpl, Then ViewRootImpl goes to measure(), layout() and draw() from top to bottom. In measure()/layout()/draw(), each view will check whether it is marked. If it is not marked, it will directly bypass it. It performs its own measurement/layout/draw and then clears the tags, which is basically: up process: add tags; down process: detect tags; update itself clears the tags. The advantage of this is that it avoids the unpassed View, i.e. the View that hasn’t changed, being updated for nothing. Here’s an example:

If D changes its size or position, it calls its own requestLayout() to tag itself, and then looks for its parent View C, which then tags itself, up to A, which is 1,2,3.

Then A distributed downward to measure/layout/drawing, and clear markers along the way, which is 4 and 6, E and F, of course, can also be measured/layout/drawing, but because they are not marked, so don’t do anything, you can see, this way, can save A lot of unnecessary operation, improve the update efficiency of the View.