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

Custom Views

Custom Views

Fde10bcf0813b2162545477be4e7470b?s=128

Eugenio Marletti

November 25, 2015
Tweet

Transcript

  1. CUSTOM VIEWS ARE YOUR FRIENDS WHEN THEY’RE NOT TRYING TO

    KILL YOU
  2. Eugenio Marletti @workingkills

  3. “An app that doesn’t fart rainbows.”

  4. View.java 22.5k lines of code

  5. why custom? * encapsulate configuration * access protected methods *

    custom behaviour or drawing * performance
  6. * transform coordinates * clip rectangle or path (expensive) *

    save/restore clip and transform (matrix all the way down) Canvas * draw
  7. Canvas can draw color line rect circle arc path point

    text bitmap
  8. Paint color style stroke ...

  9. Shader PathEffect Xfermode text Paint advanced

  10. hardware acceleration * will make you suffer * enabled by

    default when target API >= 14 * many drawing calls are not supported * notable mention: setLayerType
  11. setLayerType LAYER_NONE LAYER_SOFTWARE LAYER_HARDWARE

  12. MEASURE LAYOUT DRAW

  13. DRAW * extend View or any descendent * override onDraw

    and draw on the Canvas * to trigger draw pass -> invalidate * tip: if no background -> setWillNotDraw(false)
  14. 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
  15. 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
  16. 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
  17. HOMEWORK * touch handling * animations * performance profiling *

    accessibility
  18. 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
  19. BIT.LY/CUSTOM-VIEWS-ANDROID @workingkills