Performance-Optimized---layouts in android development

Source: Internet
Author: User

1. Basic

All the view in Android is "picture" on the phone screen, the system is every 16MS update activity in the content, so in order to let users do not see the lag, it is necessary to do everything possible to make the interface within 16MS changes to complete, Follow the principle of actually only one-----as far as possible to draw less things, so the efficiency will certainly improve, as to what reduce the layout level, avoid repeating drawing, summarize down or as far as possible to draw things.

Why is 16ms? Because the mobile phone in the market is generally 60hz, so 16ms/frame ≈1s/60hz

2. Methods

Use the developer tools on your phone to---> Debug GPU Over-drawing, and use this tool to detect layout levels.

The color of course is the more shallow the better, can show the view of their own color, then is the most ideal situation

  

2.1 Window's background

Window's Background property is the default in every topic, but in actual development, the background color of each interface is essentially set and will cover the entire screen, so it's better to just cancel it.
In addition, this is also a good way to solve the slow cold start of the app, the first way is to set the background of the form in order to be transparent, that is, when the user clicks the app, because the background of the form is transparent, the slow start will be attributed to the Android system, which makes the Android system without a lot of blame, Now the best way to solve the cold boot is to set up a picture, just like the normal start page, which gives the user a visual illusion of the second open.

Ps:
Although Android is relatively slow, but you get a one thousand or two thousand phone and someone else iOS five thousand or six thousand ratio? Funny

  

2.2 Choosing the best layout method

Android has 5 of the most commonly used layouts (Google's subsequent additionsGridLayoutFlexboxLayoutAndPercentRelativeLayoutetc.), but the most common isLinearLayoutAndRelativeLayout
Each oneViewGrounpBasically will be inonMeasuremethod to measure the size of the child view, in generalLinearLayoutThe measurement efficiency will be higher thanRelativeLayoutBecauseLinearLayoutThe number of measurements is low, andRelativeLayoutLayout of the relativity, so it is more than the number of measurements, after all, there is a possibility that the viewa in the lateral dependence on the VIEWB, and viewb in the longitudinal dependence on the Viewa
  

Therefore, under normal circumstances, it is best to useLinearLayout
Since there is a general situation, then there are two of cases @[email protected]!, isLinearLayoutUse in layoutsweightWeights to be laid out, this increasesLinearLayoutThe number of measurements, so that it andRelativeLayoutAt the same starting line, but it is well known that many times it is necessary to useweightproperty to do the evil screen adaptation, then the question comes, how to choose?
My personal metrics:
1. When the inclusion of a parent view has 3 levels of nesting, it is consideredRelativeLayout, I would definitely prefer to have 3 layers nested without the parent view.RelativeLayout
2. For not simply to the extremeListViewOrRecyclerView, I would basically chooseRelativeLayoutTo a layer of solution.
  

2.3 Removing a property resource that is completely impossible to use

Development time usually in the layout, will View set some data for layout reference, for example, to text set some text, give src or background set some resources or pictures (especially pictures), but when the program runs, these things 99% will not be used, But the Android system will still be silly to load these things, so do not assign values to these properties in the layout
  

But there's a problem,
If you do not assign a value, many people write layout without the data reference will be very unaccustomed, such as setting the size of the text, if the assignment will affect the efficiency, the total can not run and then delete one by one, so in case the effect is not ideal after running, and then add one by one,
So Google provides that the tools properties under that namespace will only be displayed in the layout page, not when the program is running
  

Another situation is to remove the completely obscured properties, such as the picture below the image of the ImageView part of the LinearLayout background color, this situation does not set the overall background color of the layout, individually set ListView or it item can ( Do not think you can not see the Android will not draw, Android if it is so smart, but also the programmer to do?
  
  

2.4 Margin and padding

This part belongs to the individual conjecture, padding and margin at times the effect is identical, but which is more efficient? I think it is padding because the full name of the ' margin ' property is layout_margin determined by the parent view, does that affect the speed of the parent view measurement sub view?

Performance-Optimized---layouts in android development

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.