Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android UI 優化(一):減少布局層級,善用 HierarchyViewer 排查

編輯:關於 Android 程式設計

布局層級過深是 Android 性能問題裡最常見、也最容易被忽視的一類。每一次多余的 measure / layout 傳遞,都會在滑動時變成肉眼可見的掉幀。

一、避免布局鑲嵌過深

看下面這段結構:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/main_ll_duoyu"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_horizontal"
                android:layout_gravity="center_vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/hello_world" />
            </LinearLayout>
        </TextView>
    </LinearLayout>
</LinearLayout>

這個結構有幾處明顯的問題:

  1. id 為 main_ll_duoyu 的這個 LinearLayout 完全沒有承擔任何佈局職責,去掉它效果不變;
  2. TextView 裡嵌套 LinearLayout 是無效的——TextView 不是 ViewGroup,其子 View 根本不會被渲染;
  3. 最外層與第二層都是 match_parent 的 LinearLayout,層層包裹毫無意義。

我們完全可以去掉 id 為 main_ll_duoyu 的這個 LinearLayout,一樣能實現想要的效果:

更合理的寫法應該是只保留一層容器,用 gravity 和 padding 完成定位:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</LinearLayout>

二、用工具查看布局層級

工具路徑:

sdk/tools/hierarchyviewer.bat,雙擊打開。

HierarchyViewer 能直觀地展示當前的視圖樹結構、每個節點的測量與佈局耗時,還能導出屏幕的像素加載模式。

對比如下三張圖:

我們去掉 main_ll_duoyu 這個 LinearLayout 之後,對比前兩張圖和最後一張圖,中間整整少了一層。這裡只是舉個例子,實際項目中可以用 Google 提供的這些工具幫助我們優化 app,增加用戶體驗的流暢性。

使用 HierarchyViewer 時有幾個實用技巧:

  • 它只能連接 debuggable 的應用,release 包看不到;
  • 在 Tree View 中雙擊某個節點,右側會顯示該 View 的詳細屬性,包括 layout params、padding、visibility;
  • 點擊紅色或黃色的圓點,可以看到該節點在 measure、layout、draw 三個階段各花了多少時間——紅色代表是全組最慢的,最值得優化;
  • Pixel Perfect 標簽頁可以放大查看具體像素,用來排查圖片模糊、1px 邊線缺失這類問題很方便。

三、了解默認最大布局深度

Android 默認的最大布局深度為 10 層。

超過這個深度,在部分設備與系統版本上會出現渲染異常或性能急劇下降。雖然新版系統對這個限制有所放寬,但把它當作一條紅線來遵守仍然是明智的——層級控制在 5 層以內,通常就能保證良好的滑動流暢度。

補充:其他常見的層級削減手段

  1. 用 RelativeLayout / ConstraintLayout 替換多層 LinearLayout。LinearLayout 為了實現「平分」「權重」常常不得不嵌套三四層,而 RelativeLayout 可以通過相對定位一層搞定。ConstraintLayout 更是把扁平化做到了極致,配合 Guideline、Barrier 能實現幾乎任意複雜的界面。
  2. 用 <merge> 消除冗余根節點。當你 include 一個布局,而它的根節點與父容器類型一致時,用 <merge> 替換根節點可以直接省掉一層。
  3. 移除不可見的容器。始終不會用到的容器應該直接刪掉,而不是靠 setVisibility(GONE) 藏著;確實需要延遲使用的,考慮用 ViewStub。
  4. 避免 weight 濫用。LinearLayout 中使用 layout_weight 會導致每個子 View 被測量兩次(一次按 0 測,一次按剩餘空間測),嵌套使用時開銷成倍放大。
  5. 用 overdraw 檢查重疊繪製。開發者選項裡的「調試 GPU 過度繪製」能把界面按顏色分區,藍色代表 1 次,綠色 2 次,粉紅 3 次,大面積紅色就说明该區域背景被反復繪製,通常需要削減多層重疊的背景色。

UI 優化是個需要長期堅持的習慣,而不是一次性的運動。把 HierarchyViewer(新版本可用 Layout Inspector)加入日常自測流程,比事後追着掉幀問題跑要有效得多。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved