Teach you to use the Android SDK Layout optimization tool layoutopt

Source: Internet
Author: User

Creating good-looking Android layouts is a challenge, and when you spend hours tweaking them to adapt to multiple devices, you usually don't want to readjust, but cumbersome nesting layouts are often inefficient, and fortunately, there is a tool in the Android SDK to help you optimize layouts to reduce memory consumption, Improve application run performance.

Optimization is a tricky, well-performing code is important, but the cost of writing good code is often very high, and you may not be premature to implement optimizations for those that run only once or temporarily, if your application is unresponsive and sells expensive, or slows down other applications in the system, Users will definitely respond and your app downloads will likely be affected.

Optimizing your layout as early as possible during development is an easy way to save costs and improve performance, and the Android SDK brings a tool that automatically analyzes your layouts and discovers layout elements that may not be needed to reduce the complexity of the layout.

The first step: Getting Ready for work

If you want to use the optimization tools available in the Android SDK, you need to work on the command line of the development system, and if you're not familiar with command-line tools, you'll have to study harder.

We strongly recommend that you add the path of the Android tool to the operating system environment variables, so that you can directly hit the name run related tools, or each time to enter the full file path at the command prompt, now there are two tools in the Android SDK directory:/tools and/ Platform-tools, this article mainly uses the Layoutopt tool located in the/tools directory, and I want to say that the ADB tool is located in the/platform-tools directory.

Run layoutopt

Running the Layoutopt tool is fairly straightforward, just keep up with the directory of the layout file or layout file as a parameter, and be aware that here you must include the full path to the layout file or directory, even if you are currently in this directory. Let's look at a simple example:

D:\d\tools\eclipse\article_ws\nothing\res\layout>layoutopt

D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml

D:\d\tools\eclipse\article_ws\Nothing\res\layout\main.xml

D:\d\tools\eclipse\article_ws\nothing\res\layout>

Note that in the example above, the full path to the file is included, and no content is output if the full path is not specified, for example:

D:\d\tools\eclipse\article_ws\nothing\res\layout>layoutopt Main.xml

D:\d\tools\eclipse\article_ws\nothing\res\layout>

So, if you don't see anything, it's likely that the file was not parsed, which means the file might not be found.

using layoutopt output

The output of layoutopt is only a suggestion, you can selectively adopt these suggestions in your application, and here are a few examples using LAYOUTOPT output recommendations.

a useless layout

During the layout design, we frequently move various components, some of which may end up being used, such as:

xml/html Code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="Horizontal">
  7. <linearlayout
  8. android:id="@+id/linearlayout1"
  9. android:layout_height="wrap_content"
  10. android:layout_width="wrap_content"
  11. android:orientation="vertical">
  12. <TextView
  13. android:id="@+id/textview1"
  14. android:layout_width="wrap_content"
  15. android:text="TextView"
  16. android:layout_height="wrap_content"></TextView>
  17. </linearlayout>
  18. </linearlayout>

The tool will soon tell us that the linearlayout inside the linearlayout is superfluous:

11:17 this linearlayout layout or it linearlayout parent is useless

Output results The first two digits of each line indicate the suggested line number.

root can be replaced

The output of the layoutopt is sometimes contradictory, for example:

xml/html Code
  1. <? XML version= "1.0" encoding="Utf-8"?>
  2. <framelayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6. <linearlayout
  7. android:id="@+id/linearlayout1"
  8. android:layout_height="wrap_content"
  9. android:layout_width="wrap_content"
  10. android:orientation="vertical">
  11. <TextView
  12. android:id="@+id/textview1"
  13. android:layout_width="wrap_content"
  14. android:text="TextView"
  15. android:layout_height="wrap_content"></TextView>
  16. <TextView
  17. android:text="TextView"
  18. android:id="@+id/textview2"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"></TextView>
  21. </linearlayout>
  22. </framelayout>

This layout will return the following output:

5:22 The Root-level <FrameLayout/> can is replaced with <merge/>
10:21 this linearlayout layout or it framelayout parent is useless

The

       first line proposal is feasible, but not required, we want two textview to be placed vertically, so linearlayout should be retained, and the second line of recommendations can be adopted, You can delete useless framelayout.

       Interestingly, this tool is not all-powerful, for example, in the example above, if we add a background property to Framelayout and then run the tool, The first proposal will of course disappear, but the second suggestion will still show that the tool knows we cannot control the background by merging, but after checking the linearlayout, it seems to forget that we have added a property that LinearLayout cannot provide to framelayout.

        too many views

        Each view consumes memory, too many views are placed in one layout, the layout consumes too much memory, and if a layout contains more than 80 views, layoutopt might suggest the following:
 
        -1:-1 This layout had too many views:83 views, it should had <= 80!  
 &nb sp;     -1:-1 This layout had too many views:82 views, it should had <= 80!  
        -1:-1 This layout had too many views:81 views, it should had <= 80! 

       the recommendation above is that the number of views cannot exceed 80, and of course the latest devices may be able to support so many views, but if there is a real performance situation, it is best to adopt this recommendation.

        nested too many

        layout should not have too many nesting, layoutopt (and Android team) suggested that the layout remains within 10 levels, even the largest tablet screen, the layout should not exceed 10 levels, relativelayout may be a solution, but its usage is more complex, Fortunately, the graphical Layout resource tool in Eclipse has been updated to make it all easier.

       The following is the output of layoutopt when the layout is too nested:
 
        -1:-1 This layout had too many nested layouts:12 levels, it should has <= 10!  
   ;     305:318 This linearlayout layout or it relativelayout parent is possibly useless         307:314 This linearlayout layout or it framelayout parent is possibly Useless&nbs p; 
       310:312 This linearlayout layout or it linearlayout parent is possibly useless 

Nested layout warnings are often accompanied by warnings of unwanted layouts, helping to find out which layouts can be removed, and avoid all screen layouts being redesigned.

Summary

Layoutopt is a quick and easy-to-use layout analysis tool to find inefficient and useless layouts, and what you have to do is decide whether to adopt the optimization recommendations given by layoutopt, although the adoption of recommendations to make changes will not immediately significantly improve performance, but there is no reason to need complex layouts to slow down the entire application, And later maintenance is also very difficult. Simple layouts not only simplify the development cycle, but also reduce test and maintenance effort, so you should optimize your layout as early as possible during application development, and don't wait until the end user feedback comes back to make changes.

Teach you to use the Android SDK Layout optimization tool layoutopt

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.