Custom View-Draw

draw

  • Drawing is a two pass (two pass) process: a measure pass and a layout pass.
    • The measuring pass is implemented in measure(int, int), from top to bottom from the top of the tree. In this recursive process, each View will pass on its own dimension specifications. At the end of the measure pass, each View has stored its own measurements, that is, the measurement results.
    • The second is the layout pass (layout pass), which occurs in layout(int, int, int, int), which is still top-down.

void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

  • In general, overriding the onMeasure() method is to customize the View size rules. If the size of your custom View is consistent with the behavior of the parent control, you do not need to override the onMeasure() method
  • If you do not override the onMeasure method, the size of the custom view is the same as the parent control by default. Of course, you can also write the width and height in the layout file, and override this method to set the custom view size according to your needs.
  • When overriding the onMeasure method, you must call setMeasuredDimension(int,int) to store the measured width and height of this View. If this is not done, an IllegalStateException will be thrown by the measure(int, int) method.
  • Reset the size of the child view in the onMeasure() method in ViewGroup, as follows:
    • void measureChild(View child, int parentWidthMeasureSpec,
      int parentHeightMeasureSpec);设置指定的childView的MeasureSpec
    • void measureChildWithMargins(View child,
      int parentWidthMeasureSpec, int widthUsed,
      int parentHeightMeasureSpec, int heightUsed); When setting the MeasureSpec of childView, the padding and margin are added, as well as the specified additional widthUsed and heightUsed
    • void measureChildren(int widthMeasureSpec, int heightMeasureSpec); Set MeasureSpec for all childView
  • Although the parameters widthMeasureSpec and heightMeasureSpec are of int type, they do not represent the size of the view, but are the combined value of the bit operation performed by the size and mode.
    • Get the size (size) method: MeasureSpec.getSize(widthMeasureSpec); this is the size of the view
    • The method to get the mode (mode): MeasureSpec.getMode(widthMeasureSpec); Mode type is as follows:
      • UNSPECIFIED: The parent does not impose any constraints on the child, and the child can be of any size (that is, unspecified)

      • EXACTLY: The father determines the exact size of the child, and the child is limited to the given boundary, ignoring the size he wants

      • AT_MOST: The maximum size that the child can reach. When set to wrap_content, the mode is AT_MOST, which indicates the maximum size of the child view, so that the child view will set its own size according to this upper limit

  • The corresponding relationship between the parent control and the child control MeasureSpec is as follows:
    Insert picture description here

void onLayout(boolean changed, int left, int top, int right, int bottom)

  • The parameter in the onLayout() method in the custom ViewGroup is the difference between the coordinates of its four vertices relative to the upper left corner.
  • The parameter in the onLayout() method in the custom View is the difference between the coordinates of the four vertices relative to the upper left corner of the parent layout. The parameters of the onLayout() method in the View are described as follows:

Insert picture description here

  • Set the position of the childview by calculating the coordinates of the four vertices of the childview through certain rules according to the parameters, and use the view.layout() method to call the childview's onLayout() method.
  • The layout() method will calculate the values ​​of the 4 attributes of the view: mLeft; mTop; mBottom; mRight. The corresponding method is view.getLeft(); view.getTop(); view.getBottom(); view.getRight() The meaning is as follows
    • mLeft: the distance from the left edge of the child View to the left edge of the parent view
    • mTop: the distance from the upper boundary of the child View to the upper boundary of the parent view
    • mRight: the distance from the right edge of the child view to the left edge of the parent view
    • mBottom: the distance from the bottom margin of the child View to the upper boundary of the parent View

Guess you like

Origin blog.csdn.net/genmenu/article/details/88868533