Slide 1

Slide 1 text

CUSTOM VIEWS ARE YOUR FRIENDS WHEN THEY’RE NOT TRYING TO KILL YOU

Slide 2

Slide 2 text

Eugenio Marletti @workingkills

Slide 3

Slide 3 text

“An app that doesn’t fart rainbows.”

Slide 4

Slide 4 text

View.java 22.5k lines of code

Slide 5

Slide 5 text

why custom? * encapsulate configuration * access protected methods * custom behaviour or drawing * performance

Slide 6

Slide 6 text

* transform coordinates * clip rectangle or path (expensive) * save/restore clip and transform (matrix all the way down) Canvas * draw

Slide 7

Slide 7 text

Canvas can draw color line rect circle arc path point text bitmap

Slide 8

Slide 8 text

Paint color style stroke ...

Slide 9

Slide 9 text

Shader PathEffect Xfermode text Paint advanced

Slide 10

Slide 10 text

hardware acceleration * will make you suffer * enabled by default when target API >= 14 * many drawing calls are not supported * notable mention: setLayerType

Slide 11

Slide 11 text

setLayerType LAYER_NONE LAYER_SOFTWARE LAYER_HARDWARE

Slide 12

Slide 12 text

MEASURE LAYOUT DRAW

Slide 13

Slide 13 text

DRAW * extend View or any descendent * override onDraw and draw on the Canvas * to trigger draw pass -> invalidate * tip: if no background -> setWillNotDraw(false)

Slide 14

Slide 14 text

MEASURE & LAYOUT * override onMeasure * specs can be UNDEFINED, EXACTLY or AT_MOST * must call setMeasuredDimension * to trigger measure/layout pass -> requestLayout * children handle layout in onSizeChanged

Slide 15

Slide 15 text

CUSTOM LAYOUT * extend ViewGroup or any descendent * override onLayout and measure + layout each child * children ready in onFinishInflate * added/removed in OnHierarchyChangeListener * can use custom LayoutParams to hold child data

Slide 16

Slide 16 text

EXTRAS * save/restore instance state * use XML attrs * use attach state change callback * harness the power of drawable state * test your UI on slow and/or big devices WITH data binding

Slide 17

Slide 17 text

HOMEWORK * touch handling * animations * performance profiling * accessibility

Slide 18

Slide 18 text

LINKS Mastering the Android Touch System (talk) https://goo.gl/KzoUVE The mysterious case of who killed arcs on Android https://goo.gl/gWLZ71 Hinting Around: Android text demystified (talk) https://goo.gl/3vyehQ use the Source! Creating Custom Views (official documentation) http://goo.gl/cPbWQW Hardware Acceleration (official documentation) http://goo.gl/AEL1O5 Romain Guy blog http://goo.gl/iUjZPH Dianne Hackborn – about Hardware Acceleration https://goo.gl/tpZ7ex

Slide 19

Slide 19 text

BIT.LY/CUSTOM-VIEWS-ANDROID @workingkills