Android性能優(yōu)化:布局優(yōu)化

版權(quán)聲明:本文為Carson_Ho原創(chuàng)文章,轉(zhuǎn)載請(qǐng)注明出處![圖片上傳中...(微信圖片_20181227174249.png-5e47ed-1545903777462-0)]

目錄
一、影響的性能

布局性能的好壞 主要影響: Android應(yīng)用中的頁(yè)面顯示速度

二、如何影響性能

布局影響Android性能的實(shí)質(zhì):頁(yè)面的測(cè)量 & 繪制時(shí)間
1個(gè)頁(yè)面通過(guò)遞歸 完成測(cè)量 & 繪制過(guò)程 = measure、layout 過(guò)程

三、優(yōu)化思路
  • 優(yōu)化方向:布局性能、布局層級(jí)、布局復(fù)用性和測(cè)量&繪制時(shí)間
  • 具體如下
    針對(duì) 頁(yè)面布局的性能、層級(jí)、測(cè)量繪制時(shí)間 進(jìn)行優(yōu)化,從而提高 Android應(yīng)用中的頁(yè)面顯示速度
    4.具體優(yōu)化方案
  • 具體如下
  • 下面,我將詳細(xì)分析每種優(yōu)化方案

4.1 選擇耗費(fèi)性能較少的布局

  • 性能耗費(fèi)低的布局 = 功能簡(jiǎn)單 = FrameLayout、LinearLayout
  • 性能耗費(fèi)高的布局 = 功能復(fù)雜 = RelativeLayout

即布局過(guò)程需要消耗更多性能(CPU資源&時(shí)間)

    1. 嵌套所耗費(fèi)的性能 > 單個(gè)布局本身耗費(fèi)的性能
    1. 即 完成需求時(shí):寧選擇 1個(gè)耗費(fèi)性能高的布局,也不采用嵌套多個(gè)耗費(fèi)性能低的布局

4.2減少布局層級(jí)(嵌套)

  • 作用:減少 布局層級(jí)
    配合<include>標(biāo)簽使用,可優(yōu)化 加載布局文件時(shí)的資源消耗
    使用說(shuō)明:
    1. <merge>作為被引用布局A的根標(biāo)簽。
  • 2.當(dāng)其他布局通過(guò)<include>標(biāo)簽引用布局A時(shí),布局A中的<merge>標(biāo)簽內(nèi)容(根節(jié)點(diǎn))會(huì)被去掉,在<include>里存放的是布局A中的<merge>標(biāo)簽內(nèi)容(根節(jié)點(diǎn))的子標(biāo)簽(即子節(jié)點(diǎn)),以此減少布局文件的層次
/** 
 * 實(shí)例說(shuō)明:在上述例子,在布局B中 通過(guò)<include>標(biāo)簽引用布局C
 * 此時(shí):布局層級(jí)為 =  RelativeLayout ->> Button 
 *                                  —>> RelativeLayout ->> Button
 *                                                     ->> TextView
 * 現(xiàn)在使用<merge>優(yōu)化:將 被引用布局C根標(biāo)簽 的RelativeLayout 改為 <merge>
 * 在引用布局C時(shí),布局C中的<merge>標(biāo)簽內(nèi)容(根節(jié)點(diǎn))會(huì)被去掉,在<include>里存放的是布局C中的<merge>標(biāo)簽內(nèi)容(根節(jié)點(diǎn))的子標(biāo)簽(即子節(jié)點(diǎn))
 * 即 <include>里存放的是:<Button>、<TextView>
 * 此時(shí)布局層級(jí)為 =  RelativeLayout ->> Button 
 *                                ->> Button
 *                                ->> TextView
 * 即 已去掉之前無(wú)意義、多余的<RelativeLayout>
 */  

 // 被引用的公共部分:布局C = layout_c.xml
 <?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>

// 布局B: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>

4.2.2 合適選擇布局類型

  • 通過(guò)合理選擇布局類型,從而減少嵌套
  • 即:完成 復(fù)雜的UI效果時(shí),盡可能選擇1個(gè)功能復(fù)雜的布局(如RelativeLayout)完成,而不要選擇多個(gè)功能簡(jiǎn)單的布局(如LinerLayout)通過(guò)嵌套完成

4.3 提高 布局的復(fù)用性

  • 原因 :提取布局間的公共部分,通過(guò)提高布局的復(fù)用性從而減少測(cè)量 & 繪制時(shí)間
  • 優(yōu)化方案 :使用 布局標(biāo)簽 <include>

4.3.1使用布局標(biāo)簽
作用:實(shí)現(xiàn) 布局模塊化,即 提取布局中的公共部分 供其他布局共用。
具體使用

  • a. 通過(guò)<include>標(biāo)簽引入抽取的公共部分布局C

  • b. <include>標(biāo)簽所需屬性 = 公共部分的layout屬性,作用 = 指定需引入、包含的布局文件

    // 實(shí)例說(shuō)明:抽取 布局A、B中的公共部分布局C & 放入到布局B中使用
    
    /** 
      * 布局B: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" />
    
      // 通過(guò)<include>標(biāo)簽引入抽取的公共部分布局C
      // <include>標(biāo)簽所需屬性 = 公共部分的layout屬性,作用 = 指定需引入、包含的布局文件
      <include layout="@layout/layout_c.xml" />
    
     </RelativeLayout>
    
     /** 
       * 公共部分的布局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>
    

4.4減少初次測(cè)量&繪制時(shí)間
主要優(yōu)化方案:使用 布局標(biāo)簽<ViewStub> & 盡可能少用布局屬性 wrap_content
4.4.1使用布局標(biāo)簽

  • 作用:按需加載外部引入的布局。屬 輕量級(jí)View、不占用顯示 & 位置。
  • 應(yīng)用場(chǎng)景:引入 只在特殊情況下才顯示的布局(即 默認(rèn)不顯示)。如:進(jìn)度顯示布局、信息出錯(cuò)出現(xiàn)的提示布局等
    使用說(shuō)明:
    1. 先設(shè)置好預(yù)顯示的布局
    1. 在其他布局通過(guò)<ViewStub>標(biāo)簽引入外部布局(類似<include>);注:此時(shí)該布局還未被加載顯示
    1. 只有當(dāng)ViewStub被設(shè)置為可見(jiàn) / 調(diào)用了ViewStub.inflate()時(shí),ViewStub所指向的布局文件才會(huì)被inflate 、實(shí)例化,最終 顯示<ViewStub>指向的布局
/** 
 * 實(shí)例說(shuō)明:在布局A中引入布局B,只有在特定時(shí)刻C中才顯示
 */  

 // 步驟1:先設(shè)置好預(yù)顯示的布局B = 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="@dimen/dp_10"/>

    <TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_10"/>

</RelativeLayout>

// 步驟2:在布局A通過(guò)<ViewStub>標(biāo)簽引入布局B(類似<include>);注:此時(shí)該布局還未被加載顯示
// 布局A:layout_a.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" />

    <ViewStub
        android:id="@+id/Blayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout="@layout/layout_b" />

</RelativeLayout>

只有當(dāng)ViewStub被設(shè)置為可見(jiàn) / 調(diào)用了ViewStub.inflate()時(shí),ViewStub所指向的布局文件才會(huì)被inflate 、實(shí)例化,最終 顯示<ViewStub>指向的布局

ViewStub stub = (ViewStub) findViewById(R.id.Blayout);   
stub.inflate();

特別注意

    1. ViewStub中的layout布局不能使用merge標(biāo)簽,否則會(huì)報(bào)錯(cuò)
    1. ViewStub的inflate只能執(zhí)行一次,顯示了之后,就不能再使用ViewStub控制它了
    1. 與View.setVisible(View.Gone)的區(qū)別:View 的可見(jiàn)性設(shè)置為 gone 后,在inflate 時(shí),該View 及其子View依然會(huì)被解析;而使用ViewStub就能避免解析其中指定的布局文件,從而節(jié)省布局文件的解析時(shí)間 & 內(nèi)存的占用

4.4.2 盡可能少用布局屬性 wrap_content
布局屬性 wrap_content 會(huì)增加布局測(cè)量時(shí)計(jì)算成本,應(yīng)盡可能少用,在已知寬高為固定值時(shí),不使用wrap_content。
5.布局調(diào)試工具

  • 背景
    盡管已經(jīng)注意到上述的優(yōu)化策略,但實(shí)際開(kāi)發(fā)中難免還是會(huì)出現(xiàn)布局性能的問(wèn)題
  • 解決方案
    使用 布局調(diào)優(yōu)工具 。 此處主要介紹 常用的:hierarchy viewer、Lint、Systrace
    5.1 Hierarchy Viewer
  • 簡(jiǎn)介:Android Studio 提供的UI性能檢測(cè)工具。
  • 作用:可視化獲得UI布局設(shè)計(jì)結(jié)構(gòu) & 各種屬性信息,幫助我們優(yōu)化布局設(shè)計(jì)
  • 具體使用:Hierarchy Viewer 使用指南
    5.2 Lint
  • 簡(jiǎn)介:Android Studio 提供的 代碼掃描分析工具
  • 作用:掃描、發(fā)現(xiàn)代碼結(jié)構(gòu) / 質(zhì)量問(wèn)題;提供解決方案
    5.3 Systrace
  • 簡(jiǎn)介:Android 4.1以上版本提供的性能數(shù)據(jù)采樣 & 分析工具
  • 作用:檢測(cè) Android系統(tǒng)各個(gè)組件隨著時(shí)間的運(yùn)行狀態(tài) & 提供解決方案
總結(jié)
  • 本文主要講解Android 性能優(yōu)化中的 布局優(yōu)化
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在前面幾篇文章當(dāng)中,我們學(xué)習(xí)了如何通過(guò)合理管理內(nèi)存,以及高性能編碼技巧的方式來(lái)提升應(yīng)用程序的性能。然而實(shí)際上界面布...
    Ten_Minutes閱讀 1,616評(píng)論 1 8
  • 優(yōu)化思路與分析 視圖層級(jí)上的所有View都是必須的,不同的布局方式也可能對(duì)測(cè)量過(guò)程產(chǎn)生重要的影響。通常來(lái)說(shuō),你的視...
    theFullHorizon閱讀 1,262評(píng)論 0 2
  • 前面幾篇文章在前面幾篇文章當(dāng)中, Android 內(nèi)存泄漏和OOM分析(一) Android 內(nèi)存泄漏和OOM分析...
    蘇州丸子閱讀 1,033評(píng)論 0 0
  • 第532枚。 石材:壽山芙蓉石。 印面大?。?.7×2.1cm。 釋文:彩玉。 彩,文章也。(《說(shuō)文解字》)彩的本...
    半井居士閱讀 319評(píng)論 0 7
  • 小時(shí)候 喜歡依偎在你的懷里 那是世界上最溫暖的地方 喜歡吮吸你甘甜的乳汁 那便是世界最寶貴的財(cái)富 喜歡緊握你寬大的...
    寧爍詞閱讀 272評(píng)論 0 0

友情鏈接更多精彩內(nèi)容