20
VIEW CUSTOMIZATION IN A NUTSHELL

[Ultracode Munich Meetup #7] Android view customization in a nutshell

  • Upload
    bemyapp

  • View
    281

  • Download
    1

Embed Size (px)

Citation preview

Page 1: [Ultracode Munich Meetup #7] Android view customization in a nutshell

VIEW CUSTOMIZATIONIN A NUTSHELL

Page 2: [Ultracode Munich Meetup #7] Android view customization in a nutshell

@bernhardpflug

plus.google.com/+BernhardPflug

Page 3: [Ultracode Munich Meetup #7] Android view customization in a nutshell

Iconmobile170

Headquarter

BMW

Page 4: [Ultracode Munich Meetup #7] Android view customization in a nutshell

BMW remote apps

Page 5: [Ultracode Munich Meetup #7] Android view customization in a nutshell

The app

Page 6: [Ultracode Munich Meetup #7] Android view customization in a nutshell

UX Don’t just start coding

Make it easy to send feedback

Analyse usage

Page 7: [Ultracode Munich Meetup #7] Android view customization in a nutshell

One UX example

Page 8: [Ultracode Munich Meetup #7] Android view customization in a nutshell

Custom view drawing with animation

public class TankView extends ViewGroup { ! . . . !

private void updateBoilerAppearance() { . . . liquidPaths[i] = new Path(); liquidPaints[i] = new Paint(liquidPaint); liquidPaints[i].setShader(new LinearGradient(. . .}, new float[] { . . . }, Shader.TileMode.CLAMP)); ! liquidPaths[i].moveTo(. . .); liquidPaths[i].lineTo(. . .); liquidPaths[i].quadTo(. . .);

. . . ! liquidPathEffects[i].add(new DashPathEffect(new float[] { . . . )); ! } !

. . . } !!

Page 9: [Ultracode Munich Meetup #7] Android view customization in a nutshell

!@Overrideprotected void dispatchDraw(Canvas canvas) { canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), alpha, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG); canvas.drawPath(boilerContour, contourPaint); . . . if (state == State.HEATING) { // draw pipe canvas.drawRect(pipeBounds, fillPaint); canvas.drawLine(. . . ); canvas.drawLine(. . . ); // draw liquid for (int i = 0; i < liquidPaths.length; i++) { canvas.drawPath(liquidPaths[i], liquidPaints[i]); } liquidAnimHandler.postDelayed(liquidAnimation, 50); } }

Custom view drawing with animation

Page 10: [Ultracode Munich Meetup #7] Android view customization in a nutshell

@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) { updateBoilerAppearance(); super.onSizeChanged(w, h, oldw, oldh);}

Custom view drawing with animation

Page 11: [Ultracode Munich Meetup #7] Android view customization in a nutshell

public class SvgView extends View { ! . . . ! @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); if (w > 0 && h > 0 && svg != null) { svg.setDocumentWidth(w); svg.setDocumentHeight(h); Bitmap svgBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas bitmapCanvas = new Canvas(svgBitmap); // Render our document onto our canvas svg.renderToCanvas(bitmapCanvas); setBackgroundDrawable(new BitmapDrawable(getResources(), svgBitmap)); } } ! . . . }

Consider SVG instead of dpi graphics

{} @ github.com/bernhardpflug/AndroidUtilities

Page 12: [Ultracode Munich Meetup #7] Android view customization in a nutshell

Custom swipe component

Page 13: [Ultracode Munich Meetup #7] Android view customization in a nutshell

Custom swipe component

Ground View

Sky View

Page 14: [Ultracode Munich Meetup #7] Android view customization in a nutshell

public class VerticalViewPager extends ViewGroup { . . . ! @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = getDefaultSize(0, widthMeasureSpec); int height = getDefaultSize(0, heightMeasureSpec); setMeasuredDimension(width, height); int hPadding = getPaddingLeft()+getPaddingRight(); int vPadding = getPaddingTop()+getPaddingBottom(); int contentWidth = getChildMeasureSpec(widthMeasureSpec, hPadding , width); int contentHeight = getChildMeasureSpec(heightMeasureSpec,vPadding , height); groundView.measure(contentWidth, contentHeight); int skyViewHeight = skyView.getLayoutParams().height; int skyHeight = getChildMeasureSpec(heightMeasureSpec, vPadding, skyViewHeight); skyView.measure(contentWidth, skyHeight); } ! . . . !}

Implementing custom swipe components

{} @ github.com/bernhardpflug/AndroidUtilities

Ground View

Sky View

Page 15: [Ultracode Munich Meetup #7] Android view customization in a nutshell

@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int width = r - l; final int height = b - t; int skyViewHeight = skyView.getMeasuredHeight(); skyView.layout(0, 0, width, skyView.getMeasuredHeight()); groundView.layout(0, skyViewHeight, width, height + skyViewHeight); }

Implementing custom swipe components

{} @ github.com/bernhardpflug/AndroidUtilities

Ground View

Sky View

Page 16: [Ultracode Munich Meetup #7] Android view customization in a nutshell

@Overridepublic boolean onTouchEvent(MotionEvent event) { ! . . . ! switch (event.getAction()) {

. . . case MotionEvent.ACTION_MOVE: ! int touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); if (!verticalScrolling && Math.abs(deltaY) > touchSlop) { verticalScrolling = true; requestDisallowInterceptTouchEvent(true); ! if (getScrollY() + deltaY <= State.CLOSED && getScrollY() + deltaY >= State.OPEN) { scrollBy(0, (int) deltaY); } } !!}

Implementing custom swipe components

{} @ github.com/bernhardpflug/AndroidUtilities

Page 17: [Ultracode Munich Meetup #7] Android view customization in a nutshell

@Overridepublic boolean onTouchEvent(MotionEvent event) { ! . . . ! switch (event.getAction()) {

. . . case MotionEvent.ACTION_UP: scroller.startScroll(0, getScrollY(), 0, getY(destinationState) - getScrollY()); break; !!}

Implementing custom swipe components

{} @ github.com/bernhardpflug/AndroidUtilities

Page 18: [Ultracode Munich Meetup #7] Android view customization in a nutshell

@Overridepublic void computeScroll() { if (scroller.computeScrollOffset()) { int x = scroller.getCurrX(); int y = scroller.getCurrY(); scrollTo(x, y); postInvalidate(); } }

Implementing custom swipe components

{} @ github.com/bernhardpflug/AndroidUtilities

Page 19: [Ultracode Munich Meetup #7] Android view customization in a nutshell

at.bernhardpflug.heating

Give it a try

Google Play

{} @ github.com/bernhardpflug/AndroidUtilities

Page 20: [Ultracode Munich Meetup #7] Android view customization in a nutshell

Thank you!

plus.google.com/+BernhardPflug

@bernhardpflug