ConstraintLayout UI性能分析


ConstraintLayout, 2016年由Google I/O推出,Android Studio中, ConstraintLayout已替代RelativeLayout, 成為Hello World的默認布局.
關於ConstraintLayout的使用就不說了,請看https://developer.android.com/training/constraint-layout/index.html
或者 http://blog.csdn.net/guolin_blog/article/details/53122387
可以看出ConstraintLayout的優勢主要體現在兩點
1. 可以更方便的通過可視化拖拽的方式生成布局(大部分開發者可能更願意用xml代碼)
2. 復雜布局下終於可以不用一層套一層了
 
是不是打算把項目里的布局RelativeLayout都換成ConstraintLayout了?等一等...,ConstraintLayout 性能到底怎么樣?是不是任何布局都適合用ConstraintLayout?
 
 簡單寫個布局,對比一下
 
        

 

最簡單的根布局包含幾個TextView和ImageView,沒有嵌套,可以看出LinearLayout是渲染最快的,這個符合預期,重點比較RelativeLayout和ConstraintLayout,發現ConstraintLayout在onMesaure階段

耗時數倍於RelativeLayout。

再看下簡單嵌套布局ConstraintLayout表現怎么樣?

     

 

RelativeLayout和LinearLayout作為根布局下,又包含了一個LinearLayout的布局,ConstraintLayout當然不需要了

這幾種布局下,RelativeLayout和LinearLayout 加載速度相差無幾,ConstraintLayout的onMesaure又拖后腿了...

深度嵌套的復雜布局沒有試,但RelativeLayout肯定會隨着嵌套層數的增多 速度越來越慢,這時ConstraintLayout的優勢便體現出來了

 

Nicolas Roard是ConstraintLayout的開發人員

 

所以,我的建議是 簡單或者輕度嵌套繼續使用傳統布局(優先選擇LinearLayout),涉及到復雜布局及深度嵌套,ConstraintLayout更方便,性能也能體現出來優勢。

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM