HenCoder season 2: Customizing The View Part 2: Customizing the layout process starts with this episode. Seems a little excited.

Cut the nonsense and get to the point.

Introduction to the

As I said earlier, there are three key points to customizing a View: drawing, layout, and touch feedback. Last season told the drawing, although the content is much (like talked about 8?) But it’s actually the easiest. On the contrary, the layout process of technical knowledge, less content (should be 3 can be finished), but you need to understand its inner workings to understand its use, and its working principle is a bit convoluted. So if you follow the custom of HenCoder learning process layout, is roughly such an experience: watching a video or read articles, feel to absorb a great knowledge and information in the appearance of the great difficult to swallow, but when you really to swallow them, and then downloaded my practice project to do, but found that: Oh, my God. Is that all? How can customization of the layout process be so simple?

The word “easy” makes sense in many places, but it’s especially true when it comes to customizing the layout process.

For detailed concepts, principles, and technical details, check out the video below:

If can’t see the video, you can point links to the original station see: www.bilibili.com/video/av160…

conclusion

Someone said: What? So that’s the summary, right?

Ho ho, right. All of that was covered in the video, so here’s a summary of the key points from the video:

Meaning of the layout process

The layout process is the process of calculating the actual size of the program at runtime using the code of the layout file.

The work of the layout process

Two stages: the measurement stage and the layout stage.

Measurement: recursively call measure() for each View or ViewGroup from top to bottom, measure their size and calculate their position; Layout: Recursively call the layout() method of each View or ViewGroup from top to bottom, assigning the measured size and position to them.

The layout process of a View or ViewGroup

  1. In the measurement phase, the measure() method is called by the parent View, and after some preparation and optimization work is done in the measure(), the onMeasure() is called to carry out the actual self-measurement. OnMeasure () does things that View and ViewGroup do differently:

    1. View:ViewonMeasure()Will calculate its own size and save it;
    2. ViewGroup:ViewGrouponMeasure()Is going to call all of the subviewsmeasure()Let them measure themselves and calculate their actual size and position based on the expected size of the child View (in fact, 99.99% of parent views use the expected size of the child View as their actual size, for reasons that will be discussed in the next installment or so) and save. At the same time, it also calculates its own size based on the size and position of the child View and saves it;
  2. In the layout phase, the layout() method is called by the parent View. In layout(), it saves its own position and size passed in by the parent View, and calls onLayout() to do the actual internal layout. OnLayout () also does different things for View and ViewGroup:

    1. View: There is no child View, soViewonLayout()Do nothing.
    2. ViewGroup:ViewGrouponLayout()Will call all of its own subviewslayout()Method, pass their size and position to them, and let them complete their internal layout.

The layout process is customized

Three categories:

  1. rewriteonMeasure()To modify existing onesViewThe size of the;
  2. rewriteonMeasure()To the new custom customViewThe size of the;
  3. rewriteonMeasure()onLayout()To the new custom customViewGroupThe internal layout.

The first type of custom practices

To change the size of an existing View, rewrite onMeasure() :

  1. rewriteonMeasure()Method, and calls it insidesuper.onMeasure(), trigger the original self-measurement;
  2. insuper.onMeasure()The following is usedgetMeasuredWidth()getMeasuredHeight()To obtain the previous measurement results, and use their own algorithm, according to the measurement results to calculate new results;
  3. callsetMeasuredDimension()To save the new result.

Practice project

To avoid forgetting while the iron is hot, you are strongly advised to practice this exercise: HenCoderPracticeLayout1

Next up

The next installment is the final installment of the layout section: new custom View sizes.

Feel great?

So pay attention? Left left left