29
UI optimization of android applications Dino Kovač, Ivan Marić

Infinum Android Talks #09 - UI optimization

Embed Size (px)

Citation preview

Page 1: Infinum Android Talks #09 - UI optimization

UI optimization of android applications

Dino Kovač, Ivan Marić

Page 2: Infinum Android Talks #09 - UI optimization

–Donald Knuth

“Premature optimization is the root of all evil.”

Page 3: Infinum Android Talks #09 - UI optimization

Tools

• Hierarchy Viewer

• “Show GPU Overdraw” (Developer options, android 4.2+)

Page 4: Infinum Android Talks #09 - UI optimization

Example app• One relevant activity

• Two lists

• A compound view as the right list item

Page 5: Infinum Android Talks #09 - UI optimization

View tree

Page 6: Infinum Android Talks #09 - UI optimization

How android draws views

1. measure pass - each view stores its measurements

2. layout pass - each view positions its children

3. draw pass - each view draws itself

Page 7: Infinum Android Talks #09 - UI optimization

Focus of optimization• Flattening layout hierarchy

• Minimizing overdraw

Page 8: Infinum Android Talks #09 - UI optimization

Layout simplification

Page 9: Infinum Android Talks #09 - UI optimization

<merge />

Page 10: Infinum Android Talks #09 - UI optimization

<merge />

Page 11: Infinum Android Talks #09 - UI optimization

Overdraw

• Pixels get drawn more than once

• blue(2), green(3), light red(4), dark red(5)

• mostly multiple backgrounds

Page 12: Infinum Android Talks #09 - UI optimization

Tweaking the selector

Page 13: Infinum Android Talks #09 - UI optimization

Overdraw before/after

Page 14: Infinum Android Talks #09 - UI optimization

Some numbers

Hierarchy depth

Total views Measure Layout Draw Total

Before 9 76 0.354 1.7792 3.3624 5.4956

After 8 52 0.3402 1.0984 2.7098 4.1484

Page 15: Infinum Android Talks #09 - UI optimization

Space

• “a lightweight View subclass that may be used to create gaps between components”

• draw() method empty

Page 16: Infinum Android Talks #09 - UI optimization

ViewStub

• “an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime”

• does not support the <merge /> tag

Page 17: Infinum Android Talks #09 - UI optimization

ViewStub use case

Page 18: Infinum Android Talks #09 - UI optimization

ViewStub usage

((ViewStub) findViewById(R.id.stub_import)).inflate();

Page 19: Infinum Android Talks #09 - UI optimization

View tree before inflating

Page 20: Infinum Android Talks #09 - UI optimization

View tree after inflating

Page 21: Infinum Android Talks #09 - UI optimization

Think outside of boxIvan Marić

Page 22: Infinum Android Talks #09 - UI optimization

Problem

• Implement items preview in grid

• Enable swipe gesture

• Enable editing grid

• No limit to number of pages

Page 23: Infinum Android Talks #09 - UI optimization

Solution

• ViewPager with GridView

• Grid item view states:• Preview empty state• Item preview• Edit empty state

Page 24: Infinum Android Talks #09 - UI optimization
Page 25: Infinum Android Talks #09 - UI optimization

Optimize

• Simplify layout with just one view

• Eliminate measurement

• Custom view

Page 26: Infinum Android Talks #09 - UI optimization
Page 27: Infinum Android Talks #09 - UI optimization
Page 28: Infinum Android Talks #09 - UI optimization
Page 29: Infinum Android Talks #09 - UI optimization

References• http://www.youtube.com/watch?v=URyoiAt8098

• http://www.youtube.com/watch?v=-FUw8HMbmBQ

• http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-by.html

• http://developer.android.com/training/improving-layouts/optimizing-layout.html

• http://android-developers.blogspot.com/2009/03/android-layout-tricks-3-optimize-with.html

• https://github.com/reisub/android_uioptimizationexample