Complexity? • # of views • Depth of view hierarchy • (Really) # of measure() and layout() executions • Bad performance → UI/animations jank #PERFMATTERS
starts measure/layout starting at root. r r r r Some layouts need multiple measure/layout passes, e.g. RelativeLayout Lists make many copies of same layout Deep hierarchies increase complexity and dependency
Android Device Monitor • Best: use on physical device running 4.1 up • Do not use runtime numbers given in Hierarchy Viewer; completely inaccurate HIERARCHY VIEWER :
• Purpose: debug rendering of a layout • Also has high-level view of hierarchy useful to determining general depth/nested-ness • Similarly new Blueprint View LAYOUT INSPECTOR :
→ interactive reports • Frame rendering → how much time measure/layout takes up • warns of frames that exceed 16.6ms needed for 60fps • dumpsys: collects and dumps “interesting information” about system services status • adb shell dumpsys gfxinfo <PACKAGE_NAME> → perf info related to frames of animation • adb shell dumpsys gfxinfo <PACKAGE_NAME> framestats → detailed frame timing info; on Marshmallow SYSTRACE + DUMPSYS :
UI performance • Pub/Sub API → current app window • Equivalent to adb shell dumpsys gfxinfo <PACKAGE_NAME> framestats • Not limited to last 120 frames like framestats • Gather performance data and “catch regressions in UI performance” FRAMEMETRICSLISTENER :
not good ideas. • Good candidates for things that you should fix first. • Don’t nest weights • Remove useless views • Don’t nest too much MAKE LINT HAPPY :
opt for simplest • Often attributes or other techniques can replace multiple views, e.g.: • TextView compound drawables • Spannable • Directly apply margins, padding, backgrounds, etc. • Placeholder/divider → View or Space. • Sometimes you just need to pick the right view/layout. • Less views, less levels, less nesting • ConstraintLayout SIMPLIFY AND REDUCE :
Total control over measure/layout logic. • Mitigate double-layout phases. • Also: totally custom Views (draw all the things). • Start out with simple, straightforward layouts. • Balance performance gains with development effort. GO CUSTOM :
simplest solutions where possible • “Don’t do too much stuff.” • Fewer/flatter Views and ViewGroups. • Don’t use RelativeLayout at the root! • Don’t let problems accumulate. • Balance performance gains with development effort.