Android Rendering Optimization

Source: Internet
Author: User

Objective
It turns out my code is so rotten!

Why optimize
Lag, due to complex layout or excessive rendering of the interface does not occur within 16ms per frame.

A complex layout
Android system every 16MS issued vsync signal, triggering the UI rendering, every time the rendering is successful, it must reach a smooth screen required 60fps, otherwise, the phenomenon of dropped frames, the more dropped frames, the user clearly feel the lag.

Add:
1, FPS, display the number of frames per second, frame rate measurement units (frames per second);
2, why is 60fps (16MS=1000/60)? Because collaboration between the human eye and the brain is not able to perceive more than 60fps of screen updates.

Over-Drawing
Overdraw (over-drawn) refers to the system drawing pixels on the screen multiple times in a single rendered frame. For example, if we have a stack of UI cards, the invisible UI is doing the drawing, which wastes a lot of CPU and GPU resources.

Add:
Rendering operations typically depend on two core components: CPU and GPU. The CPU is responsible for calculating operations including Measure,layout,record,execute, and the GPU is responsible for rasterization (rasterization) operations.

How to detect
Show GPU Overdraw
Open the show GPU overdraw option to see if there is excessive drawing.
Steps:
Settings, developer options, debug GPU over drawing, display the over-drawing area.
Compare a overdraw reference map with blue, light green, light red and Crimson, which represents 4 different levels of Overdraw:

Blue: means Overdraw 1 time times, pixels plotted two times;
Green: means overdraw twice times, Pixels plotted three times;
Red: means that overdraw is 3 times times, and pixels are plotted four times;
Crimson: means that the overdraw is 4 times times, the pixels are drawn five times or more.

Our goal is to minimize the red overdraw and see more blue areas.

Profile GPU Rendering
Opens the profile GPU Rendering, showing the time to render per frame.
Steps:
Display as a bar on the screen for GPU rendering mode analysis, developer options, settings

A vertical histogram is displayed on the interface to indicate the time required to render each frame, and the higher the histogram, the longer the rendering time is spent. There is a green line in the middle, representing 16ms, and we need to make sure that the total time spent on each frame is lower than the horizontal line, so that the problem of stalling can be avoided.

Hierarchy Viewer
Check if the layout in activity is too complex with the Hierarchy Viewer tool
Steps:
Tools, Android, Android Device Monitor.

Open Hierarchy Viewe:
After launching Android Device Monitor successfully, click the Toggle View icon in the new window and select Hierarchy Viewe:

Using the Hierarchy Viewer:

In fact, the dots with red or xxx represent a slower view.

Friendly tips:
The app runs first and then uses Android Device Monitor, and the simulator is recommended, and the phone may not be able to read the content.

TraceView
Use TraceView to observe CPU performance, as described in: Profiling Tools Android TraceView.

Optimization
Remove unnecessary layout backgrounds
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:background="? attr/ BackgroundColor "android:orientation=" vertical "> < Com.wuxiaolong.pullloadmorerecyclerview.PullLoadMoreRecyclerView android:id= "@+id/pullloadmorerecyclerview" Android:layout_width= "Match_parent" android:layout_height= "Match_parent"/></linearlayout>
If here Pullloadmorerecyclerview also set the background color is not necessary.

In addition, using some of Android's own themes, window is added by default a solid color background, theme added android:windowbackground= "null" to remove or add the background color you need to reduce rendering.

Optimize the layout hierarchy
Improve performance by optimizing the view hierarchy to reduce the number of overlapping UI objects.
Here to correct me a mistake, I usually do not want to use relatelayout, because I do not want to name each ID, naming is a very headache, so I use the linearlayout layout more. To improve performance, use relativelayout as much as possible.

Using the Include, merge, Viewstub
1, include layout reuse;
2, merge to reduce the view level;
3. Viewstub tag is only loaded when you need it
Detailed Introduction: Android abstract layout--include, merge, viewstub

Custom Component OnDraw ()
1, avoid a large number of temporary objects, such as String, so as to avoid frequent triggering GC;
2. Consider using Canvas.cliprect () to draw areas that need to be drawn.

Listview
1, consider the use of viewholder;
2, or Recycleview to replace

Lint
Lint is a code scanning tool that can help us identify problems with the code structure. It is a good practice to run the lint tool on a layout file to search for possible view hierarchy optimizations.
Step: Android studio,analyze-Inspect Code.
The layout information is displayed under Android> lint> performance:

To see more details, you can click each item to expand it and see more information in the pane on the right side of the screen.
Lint rules:

Use compound drawables-a linearlayout which contains an ImageView and a TextView can is more efficiently handled as A Compound drawable.
Merge Root Frame-if a framelayout is the root of a layout and does not provide background or padding etc, it can be Replaced with a merge tag which are slightly more efficient.
Useless leaf-a layout that have no children or no background can often be removed (since it's invisible) for A Flatt Er and more efficient layout hierarchy.
Useless parent-a layout with children, a-a ScrollView or a root layout, and does not had A background, can be removed and has its children moved directly to the parent for a flatter and more efficient layout Hierarchy.
Deep layouts-layouts with too much nesting is bad for performance. Consider using flatter layouts such as relativelayout or GridLayout to improve performance. The default maximum depth is 10. The
Lint, in addition to layout optimization, also checks for issues such as encoding, accessibility, and so on.

Android Rendering Optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.