View life cycle

OnMeasure (), onLayout(), and onDraw().

In onMeasure(), the son is measured first (the life cycle of the son is measured first), and the size of the son can be determined only when the son is determined.

Custom views fall into two categories:

  • The custom View

It inherits from a View, a SurfaceView, or some other View. OnMeasure ()–> onDraw() will execute, onLayout() depends on demand

  • Custom ViewGroup

OnMeasure ()–> onLayout() will be executed, depending on the requirements

What does a custom View contain

Onlayout onMeausre/Layout: viewGroup Display: onDraw: View: Canvas Paint matrix Clip Rect Animation Path line Text Drawing frameWork: Interaction: onTouchEvent: composite viewGroup

LayoutParams and MeasureSpec

LayoutParams

LayoutParams(layout parameters), which are defined in XML



To obtain

MeasureSpec

MeasureSpec is an int value (32), high two said the parent container restrictions on the layout of the view (Mode), low 30 said the Size of the view





1. Unspecified – unlimited

2. Exactly — exactly the size

3. At_most — Maximum

Implementing streaming layout

1. The ViewGroup inheritance

2. Custom ViewGroup needs to implement onMeasure() and onLayout()

3. If you want to measure your own size, you need to measure your son’s size first, so go through the son’s View and go to view.measure ().

4. The width and height of the son is calculated from his own LayoutParams on his MeasureSpec Mode +, which is the table above. If the father gives exactly the size and the son is android:layout_width=”10dp”, then it is 10dp

Step 5 Measure your son’s width and height

6. The width of the son is given, which is to calculate the width of each line (and take the maximum line width). Calculate the total height

7. The for loop is finished, so that each child has finished measuring, and knows the maximum width and height, so that they can measure their own width and height

8. However, his width and height are not only affected by his son, but also related to the Mode given by his father

So onMeasure () is done, onLayout ()

9. Since we have already determined which views to store in each row, we can store them in an array, which makes layout much easier

The important thing is to determine the top, bottom, left and right.

The top, bottom, left and right is the top left corner of the father’s frame

So the left side of the first view is going to be 0, and the right side is going to be left plus the width of the view. (Consider the view’s direct margin when calculating)

The second view is the right of the first view + the left of the second view…. And so on (used width and height)

supplement

The getMeasuredWidth () and the getWidth ()

View.getmeasuredwidth ();

This will have a value after the view.getMeasuredWidth() measure, but view.getwidth () will have to be drawn

So sometimes the view.getwidth () value will be 0, but refresh it or the next time you draw it.

Don’t forget view’s Visibility

There are hidden situations to consider