The sanctuary of calm and ambition | Technology will be eliminated, but thought will remain forever (think more, summarize, share more)
We know how we can write efficient layouts as our business becomes more responsible, the UI layout gets more complex, and a lot of layout information is bound to get a certain amount of performance loss.
1. Use <include> <merge> <viewStub> tags.
Https://developer.android.com/training/improving-layouts/optimizing-layout.html
2.? Reduce the use of the Layout_weight parameter in LinearLayout, because using this property causes the child element to be measured two times.
3. Learn to measure layout hierarchy and optimize layouts
https://developer.android.com/training/improving-layouts/optimizing-layout.html
4. Make layout wide and shallow, rather than narrow and deep (embodied in the Hierarchy Viewer's tree view)
When connected to the real machine, hierarchyviewer error: Unable to load the Windows data on the device
Cause: If you need to use hierarchyviewer, you need to open the corresponding Viewserver service on your phone. Hierarchy Viewer when connecting to a mobile phone, a client called View server must be started on the phone to communicate with the socket. In the commercial mobile phone, is unable to open the view server, therefore the Hierarchy Viewer is unable to connect to the ordinary commercial handset.
Workaround:
Method One: Use the emulator.
Method Two: The app needs to open the view Server, you can refer to the implementation of a class.
Http://pan.baidu.com/s/1sjIulYl? Extract code: WRC8
How to use:
Introduce the Viewserver.java class into the Util package, in the activity class that needs to see the layout hierarchy,
Add the following code:
protectedvoidonCreate(Bundle savedInstanceState) { |
????super.onCreate(savedInstanceState); |
????ViewServer.get(this).addWindow(this); |
????ViewServer.get(this).removeWindow(this); |
????ViewServer.get(this).setFocusedWindow(this); |
5. Use the lint tool to optimize layout
> Using the merge Root framework – if Framlayout is just a pure (no setting background, spacing, etc.) layout root element, we can use the merge tag as the root tag;
A useless branch – if a layout does not have any subcomponents, it can be removed to improve efficiency
A useless parent control – if a layout has only child controls, no sibling controls, and is not a scrollview or root node, and the background is not set, then we can remove the parent control and elevate it directly to the parent control
> Deep layout– Minimize the level of embedding, consider using more lateral components relativelayout or GridLayout to improve layout performance, the default maximum depth is 10
Lint tools for use in Android Studio:
Analyze→inspect code will automatically start the Lint tool at this time.
6. Although the official said LinearLayout and Relationlayout in the performance of the same, but can use relative layout where possible not to use LinearLayout.
7. Colleagues compared the performance of Relationlayout and Framelayout, and found that framelayout performance is better than relationlayout. The reason is simple: because Framelayout is simpler than relationlayout, there are not so many attributes, all more efficient. Whether you're using that type of layout, and if you can meet your needs, just remember that using attributes with less and simpler layouts must be true.
http://stackoverflow.com/questions/22875453/framelayout-vs-relativelayout-for-overlays
8. For layout of TextView and ImageView, use TextView Replace directly (Drawabletop similar attribute)