R PARENT PASSES CONSTRAINTS CHILD CALCULATES MEASURED WIDTH/HEIGHT MEASURE HOW ANDROID DRAWS VIEWS onMeasure() LAYOUT onLayout() DRAW onDraw() MEASURE OWN CHILDREN (IF ANY)
WHAT DO YOU NEED TO IMPLEMENT? VIEW MEASURE LAYOUT DRAW onMeasure() onLayout() onDraw() NO NO YES BUT YOU SHOULD REUSABILITY/FLEXIBILITY VIEW HAS NO CHILDREN REQUIRED FOR VIEW TO APPEAR
VIEW CONSTRUCTORS CREATE NEW FROM CODE View(Context context) View(Context context, AttributeSet attrs) CREATE FROM XML View(Context context, AttributeSet attrs, int defStyleAttr) CREATE FROM XML WITH A STYLE FROM A THEME ATTRIBUTE View(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) CREATE FROM XML WITH A STYLE FROM A THEME ATTRIBUTE OR STYLE RESOURCE A DEEP DIVE INTO ANDROID VIEW CONSTRUCTORS @DANLEW42
REFLECTING STATE CHANGES MARK A VIEW AS DIRTY (NEEDS A RE-DRAW) invalidate() RE-DRAWN “AT SOME POINT IN THE FUTURE” BE MORE PRECISE: invalidate(int l, int t, int r, int b)
THINGS TO REMEMBER WHEN DRAWING NO ALLOCATIONS IN ONDRAW. SERIOUSLY. INVALIDATE ONLY WHEN YOU NEED. INVALIDATE ONLY WHAT YOU NEED. DRAW IN PX. THINK IN DP AND SP. TEXT IS POSITIONED AT THE BASELINE
HOW MEASUREMENTS ARE MADE CHILD PARENT android:layout_width="match_parent" android:layout_height="wrap_content" setLayoutParams() How a child tells its parent how it wants to be laid out. MarginLayoutParams LinearLayout.LayoutParams 1Child defines LayoutParams in XML or Java getLayoutParams()
HOW MEASUREMENTS ARE MADE CHILD PARENT How a parent communicates constraints to children child.measure() int = { mode | size } AT_MOST X → Be any size up to X EXACTLY X → Be exactly X UNSPECIFIED → No constraints 1 2 Child defines LayoutParams in XML or Java Parent calculates MeasureSpecs and passes to child.measure()
HOW MEASUREMENTS ARE MADE Must call; otherwise → runtime exception. resolveSize(int size, int measureSpec) CHILD PARENT setMeasuredDimension() 1 2 3 Child defines LayoutParams in XML or Java Parent calculates MeasureSpecs and passes to child.measure() Child calculates width/height; setMeasuredDimension() getMeasuredWidth() getMeasuredHeight()
HOW MEASUREMENTS ARE MADE Child defines LayoutParams in XML or Java 1 Parent calculates MeasureSpecs and passes to child.measure() 2 Child calculates width/height; setMeasuredDimension() 3 Parent calls child.layout(); final child size/position. 4 CHILD PARENT child.layout() getWidth() getHeight()
WHAT DO YOU NEED TO IMPLEMENT? VIEWGROUP MEASURE LAYOUT DRAW onMeasure() onLayout() onDraw() YES YES NO HAVE TO MEASURE EACH CHILD: child.measure() (ABSTRACT) HAVE TO LAYOUT EACH CHILD: child.layout() DOES NOT DRAW BY DEFAULT setWillNotDraw
WHERE TO GO FROM HERE ACCESSIBILITY. A11Y FTW. ADD IT. @KELLYSHUSTER CUSTOM DRAWABLES. CUSTOM DRAWABLE STATES. @RHARTER @MARCOSPAULOSD ALL THE DRAWING OPERATIONS. PATHS. FILTERS. SHADERS. OH MY! @ROMAINGUY @CHIUKI HARDWARE ACCELERATION. LEARN ABOUT LAYERS + DISPLAY LISTS. @WORKINGKILLS GO CUSTOM. TOUCH. GESTURES. @PBREAULT
TOUCH/GESTURES Making Sense of the Touch System https://speakerdeck.com/ pbreault/making-sense-of-the-touch-system | https:// youtu.be/usBaTHZdXSI Making the View Interactive https://developer.android.com/ training/custom-views/making-interactive.html 45
DRAWING + DRAWABLES Fun with Android Shaders and Filters https://chiuki.github.io/ android-shaders-filters Don’t Fear the Canvas https://youtu.be/KH8Ldp39TUk Custom drawable states in Android http://charlesharley.com/2012/ programming/custom-drawable-states-in-android Custom Drawables http://ryanharter.com/blog/2015/04/03/ custom-drawables/ The Magic World of Drawables https://youtu.be/1YjB1uUfxgE Hardware Acceleration https://developer.android.com/guide/ topics/graphics/hardware-accel.html 47