The previous blog analyzes the direct cause of the slow app lag, and here we analyze some of the optimization scenarios (mainly from the layout related to direct impact rendering mechanism).
1) invalidations, Layouts, and performance (animation, layout optimization)
Smooth and subtle animations are one of the most important elements of app design, and these animations can dramatically improve the user experience. The following explains how the Android system handles update operations for UI components.
Typically, Android needs to convert an XML layout file into an object that the GPU can identify and draw. This operation is done with the help of Displaylist. Displaylist holds all the data information that will be given to the GPU to draw to the screen.
Displaylist is created for the first time a view needs to be rendered, and when the view is displayed on the screen, we execute the GPU drawing instructions to render. If you need to render the view again when you have to do something similar to moving the view, then we just need to do an extra render instruction. However, if you modify some of the visible components in the view, then the previous displaylist cannot continue to use, we need to go back and recreate a displaylist and re-execute the rendering instructions and update to the screen.
It is important to note that whenever the drawing content in the view changes, the creation of displaylist, rendering displaylist, and updating to the screen will be performed in a series of actions. The performance of this process depends on the complexity of your view, the state of the view, and the execution performance of the rendering pipeline. For example, suppose that the size of a button needs to be twice times larger than the current one, and the position of the other child view needs to be recalculated and placed by the parent view before the button size is increased. Modifying the size of the view will trigger the recalculation of the entire hierarcyview (especially when the animation is being drawn). Modifying the location of the view triggers the Hierarchview to recalculate the location of the other view. If the layout is complex, this can easily lead to serious performance problems. We need to minimize overdraw.
We can look at the performance of the rendering by using the monitor GPU rendering described earlier, or you can see the view update operation through the show GPU view updates in the developer options. Finally, we can also view the layout by hierarchyviewer this tool, making the layout as flat as possible (minimizing the number of layers), and removing the necessary UI components, which can reduce the computational time of Measure,layout, and when animating, The relationship between the moving element and its parent control should be considered, as far as possible during motion element movement, will not cause its parent control to change, otherwise it will cause the parent control to redraw, the entire redraw may affect the entire layout file, this is one of the possible reasons for the slow animation you make.
2) Overdraw, Cliprect, Quickreject (over-drawn)
One of the most important aspects of performance problems is the excessive complexity of drawing operations. We can use tools to detect and fix overdraw problems with standard UI components, but it's a bit of a problem for highly customizable UI components.
One trick is that we can significantly improve the performance of drawing operations by executing several APIs methods. As mentioned earlier, rendering updates for non-visible UI components can cause overdraw. For example, if the nav drawer is sliding out of the previously visible activity, it will lead to overdraw if you continue to draw UI components that are not visible in the nav drawer. To solve this problem, the Android system minimizes overdraw by avoiding drawing components that are completely invisible. The view that is not visible in the nav drawer is not wasted resources (for elements that do not need to be seen by the user, the property is set to gone, which optimizes performance to some extent, compared to the set to invisable).
Unfortunately, for overly complex custom view (overriding the OnDraw method), the Android system cannot detect what is going on in OnDraw, and the system cannot monitor and automatically optimize, and there is no way to avoid overdraw. But we can use Canvas.cliprect () to help the system identify those areas that are visible. This method allows you to specify a rectangular area that will be drawn only within this area, and other areas will be ignored. This API can be very helpful for those custom view that has multiple sets of overlapping components to control the displayed area. While the Cliprect method can also help conserve CPU and GPU resources, drawing instructions outside of the Cliprect area will not be executed, and components that are part of the content within the rectangular region will still be drawn.
In addition to the Cliprect method, we can also use Canvas.quickreject () to determine if a rectangle is not intersected, skipping the drawing operations in the non-rectangular areas. With those optimizations, we can look at the results using the show GPU Overdraw described above.
3) Custom views and performance (optimized for custom view)
The Android system offers more than 70 standard view, such as Textview,imageview,button and more. At some point, these standard view does not meet our needs, then we need to implement a view ourselves, this section will describe how to optimize the performance of custom view.
In general, for a custom view, we may make the following three errors:
· Useless calls to OnDraw (): We know that calling View.invalidate () will trigger the redraw of the view, there are two principles to follow, and the 1th is to trigger the Invalidate method only when the content of the view changes. The 2nd one is to use Cliprect and other methods to improve the performance of the drawing.
· Useless pixels: Reduce unnecessary drawing elements when drawing, for those invisible elements, we need to avoid redrawing as much as possible.
· Wasted CPU cycles: For elements not on the screen, you can use Canvas.quickreject to remove them to avoid wasting CPU resources. In addition, the GPU is used to render the UI as much as possible, which can greatly improve the overall performance of the program.
Finally, always keep in mind, as far as possible to improve the view of the drawing performance, so as to ensure that the interface refresh frame rate as high as possible.
4) Hidden Cost of Transparency (transparency effect of hidden performance consumption)
This section describes how to reduce the effect of transparent areas on performance. In general, for an opaque view, it only needs to be rendered once, but if the view is set with an alpha value, it will need to be rendered at least two times. The reason is that the view containing alpha needs to know in advance what the next layer of the hybrid view is, and then blend in with the upper view.
In some cases, a view that contains alpha may trigger a change to the view on the Hierarchyview and the parent view is redrawn again. Let's look at an example where the picture in the ListView and the two-level headings are set to transparency.
In most cases, the elements on the screen are rendered backwards and forwards. In the illustration above, the background map (blue, green, red) is rendered first and then the avatar image is rendered. If the post-rendered element has an alpha value set, the element will be blend with the rendered element on the screen. Most of the time, we will set alpha for the entire view to achieve fading animation effect, if we figure in the ListView to do a gradual reduction of alpha processing, we can see the ListView on the TextView and so on, and so on the component will gradually fused to the background color. But in this process, we cannot observe that it has actually triggered an additional drawing task, our goal is to make the entire view transparent, but during the time the ListView does not stop doing blending operations (because the GetView in the ListView adapter is frequently called, In addition to the ListView, which also includes Grideview, this can lead to a number of performance issues.
How to render to get the effect we want? We can first draw the elements on the view in the usual way from the back to the front, but not directly to the screen, but after using GPU preprocessing, and then GPU rendering to the screen, the GPU can be directly on the interface of the original data rotation, set transparency and so on. Rendering using the GPU, although the first operation is more time-consuming than drawing directly to the screen, once the raw texture data is generated, the next operation is less time-saving (in fact, the CPU is dropped on the GPU, and the overall performance is improved due to the GPU's texture mechanism).
How can I get the GPU to render a view? We can specify how the view should be rendered by Setlayertype, starting with SDK 16, and we can use Viewpropertyanimator.alpha (). Withlayer () to specify. As shown in the following:
Another example is a view with a shaded area, and this type of view doesn't have the problem we mentioned earlier because they don't have cascading relationships.
To make the renderer aware of this situation and avoid taking extra GPU memory space for this view, we can do the following settings.
With the above settings, performance can be significantly improved, as shown in:
Summary: Directly and rendering related to the layout file, in order to achieve the best program rendering, there are several principles 1. The number of layout layers is as low as possible (flattened). 2, try to avoid over-rendering. 3. Try to simplify layout, 4, often changing view layout depth as low as possible (each change will involve upper and upper control changes). This is a direct and rendering mechanism related to the layout, rendering also proposed requirements, each function of a relatively single module execution time as close as possible to 16ms (then the frame rate of 60fps), up to 32ms, if more will affect the user experience, in fact, 16ms is a frame does not fall of the drawing, From 16ms to 32ms this period of time dropped frames, but did not affect the user experience, but more than 32ms is very influential, lag slow appeared. As to how to shorten the time of each module, this will have to be the specific module to be decided. Here the 16ms and 32ms is the main thread of time, so generally time-consuming (such as network requests, file operations, and database operations, such as the typical functions of the optimization, it is difficult to compress the time to 16ms), this situation can use the additional threads to handle these taskbar, some people may have doubts, Since the thread can solve the problem, then I do not have to optimize the direct use of the thread is good, this argument is actually problematic, because thread management still takes up CPU time, if the number of threads, CPU management time will be more, It is worth noting that the first step of the rendering mechanism to parse the XML layout file (measuring the display List) work is also done by the CPU, so there may be a situation is to put the time-consuming tasks in the new thread, but still slow. Therefore, it is generally optimized to use the new thread when the distance is 16ms or far from the standard.
Android App performance optimization solves the layout optimization of the card slow-down