In Android, View is the base class for all controls. It is an abstraction of the concept of controls and represents a control.

The View coordinate system

As shown in the figure:

Top: The top margin of the View in the parent component, which can be obtained using the getTop method.

Left: The left margin of the View in the parent component, which can be obtained using the getLeft method.

Right: The View’s right margin in the parent component, which can be obtained using the getRight method.

Bottom: The View’s bottom margin in the parent component can be obtained using the getBottom method.

The working process

View has three main processes:

measure

Measure the width and height of the View, just the measured value, not necessarily the final value. And in the ViewGroup, you also need to facilitate measure methods for child elements, which are then recursively executed all the way to the leaf node.

Create rules for MeasureSpec of a normal View

layout

Initialize mLeft, mBottom, mRight, and mTop in the Layout method to determine the component position and actual width and height.

If it is a ViewGroup component, the onLayout method is also called to calculate the position and width of the child components. It then facilitates the layout method of the child component and passes in the computed position and size.

The padding value of a custom control that directly inherits from a View or ViewGroup does not take effect by default and needs to be handled by itself. If the control directly inherits from a ViewGroup, it needs to handle not only the padding value but also the margin value of its child components

draw

  1. Draw background.draw(canvas)

  2. OnDraw yourself

  3. Draw Children (dispatchDraw)

  4. Draw decoration (onDrawScrollBars)

Call the draw method of the child element with dispatchDraw to pass the layers down.