preface
- The custom
View
是Android
The basics developers must understand - There’s a lot about customization online
View
Principles of the article, but there are some problems:Incomplete content, unclear thinking, no source code analysis, simple problems complex and so on - Today, I will summarize customizations in a comprehensive way
View
I can guarantee that this isThe most comprehensive, clear, and understandable on the market
- This paper holds the principle of “conclusion first, detailed analysis later”, that is, let us know perceptual knowledge first, and then through rational analysis to understand the problem;
- So, please keep the conclusion in mind before moving on to the analysis;
- The article is long and takes a long time to read, so it is suggested to collect and wait for enough time to read
directory
1. Stock up on knowledge
1.1 ViewRoot
-
Define the connector, corresponding to the ViewRootImpl class
-
role
- The connection
WindowManager
和DecorView
- complete
View
Three processes:measure
,layout
,draw
- The connection
-
Pay special attention to
// In the main thread, the Activity object is created: // 1. Create a ViewRootImpll object root = new ViewRootImpl(view.getContent(),display); / / 3. ViewRootImpll object associated with DecorView root. SetView (view, wparams panelParentView)Copy the code
1.2 DecorView
- Definition: Top layer
View
The root node of the Android view tree; It’s also a subclass of FrameLayout
- Effect: Display & load layout
View layer events are passed through the DecorView and then to the View
- In particular, it contains 1 vertical direction
LinearLayout
, divided into two parts: on = title bar(titlebar)
, under = interior column(the content)
The layout file set by setContentView () in the Activity is actually added to the contents bar as its only child View = FrameLayout with id content
// The layout can be loaded by content in the code // 1. Get content ViewGroup Content = (ViewGroup)findViewById(Android.r.i.D.C ontent); ViewGroup rootView = (ViewGroup) Content.getChildAT (0);Copy the code
1.3 The relationship between Window, Activity, DecorView and ViewRoot
- Introduction to the
-
The relationship between
-
For more details, see the article: Android Custom View Basics: ViewRoot, DecorView & Window
1.4 Custom View basics
Before understanding the process of custom View, you need to understand a certain basis of custom View, see the specific article :(1) custom View basis – the most understandable series of custom View principle
2. Prepare the drawing
-
Recall the figure above, it can be seen that the last step = draw
-
But before drawing, there are some preparatory steps: create the PhoneWindow class, DecorView class, ViewRootmpl class, and so on
Therefore, below I will first draw before the preparation, and then start to talk about the drawing process
- Create and display a custom View on Android
3. Drawing process overview
- As can be seen from above,
View
The drawing process starts with:ViewRootImpl
The object’sperformTraversals()
- Source code analysis
/ * * * source analysis: ViewRootImpl performTraversals () * / private voidperformTraversalsMeasureHierarchy (Host, LP, RES,desiredWindowWidth, desiredWindowHeight); // 2. Execute performLayout(LP, mWidth, mHeight); // 3. Execute draw process performDraw(); }Copy the code
- From the above
performTraversals()
Unknown:View
The drawing process from the topThe View (DecorView)
theViewGroup
Start, layer by layerViewGroup
To the childView
Traverse surveying and mapping
That is, traversal from top to bottom, parent to child, each ViewGroup mapping all its children, and the lowest View mapping itself
- The process of drawing =
measure
Process,layout
Process,draw
The process is as follows
Next, I will elaborate on the three processes of View drawing: Measure process, layout process and draw process
4. Detailed introduction
4.1 Measure process
- Role in measuring
View
The width/height
- In some cases, multiple measurements are required
(measure)
To determine theView
Final width/height;- In this case,
measure
The width/height obtained after the process may be inaccurate;- Suggestions: In
layout
In the process ofonLayout()
To get the final width/height
- The specific process
- For more details, see the article: Custom View Measure Process.
4.2 the Layout process
- Function Computing view
(View)
The location of the
That is to calculate the position of the View’s four vertices: Left, Top, Right, and Bottom
- The specific process
- Detailed interpretation of the
Custom View Layout process – the most understandable custom View principle series (3)
4.3 the Draw process
-
Function To draw a View
-
The specific process
- Detailed explanation please see the article :(4) custom View Draw process – the most understandable custom View principle series
So far, about customizationView
The work flow is explained.
5. Customize the View steps
Step 1: Implement Measure, Layout and Draw processes
- From the View workflow (
measure
Process,layout
Process,draw
Procedure), if you want to implement customizationView
, depending on the type of custom View (singleView
/ViewGroup
), you need to customize the implementation of different methods - Main is:
onMeasure()
,onLayout()
,onDraw()
, as follows
Step 2: Customize the properties
- Create an XML file for custom attributes in the values directory
- Load custom XML file & parse property values in the constructor of custom View
- Use custom properties in layout files
6. Examples
Combining principle & implementation steps, if you want to achieve a custom View, please see the article: Hand to teach you to write a complete custom View
7. To summarize
This article summarizes the principle of custom View. By now, you should be familiar with the drawing process of a custom View. Other articles on customizing views:
(1) Custom View basis – the most understandable series of custom View principle
(2) Custom View Measure process – the most understandable series of custom View principle
(3) custom View Layout process – the most understandable custom View principle series
(4) custom View Draw process – the most understandable custom View principle series
Next, I will continue to explain the application of custom View. If you are interested, please continue to pay attention to Carson_Ho’s Android development notes