preface

  • inAndroidPerformance optimization strategies are very important in development
  • This article focuses on layout optimization for performance optimization. I hope you enjoyed it.

directory


1. Affected performance

The main impact of layout performance is: page display speed in Android applications


2. How does it affect performance

Layout affects the essence of Android performance: page measurement & drawing time

1 page through recursive measurement & drawing process = measure, layout process


3. Optimize your thinking

  • Optimization direction: layout performance, layout hierarchy, layout reusability, and measurement & drawing time
  • Specific as follows

Optimized for page layout performance, hierarchy, and measurement drawing time to improve page display speed in Android applications


4. Specific optimization plan

  • Specific as follows

  • Below, I examine each optimization in detail

4.1 Select a layout that requires less performance

  • Low performance cost layout = simple functionality =FrameLayout,LinearLayout
  • High-performance expensive layouts = complex features =RelativeLayout


    The layout process consumes more performance (CPU resources & time)








4.2 Reducing layout Levels (Nesting)

  • Principle: Fewer layers of layout ->> less work to draw ->> faster drawing ->> Improved performance
  • Optimization: Use layout tags<merge>& Suitable layout type

4.2.1 Using layout labels

  • Effects reduce layout levels

    Use with

    tags to optimize resource consumption when loading layout files

  • The specific use

<merge> as the root tag of referenced layout A // 2. When other layouts use the <include> tag to reference layout A, the <merge> tag content in layout A (root node) is removed. In <include>, the <merge> tag content in layout A (root node) is stored as the children of the <merge> tag content in layout A (root node). Example: In the above example, reference layout C * in layout B with the <include> tag. Layout hierarchy = RelativeLayout ->> Button * -- >> > TextView * Now use <merge> optimize: Change the referenced layout C root tag's RelativeLayout to <merge> * When referring to layout C, the <merge> tag content (the root node) in layout C is removed, <include> stores the children of the <merge> tag content (root node) in layout C. <Button> <TextView> <TextView> <TextView> <TextView> <TextView> <TextView> */ // Referenced common part: layout C = layout_c.xml <? The XML version = "1.0" encoding = "utf-8"? > <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="@dimen/dp_10"/> <TextView android:id="@+id/textview" android:layout_width="match_parent" Android :layout_height="@dimen/dp_10"/> </merge> Layout_b. XML <? XML version="1.0" Encoding =" UTF-8 "?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/Button" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dp_10" /> <include layout="@layout/layout_c.xml" /> </RelativeLayout>Copy the code

4.2.2 Choose the layout type appropriately

  • Reduce nesting by choosing layout types wisely
  • That is: complete the complexUIThe effect,Choose as complex a layout as possible (e.gRelativeLayout) instead of choosing multiple functional simple layouts (e.gLinerLayout) by nesting

4.3 Improve layout reusability

  • The reason is to extract common parts between layouts, reducing measurement & drawing time by improving layout reusability
  • Optimization scheme

    Using layout labels<include>

4.3.1 Using layout labels

  • Function to achieve layout modularization, that is, to extract the common parts of a layout for other layouts to share

  • The specific use

C // b. <include> tag required attribute = Layout attribute of public part, function = Specify the layout file to be included // Example description: Extract the common parts of layout A and B and put them into layout B using /** * layout B: layout_b.xml */ <? The XML version = "1.0" encoding = "utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/Button" android:layout_width="match_parent" Android :layout_height="match_parent" Android :layout_marginBottom="@dimen/dp_10" /> // Use the <include> tag to introduce the extracted public part layout C // <include> Required properties for the tag = Layout property for the common part, <include Layout ="@layout/layout_c.xml" /> </RelativeLayout> /** * Public layout C: Layout_c.xml */ <? XML version="1.0" encoding=" UTF-8 "?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="@dimen/dp_10"/> <TextView android:id="@+id/textview" android:layout_width="match_parent" android:layout_height="@dimen/dp_10"/> </RelativeLayout>Copy the code

4.4 Reduce initial measurement & drawing time

Main optimization: Use the layout tag

& use the layout attribute wrAP_content as little as possible

4.4.1 Using layout labels

  • The function loads externally introduced layouts on demand

    Note: a lightweight View, does not occupy the display & position

  • Application scenarios Introduce layouts that are displayed only in special cases (i.e., not by default)

    Such as: progress display layout, information error prompt layout, etc

  • The specific use

// 1. Set up the pre-display layout // 2. External layouts are introduced in other layouts via the <ViewStub> tag (similar to <include>); Note: The layout file pointed to by the ViewStub is synchronized and instantiated only when viewStub.inflate () is called and viewstub.inflate () is set to visible. // / Step 1: Set the pre-displayed layout B = layout_b.xml <? The XML version = "1.0" encoding = "utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="@dimen/dp_10"/> <TextView android:id="@+id/textview" android:layout_width="match_parent" Android :layout_height="@dimen/dp_10"/> </RelativeLayout> // Step 2: Use the <ViewStub> tag to introduce layout B (similar to <include>) in layout A; // Layout A: layout_a. XML <? The XML version = "1.0" encoding = "utf-8"? > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/Button" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="@dimen/dp_10" /> <ViewStub android:id="@+id/Blayout" android:layout_width="match_parent" android:layout_height="match_parent" android:layout="@layout/layout_b" /> </RelativeLayout> // Step 3: The layout file that the ViewStub points to is synchronized and instantiated only when ViewStub is set to visible/when viewstub.inflate () is invoked. ViewStub Stub = (ViewStub) findViewById(R.i.b.Layout); stub.inflate(); / / special attention / / 1. The layout of ViewStub can't use the merge label layout, otherwise an error / / 2. ViewStub inflate only executed once, shows, it can no longer use ViewStub control / / 3. The difference with view.setVisible (view.gone) : After the View's visibility is set to Gone, the View and its child views are resolved while the View is inflate. Using the ViewStub saves parsing time and memory for layout files by avoiding parsing the layout files specified thereinCopy the code

4.4.2 Use wrAP_content as little as possible

The layout attribute WRAP_content increases the computational cost of layout measurements and should be used as little as possible

Wrap_content is not used when the width and height are known to be fixed

conclusion

At this point, the layout optimization program is finished


5. Layout tuning tools

  • Background Although the above optimization strategies have been noted, layout performance problems are inevitable in actual development
  • The solution

    Use layout tuning tools


    Common ones are mainly introduced here:
    hierarchy viewer,
    Lint,
    Systrace

5.1 Hierarchy Viewer

  • This section describes the UI performance monitoring tool provided by Android Studio.

    • Function visualization of UI layout design structure & various attribute information to help us optimize layout design

    That is: easy to View the Activity layout, the attributes of each View, layout measurement – layout – drawing time

  • Hierarchy Viewer usage guide
  • 5.2 Lint

    • Introduction to the

      Android StudioCode scan analysis tool provided
    • role

      Scan and find code structure/quality issues; Provide solutions


      1. This process does not require a written test case
      2. LintEach problem found has a description & level (much like a bug found by testing) that makes it easy to locate the problem & resolve it in order of severity
      3. Lint usage guide is used


    5.3 Systrace

    • Introduction to the

      The Android 4.1Performance data sampling & analysis tools provided in previous versions
    • Function to detect the running status of each component of the Android system over time & to provide solutions

      1. Collect and other operational information to help developers more intuitively analyze system bottlenecks and improve performance

        The detection scope includes:AndroidCritical subsystems (e.gWindowManagerServiceFrameworkSome key modules), service, View system
      2. Features include: tracking systemI/OOperations, kernel work queues,CPULoad, etc., provides good data on UI display performance analysis, especially on issues such as poor animation playback, rendering lag, etc
    • Systrace Usage Guide


    6. Summary

    • This paper mainly explainsAndroidLayout optimization in performance optimization

    • I’m going to go into more detailAndroidPerformance optimization, interested can continue to pay attention toCarson_Ho android Development Notes

    Please give the top/comment a thumbs up! Because your encouragement is the biggest power that I write!