Remember the home page of a car animation tuning

1, the background

Internationalization drivers side machine is uneven, Latin American driver models are very low, especially Brazil, pull omega data memory, 2 g and below accounted for 70% +, Australia and the driver side was high-end samsung machine average memory is more than 4 g, so our code, we have to “squeeze” mean in the allocation of CPU and memory.

2. Identify problems

After the intervention of caton optimization, it was found that there was a Top3 ANR and some related catons

At com.sdu.didi.com ANR 】 【 ponent. Controlpanel. ControlPanelView $15. OnAnimationUpdate (ControlPanelView. Java: 154)

In Latin America some configuration on general model easy to appear, mostly in the form of memory is only 1 g and 2 g models, according to can locate the ANR omenga information is due to the home page of the car animation in ripple attributes animation when refresh will be caton, caton serious ANR, and drive home page animation basic is running all the time, does affect the user experience

3. Analyze the problem

 

Analysis of the code



 

Took a look at the code

In order to achieve this infinite loop effect, I used a custom dot view, and then used the property animation to change the width and height of the dot, transparency, and then requestLayout when the animation changes. This effect is implemented, but the performance is very poor. Properties animation these operations are the main thread to perform in high-end models may be able to hold performance, but some Latin American phones directly can not hold, it seems that we need to squeeze our code.

 

4. Solve problems

SurfaceView

In Android, View is redrawn by refreshing, and the system redraws the screen by sending out VSYNC signal. The refresh interval is 16ms. If we can finish drawing within 16ms, there will be no problem. This will cause the interface to lag, affecting the user experience, so Android provides SurfaceView to solve this problem

SurfaceView inherits from View, which is a special View in Android.

  • The biggest difference between a View and a normal View is that it has its own Surface, corresponding WindowState in WMS and Layer in SurfaceFlinger
  • A typical Activity contains multiple views that form a tree of hierachy views, and only the top-level DecorView, the root View, is visible to THE WMS. This DecorView has a corresponding Windows State in THE WMS. Accordingly, the corresponding Layer in SF.
  • The SurfaceView comes with a Surface, which has its own WindowState in WMS and its own Layer in SF. While it is still in the View hierachy on the App side, it is separated from the host window on the Server side (WMS and SF). The advantage of this is that rendering of the Surface can be done in a separate thread with its own GL context. This is useful for performance-related applications such as games, videos, etc., because it does not affect the main thread’s response to events.

Combined with these features, SurfaceView is generally used to display complex UI and efficient images such as games, videos and photography, which require a separate thread to process. Its advantages are as follows

  • The SurfaceView updates the screen in its child thread, while the View updates the screen in its main thread.
  • SurfaceView is used for passive updates, such as frequent screen updates, while View is used for active updates, such as touch and click event responses.
  • The SurfaceView implements double buffering at the bottom, which is much more efficient than the View.

Here’s a clear explanation of how SurfaceView works

If the current screen needs to be constantly drawn or data processing is large, to avoid UI thread congestion, use SurfaceView instead of View. The SurfaceView has an independent drawing surface, that is, it does not share the same drawing surface with its host window. Because it has an independent drawing surface, the SurfaceView UI can draw lines in an independent thread. Since it does not occupy the main thread resources, it can achieve most complex and efficient interface drawing. Such as video playback VideoView and OpenGl ES GLSurfaceView live software non-stop location praise dynamic effect, weather software full screen rain and snow dynamic effect, water in the game, cloud and so on

SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView HandlerThread + SurfaceView

Let’s use a Mi 2S to see what it looks like.

 

5, effects,

Prerequisites: Xiaomi 2S has a memory of 2G, only runs 2 software (driver side, simulation positioning software), and opens the APP and puts it on the home page of driving without other operations **

Optimize CPU consumption before home page

 

You can see it’s about 30%, and then it starts to get hot

 

CPU consumption optimized using SurfaceView



 

You can see that CPU consumption has dropped from ** 30% to less than **! Achieve the desired effect

4. Some thinking

1, doing business at the same time to keep the technical thinking, not only can feedback business, and won’t get lost in the sea of business 2, need “for”, to turn to, is not expensive, for without change the error probability will be really high, but we do not have the Apollo switch: 3, to internationalize the driver side coding have a little bit more performance, Domestic Android phones are actually very good, sometimes do not care about some performance costs, which is a little different from international business 4, can be implemented in the business technology is a good technology, otherwise it may become “dragon slaying”