Slide 1

Slide 1 text

by Cyril Mottier CRAFTING CUSTOM ANDROID VIEWS

Slide 2

Slide 2 text

@cyrilmottier

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

https://www.capitainetrain.com/jobs

Slide 5

Slide 5 text

Introduction to Android Views 1

Slide 6

Slide 6 text

DEFINITION building block The basic for user interface components

Slide 7

Slide 7 text

The Android SDK is bundled with several built-in Views TextView ImageView Button View ViewGroup LinearLayout RelativeLayout ProgressBar ViewStub

Slide 8

Slide 8 text

View

Slide 9

Slide 9 text

View TextView ImageView

Slide 10

Slide 10 text

View TextView Button ImageView

Slide 11

Slide 11 text

View TextView ViewGroup Button ImageView

Slide 12

Slide 12 text

View TextView ViewGroup Button ImageView LinearLayout FrameLayout

Slide 13

Slide 13 text

View TextView ViewGroup Button ImageView LinearLayout FrameLayout

Slide 14

Slide 14 text

What are Views made for ?

Slide 15

Slide 15 text

A View is responsible for MEASURING

Slide 16

Slide 16 text

LAYOUTING A View is responsible for

Slide 17

Slide 17 text

DRAWING A View is responsible for

Slide 18

Slide 18 text

Saving UI state Managing a Drawable state Handling touch events etc. RESPONSIBILITIES

Slide 19

Slide 19 text

Why develop custom components ?

Slide 20

Slide 20 text

Factorize Optimize Innovate Extend 4main purposes

Slide 21

Slide 21 text

1 Factorize Optimize Innovate Extend Avoid creating the same snippets of code over and over again over over over over over ove

Slide 22

Slide 22 text

Factorize 2 Optimize Innovate Extend Speed up UI rendering

Slide 23

Slide 23 text

Factorize Optimize 3 Innovate Extend Create something unique & groundbreaking

Slide 24

Slide 24 text

Factorize Optimize Innovate 4 Extend Access View’s protected behaviors

Slide 25

Slide 25 text

Creating compound controls 2

Slide 26

Slide 26 text

Demo ProgressIndicator

Slide 27

Slide 27 text

Extending an existing View 3

Slide 28

Slide 28 text

Demo UnderlinedTextView

Slide 29

Slide 29 text

Creating a completely custom View 4

Slide 30

Slide 30 text

completely custom = ˝ ˝

Slide 31

Slide 31 text

extend View completely custom = ˝ ˝

Slide 32

Slide 32 text

completely custom = ˝ ˝ extend View ViewGroup

Slide 33

Slide 33 text

passes to render a View hierarchy 3 Android does

Slide 34

Slide 34 text

1 measure 2 layout 3 draw } } requestLayout() invalidate()

Slide 35

Slide 35 text

MEASUREMENT void onMeasure( int widthMeasureSpec, int heightMeasureSpec); Contract Call setMeasuredDimension(int, int)

Slide 36

Slide 36 text

LAYOUT void onLayout( boolean changed, int lelf, int top, int right, int bottom)

Slide 37

Slide 37 text

A View can have a persisting state STATE SAVE/RESTORE

Slide 38

Slide 38 text

static class SavedState extends BaseSavedState { int progress; SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); progress = in.readInt(); } @Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(progress); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public SavedState createFromParcel(Parcel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; }

Slide 39

Slide 39 text

static class SavedState extends BaseSavedState { int progress; SavedState(Parcelable superState) { super(superState); } private SavedState(Parcel in) { super(in); progress = in.readInt(); } @Override public void writeToParcel(Parcel out, int flags) { super.writeToParcel(out, flags); out.writeInt(progress); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public SavedState createFromParcel(Parcel in) { return new SavedState(in); } public SavedState[] newArray(int size) { return new SavedState[size]; } }; }

Slide 40

Slide 40 text

@Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.progress = mProgress; return ss; } @Override public void onRestoreInstanceState(Parcelable state) { SavedState ss = (SavedState) state; super.onRestoreInstanceState(ss.getSuperState()); setProgress(ss.progress); }

Slide 41

Slide 41 text

@Override public Parcelable onSaveInstanceState() { Parcelable superState = super.onSaveInstanceState(); SavedState ss = new SavedState(superState); ss.progress = mProgress; return ss; } @Override public void onRestoreInstanceState(Parcelable state) { SavedState ss = (SavedState) state; super.onRestoreInstanceState(ss.getSuperState()); setProgress(ss.progress); }

Slide 42

Slide 42 text

To every action there is a reaction TOUCH HANDLING

Slide 43

Slide 43 text

@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // The first pointer went down (i.e. stop // animations and start a new drag gesture) break; case MotionEvent.ACTION_MOVE: // A pointer has moved (i.e. move the object // accordingly) break; case MotionEvent.ACTION_UP: // The last pointer went up (i.e. compute the // object’s velocity and start animating it) break; case MotionEvent.ACTION_CANCEL: // The gesture was intercepted by a parent break; } return true; }

Slide 44

Slide 44 text

@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // The first pointer went down (i.e. stop // animations and start a new drag gesture) break; case MotionEvent.ACTION_MOVE: // A pointer has moved (i.e. move the object // accordingly) break; case MotionEvent.ACTION_UP: // The last pointer went up (i.e. compute the // object’s velocity and start animating it) break; case MotionEvent.ACTION_CANCEL: // The gesture was intercepted by a parent break; } return true; }

Slide 45

Slide 45 text

@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // The first pointer went down (i.e. stop // animations and start a new drag gesture) break; case MotionEvent.ACTION_MOVE: // A pointer has moved (i.e. move the object // accordingly) break; case MotionEvent.ACTION_UP: // The last pointer went up (i.e. compute the // object’s velocity and start animating it) break; case MotionEvent.ACTION_CANCEL: // The gesture was intercepted by a parent break; } return true; }

Slide 46

Slide 46 text

@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // The first pointer went down (i.e. stop // animations and start a new drag gesture) break; case MotionEvent.ACTION_MOVE: // A pointer has moved (i.e. move the object // accordingly) break; case MotionEvent.ACTION_UP: // The last pointer went up (i.e. compute the // object’s velocity and start animating it) break; case MotionEvent.ACTION_CANCEL: // The gesture was intercepted by a parent break; } return true; }

Slide 47

Slide 47 text

@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // The first pointer went down (i.e. stop // animations and start a new drag gesture) break; case MotionEvent.ACTION_MOVE: // A pointer has moved (i.e. move the object // accordingly) break; case MotionEvent.ACTION_UP: // The last pointer went up (i.e. compute the // object’s velocity and start animating it) break; case MotionEvent.ACTION_CANCEL: // The gesture was intercepted by a parent break; } return true; }

Slide 48

Slide 48 text

@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // The first pointer went down (i.e. stop // animations and start a new drag gesture) break; case MotionEvent.ACTION_MOVE: // A pointer has moved (i.e. move the object // accordingly) break; case MotionEvent.ACTION_UP: // The last pointer went up (i.e. compute the // object’s velocity and start animating it) break; case MotionEvent.ACTION_CANCEL: // The gesture was intercepted by a parent break; } return true; }

Slide 49

Slide 49 text

What if a parent View wants to intercept the gesture?

Slide 50

Slide 50 text

boolean onInterceptTouchEvent(MotionEvent event) FOR EXPERTS

Slide 51

Slide 51 text

SCROLLING/FLINGING A thrown object moves until it reaches its state of rest

Slide 52

Slide 52 text

1Track velocity 2 Scroll/fling to state of rest

Slide 53

Slide 53 text

1Track velocity @Override public boolean onTouchEvent(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); switch (event.getAction()) { case MotionEvent.ACTION_UP: mVelocityTracker.computeCurrentVelocity(1000); if (mVelocityTracker.getXVelocity() > mThreshold) { // Animate the object } // no break; case MotionEvent.ACTION_CANCEL: mVelocityTracker.recycle(); mVelocityTracker = null; break; } return true; }

Slide 54

Slide 54 text

1Track velocity @Override public boolean onTouchEvent(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); switch (event.getAction()) { case MotionEvent.ACTION_UP: mVelocityTracker.computeCurrentVelocity(1000); if (mVelocityTracker.getXVelocity() > mThreshold) { // Animate the object } // no break; case MotionEvent.ACTION_CANCEL: mVelocityTracker.recycle(); mVelocityTracker = null; break; } return true; }

Slide 55

Slide 55 text

1Track velocity @Override public boolean onTouchEvent(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); switch (event.getAction()) { case MotionEvent.ACTION_UP: mVelocityTracker.computeCurrentVelocity(1000); if (mVelocityTracker.getXVelocity() > mThreshold) { // Animate the object } // no break; case MotionEvent.ACTION_CANCEL: mVelocityTracker.recycle(); mVelocityTracker = null; break; } return true; }

Slide 56

Slide 56 text

1Track velocity @Override public boolean onTouchEvent(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); switch (event.getAction()) { case MotionEvent.ACTION_UP: mVelocityTracker.computeCurrentVelocity(1000); if (mVelocityTracker.getXVelocity() > mThreshold) { // Animate the object } // no break; case MotionEvent.ACTION_CANCEL: mVelocityTracker.recycle(); mVelocityTracker = null; break; } return true; }

Slide 57

Slide 57 text

1Track velocity @Override public boolean onTouchEvent(MotionEvent event) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); switch (event.getAction()) { case MotionEvent.ACTION_UP: mVelocityTracker.computeCurrentVelocity(1000); if (mVelocityTracker.getXVelocity() > mThreshold) { // Animate the object } // no break; case MotionEvent.ACTION_CANCEL: mVelocityTracker.recycle(); mVelocityTracker = null; break; } return true; }

Slide 58

Slide 58 text

2 Scroll/fling to state of rest private final Runnable mScrollRunnable = new Runnable() { @Override public void run() { if (mOverScroller.computeScrollOffset()) { final int x = mOverScroller.getCurrX(); final int y = mOverScroller.getCurrY(); // Move object to (x, y). postOnAnimation(this); } else { // animation is over } } }; public void fling() { mOverScroller.fling( mStartX, mStartY, // start x/y mVX, mVY, // velocity x/y mMinX, mMaxX, // min/max x mMinY, mMaxY); // min/max y postOnAnimation(mScrollRunnable); }

Slide 59

Slide 59 text

2 Scroll/fling to state of rest private final Runnable mScrollRunnable = new Runnable() { @Override public void run() { if (mOverScroller.computeScrollOffset()) { final int x = mOverScroller.getCurrX(); final int y = mOverScroller.getCurrY(); // Move object to (x, y). postOnAnimation(this); } else { // animation is over } } }; public void fling() { mOverScroller.fling( mStartX, mStartY, // start x/y mVX, mVY, // velocity x/y mMinX, mMaxX, // min/max x mMinY, mMaxY); // min/max y postOnAnimation(mScrollRunnable); }

Slide 60

Slide 60 text

2 Scroll/fling to state of rest private final Runnable mScrollRunnable = new Runnable() { @Override public void run() { if (mOverScroller.computeScrollOffset()) { final int x = mOverScroller.getCurrX(); final int y = mOverScroller.getCurrY(); // Move object to (x, y). postOnAnimation(this); } else { // animation is over } } }; public void fling() { mOverScroller.fling( mStartX, mStartY, // start x/y mVX, mVY, // velocity x/y mMinX, mMaxX, // min/max x mMinY, mMaxY); // min/max y postOnAnimation(mScrollRunnable); }

Slide 61

Slide 61 text

2 Scroll/fling to state of rest private final Runnable mScrollRunnable = new Runnable() { @Override public void run() { if (mOverScroller.computeScrollOffset()) { final int x = mOverScroller.getCurrX(); final int y = mOverScroller.getCurrY(); // Move object to (x, y). postOnAnimation(this); } else { // animation is over } } }; public void fling() { mOverScroller.fling( mStartX, mStartY, // start x/y mVX, mVY, // velocity x/y mMinX, mMaxX, // min/max x mMinY, mMaxY); // min/max y postOnAnimation(mScrollRunnable); }

Slide 62

Slide 62 text

2 Scroll/fling to state of rest private final Runnable mScrollRunnable = new Runnable() { @Override public void run() { if (mOverScroller.computeScrollOffset()) { final int x = mOverScroller.getCurrX(); final int y = mOverScroller.getCurrY(); // Move object to (x, y). postOnAnimation(this); } else { // animation is over } } }; public void fling() { mOverScroller.fling( mStartX, mStartY, // start x/y mVX, mVY, // velocity x/y mMinX, mMaxX, // min/max x mMinY, mMaxY); // min/max y postOnAnimation(mScrollRunnable); }

Slide 63

Slide 63 text

2 Scroll/fling to state of rest private final Runnable mScrollRunnable = new Runnable() { @Override public void run() { if (mOverScroller.computeScrollOffset()) { final int x = mOverScroller.getCurrX(); final int y = mOverScroller.getCurrY(); // Move object to (x, y). postOnAnimation(this); } else { // animation is over } } }; public void fling() { mOverScroller.fling( mStartX, mStartY, // start x/y mVX, mVY, // velocity x/y mMinX, mMaxX, // min/max x mMinY, mMaxY); // min/max y postOnAnimation(mScrollRunnable); }

Slide 64

Slide 64 text

CONSISTENCY with the rest of the platform behavior is consistent Make sure your custom View

Slide 65

Slide 65 text

EdgeEffect

Slide 66

Slide 66 text

@Override public void draw(Canvas canvas) { super.draw(canvas); if (!mTopEdgeEffect.isFinished()) { canvas.save(); canvas.translate(getPaddingLeft(), 0); final int w = getWidth() - getPaddingLeft() - getPaddingRight(); mTopEdgeEffect.setSize(w, getHeight()); if (mTopEdgeEffect.draw(canvas)) { postInvalidateOnAnimation(); } canvas.restore(); } }

Slide 67

Slide 67 text

@Override public void draw(Canvas canvas) { super.draw(canvas); if (!mTopEdgeEffect.isFinished()) { canvas.save(); canvas.translate(getPaddingLeft(), 0); final int w = getWidth() - getPaddingLeft() - getPaddingRight(); mTopEdgeEffect.setSize(w, getHeight()); if (mTopEdgeEffect.draw(canvas)) { postInvalidateOnAnimation(); } canvas.restore(); } }

Slide 68

Slide 68 text

@Override public void draw(Canvas canvas) { super.draw(canvas); if (!mTopEdgeEffect.isFinished()) { canvas.save(); canvas.translate(getPaddingLeft(), 0); final int w = getWidth() - getPaddingLeft() - getPaddingRight(); mTopEdgeEffect.setSize(w, getHeight()); if (mTopEdgeEffect.draw(canvas)) { postInvalidateOnAnimation(); } canvas.restore(); } }

Slide 69

Slide 69 text

@Override public void draw(Canvas canvas) { super.draw(canvas); if (!mTopEdgeEffect.isFinished()) { canvas.save(); canvas.translate(getPaddingLeft(), 0); final int w = getWidth() - getPaddingLeft() - getPaddingRight(); mTopEdgeEffect.setSize(w, getHeight()); if (mTopEdgeEffect.draw(canvas)) { postInvalidateOnAnimation(); } canvas.restore(); } }

Slide 70

Slide 70 text

@Override public void draw(Canvas canvas) { super.draw(canvas); if (!mTopEdgeEffect.isFinished()) { canvas.save(); canvas.translate(getPaddingLeft(), 0); final int w = getWidth() - getPaddingLeft() - getPaddingRight(); mTopEdgeEffect.setSize(w, getHeight()); if (mTopEdgeEffect.draw(canvas)) { postInvalidateOnAnimation(); } canvas.restore(); } }

Slide 71

Slide 71 text

@Override public void draw(Canvas canvas) { super.draw(canvas); if (!mTopEdgeEffect.isFinished()) { canvas.save(); canvas.translate(getPaddingLeft(), 0); final int w = getWidth() - getPaddingLeft() - getPaddingRight(); mTopEdgeEffect.setSize(w, getHeight()); if (mTopEdgeEffect.draw(canvas)) { postInvalidateOnAnimation(); } canvas.restore(); } }

Slide 72

Slide 72 text

This was only the drawing part ...

Slide 73

Slide 73 text

mTopEdgeEffect.onAbsorb((int) velocity); .onPull((float) distance); .onRelease();

Slide 74

Slide 74 text

ViewConfiguration documentation Contains methods to standard constants used in the UI for timeouts, sizes, and distances.

Slide 75

Slide 75 text

private void init(Context context, AttributeSet attrs, int defStyle) { mViewConfiguration = ViewConfiguration.get(context); final int maxFlingV = mViewConfiguration .getScaledMaximumFlingVelocity(); final int touchSlop = mViewConfiguration .getScaledTouchSlop(); // ... }

Slide 76

Slide 76 text

private void init(Context context, AttributeSet attrs, int defStyle) { mViewConfiguration = ViewConfiguration.get(context); final int maxFlingV = mViewConfiguration .getScaledMaximumFlingVelocity(); final int touchSlop = mViewConfiguration .getScaledTouchSlop(); // ... }

Slide 77

Slide 77 text

private void init(Context context, AttributeSet attrs, int defStyle) { mViewConfiguration = ViewConfiguration.get(context); final int maxFlingV = mViewConfiguration .getScaledMaximumFlingVelocity(); final int touchSlop = mViewConfiguration .getScaledTouchSlop(); // ... }

Slide 78

Slide 78 text

Conclusion 5

Slide 79

Slide 79 text

Custom views are the best way for creating unique UIs

Slide 80

Slide 80 text

Custom views must fit to the platform appearance

Slide 81

Slide 81 text

Custom views must match to the mental model

Slide 82

Slide 82 text

THANK YOU @cyrilmottier cyrilmottier.com Cyril Mottier