About Android View Controls Controls in Android are roughly divided into two categories: ViewGroup and View. ViewGroup manages View as a container. Android view is a structure similar to the DOM tree. The parent view is responsible for measuring, positioning, drawing and other operations. The reason why the findViewById method we often use is expensive is because it is responsible for traversing the entire control tree from top to bottom to find the View instance. Try to use it as little as possible in repeated operations. Many controls currently in use are directly or indirectly inherited from View, as shown in the figure below. View inheritance tree Android UI interface architecture Each Activity contains a PhoneWindow object, and PhoneWindow sets DecorView as the root view of the application window. Inside it are the familiar TitleView and ContentView. Yes, the setContentView() that we usually use is the ContentView that is set. UI Architecture How does Android draw View? When an Activity is started, it will be asked to draw its layout. The Android framework will handle this request, of course, the premise is that the Activity provides a reasonable layout. Drawing starts from the root view and traverses the entire view tree from top to bottom. Each ViewGroup is responsible for drawing its own child Views, and each View is responsible for drawing itself. The drawing process is divided into three steps through the draw() method.
The entire drawing process is carried out in the performTraversals() method in ViewRoot. Part of the source code is as follows.
Of course, you need to know the size and drawing of the view before drawing. So first do measurement and layout (measurement and positioning), as shown below. Drawing process Measure process
Calculate the actual size of the view, get the height and width and store them in mMeasuredHeight and mMeasureWidth, the two parameters passed in by measure(int, int). MeasureSpec is a 32-bit int value, the upper 2 bits are the measurement mode, and the lower 30 bits are the measured size. The measurement modes can be divided into the following three types.
According to the source code above, the measure method cannot be overridden. The onMeasure method needs to be overridden during customization.
Looking at the source code, we can see that the final height and width are set by calling setMeasuredDimension(). If not overridden, the default is to directly call getDefaultSize to get the size. Use the getMeasuredWidth() and getMeasuredHeight() methods of View to obtain the width and height measured by the View. These two methods must be called after the onMeasure process to return valid values. Layout Process The Layout method is used to determine the position of the view layout, just like after you know the size of an object, you always need to know the position before you can draw it.
layout takes four parameters, the left, top, right, and bottom coordinates, relative to the parent view. Here you can see that the width and height just measured are used.
Set the coordinates via setFrame. If the coordinates have changed, reposition them. If it is a View object, onLayout is an empty method because the positioning is determined by ViewGroup. getWidth() and getHeight() will return the correct values only after layout is completed. A question arises here, what is the difference between getWidth/Height() and getMeasuredWidth/Height()?
getwidth Draw process
The key point is to call the onDraw method in the third step. The other steps are to draw some corners such as background, scrollBar, etc. Among them, dispatchDraw is used to recursively call the child View, and it is not needed if there is no child View. The onDraw method needs to be implemented by yourself, because the content drawn by each control is different. It is mainly drawn with the canvas object, so I won't talk about it here. References
|
<<: The essence of architecture, the way among thousands of methods
>>: 15 interesting facts about mobile apps
More than half of 2019 has passed. What new promo...
Surprise inspection ! You at this time How many g...
Author: Li Chuanfu Shi Xiangqi On December 17, 20...
Teacher Yuan Haotong - Introduction to mixing and...
This article shares with you an overview of the t...
PS Advanced Course: Commercial Product Refinement...
When friends and family get together during the S...
In the field of enterprise instant messaging, WeC...
In recent years, my country's new energy vehi...
Creator: China Digital Science and Technology Mus...
For most people, 3A masterpieces on console games...
2016 marks the third year since the establishment...
Recently, the return capsule of the Shenzhou 13 m...
According to official news, the new BYD Tang will...