Android Performance Optimization--the optimization of controls
The optimization of the image is described earlier, and the next step is to share the performance optimization of the control, which is primarily for custom View optimization.
1. First of all, let 's start with the 3 Errors We may make in custom View :
1) useless calls to OnDraw (): We know that calling View.invalidate () will trigger the redraw of the View , there are two principles to follow, the first 1 is to trigger the invalidate method only when the content of the View has changed , section 2 One is to use ClipRect and other methods to improve the performance of the drawing.
2) useless pixels: reduce unnecessary drawing elements when drawing, for those elements that are not visible, we need to avoid redrawing as much as possible.
3) Wasted CPU cycles: for elements not on the screen, you can use the 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.
Let me show you something about optimization.
1. Avoid performing operations that cause memory allocations in the OnDraw () method, such as new Paint ().
The first OnDraw () method is to execute on the UI thread , and in the UI thread try to avoid doing anything that might affect performance. Although allocating memory does not require too much system resources, this does not mean that there is no cost for free. The device has a certain refresh frequency, resulting in the View onDraw method will be frequently called, if the onDraw method is inefficient, Under the effect of frequent refresh, inefficient problems will be enlarged, and then there will be a serious impact on performance.
Call the invalidate () method to update the interface only when the view content changes .
For elements that are not on the screen, you can call the canvas.quickreject () method to eliminate the resource waste.
For example, when you swipe the interface, the system will frequently call the OnDraw () method, which will occupy a lot of memory in a short period of time. There will be memory jitter,theGC will be triggered frequently, will affect the efficiency of the CPU , and will cause a lot of battery consumption.
2, reduce The effect of alpha value on performance.
In a lot of customView, in order to make the interface more beautiful, set theAlphavalue, to letViewbecome transparent state, appear tall some, but this to performance is a certain burden. In general, for opaqueView, the display only needs to be rendered once, but if theViewSet theAlphavalue, it will need to be rendered at least two times. The reason is that it containsAlphaof theViewneed to know beforehand mixingViewwhat the next layer of elements is, and then combine the upperViewmakeBlendprocess color processing.
In some cases, a view with Alpha may trigger a change of view in the Hierarchyview The parent View on the page is redrawn once. 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 is rendered first(Blue, green, red), and then render the avatar image of the person. If the post-rendered element has a settingAlphavalue, then this element will be made with the rendered element on the screen .Blendprocessing. Many times, we will give the wholeViewSetAlphain order to achieveFadingThe animated effect if we figure in theListViewdoAlphathe gradual reduction of processing, we can seeListViewon theTextViewand so on, the component gradually blends into the background color. But in this process, we cannot observe that it has actually triggered an additional drawing task, and our goal is to make the entireViewgradually transparent, but the periodListViewdoing it all the while.Blendingoperations, which can cause 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 the GPU preprocessing, and then the GPU render to the screen, GPU you can rotate the raw data directly on the interface, set the 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.
How can I get the GPU to render a View ? We can specify how the View should be rendered using the setlayertype method , starting with the SDK , we can also use Viewpropertyanimator.alpha (). Withlayer ( ) to specify. As shown in the following:
Another example is a viewwith 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:
Android Performance Optimization--The optimization of controls