In-depth analysis of the Android view hierarchy, why is the UI interface so diverse?

brief description

In the Android Framework, the rendering mechanism refers to how to draw and layout views (View) for the application's user interface. Android's view hierarchy (View Hierarchy) is represented by each node in the view tree. When updating the view tree, Android performs the following process:

  1. After calling the setContentView() method of android.app.Activity or defining the view tree in the XML layout file, Android will calculate the position and size of each node according to the structure of the view tree to determine the boundary of each node.
  2. Android uses the Canvas class to draw UI elements and render them to the screen. The Canvas object provides developers with an API for drawing text, shapes, and bitmaps.
  3. During the rendering process, the Android Framework uses hardware acceleration technology to improve the performance of the view, such as using the GPU to render 3D graphics effects.
  4. Android introduces some UI performance optimization techniques, such as view reuse (View Recycling) and lazy loading (Lazy Loading), to avoid lag when UI rendering.
  5. When the boundary and content of the view tree change, the Android Framework will trigger the redrawing (Redraw) of the view tree. This means that Android calculates and draws new UI elements, and then updates the corresponding parts of the screen, rather than redrawing the entire screen.

Android Framewor rendering mechanism principle

In the Android Framework, the principles of the rendering mechanism mainly include the following aspects:

The drawing process of the view tree:

The view tree in the Android Framework is a tree structure composed of view nodes such as View Group and View. When Activity or Fragment needs to draw UI, the system will combine all view nodes into a view tree through ViewGroup. When the view tree is drawn, it will first traverse each ViewGroup node in the tree structure, then calculate the position and size of the node according to the properties of the node, and use the Canvas object to draw UI elements such as node background, border and shadow. Then traverse each View node in the tree structure, calculate the position and size according to the properties of the node, and use Canvas to draw text, pictures and other content on the node.

View tree caching mechanism:

View tree caching refers to caching the already drawn view tree in memory, so that the cached view tree can be used directly when it needs to be redrawn next time. In the Android Framework, the system uses the view tree caching mechanism to improve the fluency of the UI during the drawing process of the view. For example, when a node is occluded, the system can directly read the drawing result of the node from the view tree cache without drawing again.

Hardware acceleration mechanism:

The hardware acceleration mechanism means that the Android Framework hands over the rendering tasks of some UI elements to the GPU for execution, thereby improving the drawing efficiency of the UI. In the hardware acceleration mode, the system transfers a large number of UI drawing operations to the GPU for execution, such as converting complex drawing operations into OpenGL ES operations, using the GPU to render 2D and 3D graphics, and so on. In the Android Framework, the implementation of the hardware acceleration mechanism uses technologies such as the Skia graphics drawing library and the OpenGL ES graphics interface library.

Lazy loading mechanism:

Lazy loading means that when the system draws the UI, only some of the currently visible UI elements and their related UI elements are loaded instead of all of them. For example, in a ListView, only the currently visible list items will be loaded and rendered, not the entire list. In Android Framework, lazy loading mechanism helps.

Asynchronous rendering mechanism:

The asynchronous rendering mechanism means that the system renders UI elements in the background thread, reducing the occupation of the main thread for UI rendering, thereby improving the response speed of the UI. In the Android Framework, the implementation of the asynchronous rendering mechanism usually uses technologies such as multi-threading technology and asynchronous task framework.

The rendering mechanism of the Android Framework uses technologies such as the drawing process of the view tree, the view tree caching mechanism, the hardware acceleration mechanism, the delayed loading mechanism, and the asynchronous rendering mechanism to optimize the drawing efficiency and fluency of the UI.

code example

The following is a simple Android application code sample that demonstrates basic usage of the rendering mechanism in the Android Framework:

public class MainActivity extends Activity {
    private TextView textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        textView.setText("Hello, World!");
        textView.setTextColor(Color.RED);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
    }
}

In this example, we first set the layout of the Activity through the setContentView method, where activity_main is an XML layout file that contains a TextView component. Then, we use the findViewById method to obtain the reference of the TextView component, and set the text content through the setText method. The text color is set to red by the setTextColor method, and the text size is set to 24 SP (scaling independent pixels) by the setTextSize method. During this process, the Android system will calculate the position and size according to the properties of the View, and use the Canvas object to draw UI elements such as text and background.

This is just a simple example. In fact, in Android applications, we usually need to use more complex layouts and components, and need to make more custom settings for the position, size, style, animation, etc. of elements. However, whether it is a simple or complex UI design, the rendering mechanism of the Android Framework will automatically complete the drawing of the view for us in the background. For more Android framework learning, please refer to the document "Android Framework Family Bucket" ; it contains 98% of the framework's technology.

Summary of rendering mechanism

The rendering mechanism of the Android Framework is done automatically in the background, which is responsible for handling the rendering and drawing of the user interface (UI) of the Android application.

The rendering mechanism mainly includes the following aspects:

  • View Hierarchy (View Hierarchy): The UI of an Android application is usually organized in the form of a view hierarchy, which consists of multiple View (view) objects. Each View object has its own layout, style, properties, events, etc., and they can be nested, cascaded, overlapped, etc.
  • Measure, Layout, and Draw (Measure, Layout, Draw): After the view hierarchy is established, the Android system will automatically perform the measurement, layout, and drawing process in the background to determine the position and size of each view and draw it to the on the screen.
  • Graphics Fundamentals: The rendering mechanism of the Android Framework uses basic drawing tools such as Canvas and Paint to draw UI elements. These tools provide a wealth of properties and methods, including drawing text, graphics, colors, borders, shadows, gradients, etc.
  • Animation and Effect (Animation and Effect): Android Framework also provides a series of animation and effect support, allowing developers to achieve complex UI animation and interaction effects, such as displacement, scaling, transparency, rotation, gradient, etc.
  • Performance Optimization: In order to improve the performance and response speed of the application, the Android Framework also provides some optimization techniques, such as reusable layout, lazy loading, hardware acceleration, caching, recycling, etc.

In general, the rendering mechanism of the Android Framework is an automated process based on the view hierarchy, making full use of device hardware and software resources to achieve efficient, flexible and diverse UI rendering and drawing effects.

Guess you like

Origin blog.csdn.net/m0_71524094/article/details/130188985