Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mobile Era 2016: Loving Lean Layouts

Mobile Era 2016: Loving Lean Layouts

Huyen's talk from Mobile Era 2016 about ways to improve UI performance and avoid jank by building efficient layouts.

Huyen Tue Dao

November 04, 2016
Tweet

More Decks by Huyen Tue Dao

Other Decks in Programming

Transcript

  1. WHY LEAN LAYOUTS PERFMATTERS → DEVICE RESOURCES USED RESOURCES ∝

    COMPLEXITY COMPLEXITY? # OF VIEWS. DEPTH OF HIERARCHY. REALLY # OF EXECUTIONS OF onMeasure()/onLayout() BAD PERFORMANCE→ JANK
  2. WHY LEAN LAYOUTS MAINTAINABILITY READABILITY COMPLEX VIEWS → HARD TO

    PARSE TOO NESTED → TOO INDENTED STABILITY CHANGE IN ONE PART AFFECTS OTHER PART
  3. PARENT MEASURES EACH CHILD CHILD CALCULATES MEASURED WIDTH/HEIGHT MEASURE HOW

    ANDROID DRAWS VIEWS onMeasure() LAYOUT onLayout() DRAW onDraw() MEASURE OWN CHILDREN (IF ANY)
  4. PARENT SIZES AND POSITIONS CHILD MEASURE LAYOUT DRAW HOW ANDROID

    DRAWS VIEWS onMeasure() onLayout() onDraw()
  5. PARENT DRAWS PARENT TELLS CHILDREN TO DRAW MEASURE LAYOUT DRAW

    HOW ANDROID DRAWS VIEWS onMeasure() onLayout() onDraw()
  6. WHERE IS THE PROBLEM? CHANGE IN SIZE/POSITION STARTS MEASURE/LAYOUT STARTING

    AT ROOT. SOME LAYOUTS NEED MULTIPLE MEASURE/LAYOUT PASSES, E.G. RELATIVELAYOUT LISTS MAKE MANY COPIES OF SAME LAYOUT DEEP HIERARCHIES INCREASE COMPLEXITY AND DEPENDENCY
  7. FINDING PROBLEMS HIERARCHY VIEWER SDK TOOL. HIERARCHY VISUALIZATION. ANDROID DEVICE

    MONITOR BEST → PHYSICAL DEVICE. ANDROID 4.1+ | ROOTED | VIEW SERVER. RUNTIME NUMBERS COMPLETELY INACCURATE. DO NOT USE.
  8. FINDING PROBLEMS LAYOUT INSPECTOR EXAMINE/DEBUG LAYOUT ANDROID MONITOR. SINCE AS

    2.2. HIGH-LEVEL LIST OF VIEWS. SHOWS NESTING. WIP. WILL “SUPERSEDE” HIERARCHY VIEWER.
  9. SYSTRACE SYSTEM + APPLICATION PROCESS EXECUTION DATA DATA TRACE →

    INTERACTIVE REPORTS FRAME RENDERING. HOW LONG TRAVERSALS TAKE. ALERTS. SMOOTH UI → 60FPS → 16.6MS/FRAME ANDROID DEVICE MONITOR FINDING PROBLEMS
  10. DUMPSYS SYSTEM SERVICES STATUS “INTERESTING INFORMATION” PERFORMANCE INFO FOR FRAMES

    OF ANIMATION adb shell dumpsys gfxinfo <PACKAGE_NAME> FINDING PROBLEMS DETAILED FRAME TIMING INFO. MARSHMALLOW. adb shell dumpsys gfxinfo <PACKAGE_NAME> framestats
  11. FRAMEMETRICSLISTENER MEASURE INTERACTION-LEVEL UI PERFORMANCE PUB/SUB API FRAME TIMING FOR

    CURRENT APP WINDOW FINDING PROBLEMS EQUIVALENT TO adb shell dumpsys gfxinfo <PACKAGE_NAME> framestats NOT LIMITED TO LAST 120 FRAMES LIKE framestats
  12. MAKE LINT HAPPY COMPLAINS ABOUT THINGS THAT ARE NOT GOOD

    IDEAS FIXING PROBLEMS GOOD CANDIDATES FOR THINGS TO FIX FIRST DON’T NEST WEIGHTS DON’T HAVE USELESS VIEWS DON’T NEST TOO DEEP
  13. SIMPLIFY AND REDUCE DIFFERENT WAYS TO DO ONE THING. OPT

    FOR SIMPLEST. FIXING PROBLEMS REPLACE VIEWS WITH ATTRIBUTES/OTHER TECHNIQUES DIRECTLY APPLY LAYOUT/STYLING PICK THE RIGHT VIEW/LAYOUT
  14. <LinearLayout…>
 
 <TextView…
 style="@style/StyleA" />
 
 <TextView…
 style="@style/StyleB" />
 


    <TextView…
 style="@style/StyleA" />
 
 </LinearLayout> <TextView…/> + Spannables → 4 VIEWS, 2 LEVELS 1 VIEW, 1 LEVEL
  15. <LinearLayout… >
 
 <LinearLayout…>
 
 <TextView… />
 
 <TextView… />


    
 </LinearLayout>
 
 <TextView… />
 
 </LinearLayout> 
 
 <ConstraintLayout…>
 
 <TextView… />
 
 <TextView… />
 
 <TextView… />
 
 </ConstraintLayout> → 5 VIEWS, 3 LEVELS 4 VIEWS, 2 LEVEL
  16. GO CUSTOM CUSTOM VIEW/VIEWGROUP TOTAL CONTROL OVER LAYOUT/DRAW FIXING PROBLEMS

    MITIGATE DOUBLE LAYOUT PASSES START WITH SIMPLE, STRAIGHTFORWARD LAYOUTS BALANCE PERFORMANCE GAINS WITH DEVELOPMENT EFFORT
  17. ANTICIPATE AND DEVELOP GOOD HABITS SIMPLEST SOLUTIONS WHERE POSSIBLE GOOD

    PRACTICES DON’T LET PROBLEMS ACCUMULATE BALANCE PERFORMANCE GAINS WITH DEVELOPMENT EFFORT FEWER AND FLATTER NO RELATIVELAYOUT AT ROOT
  18. REFERENCES 42 ANDROID PERFORMANCE PATTERNS, WHY 60FPS? https://youtu.be/CaMTIgxCSqU DEVELOPING FOR

    ANDROID: THE NAUGHTY BITS https://youtu.be/Q2qQoJlwqlk?t=36m4s HIERARCHY VIEWER http://developer.android.com/tools/help/hierarchy-viewer.html HIERARCHY VIEWER WALKTHROUGH http://developer.android.com/tools/performance/hierarchy-viewer/index.html LAYOUT INSPECTOR http://tools.android.com/tech-docs/layout-inspector
  19. REFERENCES 43 TESTING DISPLAY PERFORMANCE http://developer.android.com/training/testing/performance.html ANALYZING UI PERFORMANCE WITH

    SYSTRACE http://developer.android.com/tools/debugging/systrace.html OPTIMIZING LAYOUT HIERARCHIES http://developer.android.com/training/improving-layouts/optimizing-layout.html ANDROID PERFORMANCE PATTERNS, DOUBLE LAYOUT TAXATION https://www.youtube.com/watch?v=dB3_vgS-Uqo ANDROID PERFORMANCE PATTERN, INVALIDATIONS, LAYOUTS, AND PERFORMANCE https://youtu.be/we6poP0kw6E
  20. REFERENCES 44 BUILD A RESPONSIVE UI WITH CONSTRAINTLAYOUT developer.android.com/training/constraint-layout/index.html ANDROID

    DEVELOPERS BACKSTAGE: EPISODE 50: CONSTRAINT LAYOUT androidbackstage.blogspot.com/2016/06/episode-50-constraint-layout.html CUSTOM VIEWGROUPS https://sriramramani.wordpress.com/2015/05/06/custom-viewgroups/ MEASURE, LAYOUT, DRAW, REPEAT: CUSTOM VIEWS AND VIEWGROUPS https://youtu.be/dLl0ovmta6A