What is the drawing process for an Android view?
The drawing process of Android View can be summarized in the following steps:
- Measurement: In the measure() method of View, the system calculates the measured width and height of the View based on the View’s layout parameters and the requirements of the parent container.
- Layout: Within the View’s layout() method, the system determines the position of the View within its parent container based on the View’s layout parameters and the parent container’s requirements.
- Drawing: The system will use hardware acceleration or software rendering to draw the content of a View on the screen in the draw() method of the View.
- Touch event distribution: In the dispatchTouchEvent() method of the View, the system will pass the touch event from the parent container to the target View and sequentially call the View’s onTouchEvent() method to handle the touch event.
- Redrawing: When calling the invalidate() method on a View, the system marks the View as “in need of redrawing”, and will call the View’s draw() method again during the next drawing cycle to perform the redraw.
It is important to note that the above process is not completed all at once, but rather triggered as needed. For example, measuring, layout, and drawing processes are triggered when a View is displayed on the screen; touch event dispatch process is triggered when a user touches the View; and the redraw process is triggered when the invalidate() method is called.