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

Custom Views

Custom Views

Eugenio Marletti

November 25, 2015
Tweet

More Decks by Eugenio Marletti

Other Decks in Programming

Transcript

  1. * transform coordinates * clip rectangle or path (expensive) *

    save/restore clip and transform (matrix all the way down) Canvas * draw
  2. hardware acceleration * will make you suffer * enabled by

    default when target API >= 14 * many drawing calls are not supported * notable mention: setLayerType
  3. DRAW * extend View or any descendent * override onDraw

    and draw on the Canvas * to trigger draw pass -> invalidate * tip: if no background -> setWillNotDraw(false)
  4. 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
  5. 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
  6. 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
  7. 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