Page 119

Android Programming Cookbook

111 / 143

public void setTouchListener(OnTouchListener onTouchListener) { this.onTouchListener = onTouchListener; } @Override public boolean dispatchTouchEvent(final MotionEvent event) { if (onTouchListener == null) return false; int pointerIndex = event.getActionIndex(); int pointerId = event.getPointerId(pointerIndex); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: lastYPosition = event.getY(); lastXPosition = event.getX(); onTouchListener.onTouch(); return true; case MotionEvent.ACTION_UP: onScaleMove = false; lastdist = 0; onTouchListener.onRelease(); break; case MotionEvent.ACTION_MOVE: int diffY = (int) (event.getY() - lastYPosition); int diffX = (int) (event.getX() - lastXPosition); lastYPosition = event.getY(); lastXPosition = event.getX(); //Check if the action was jitter if (Math.abs(diffX) > 4 || Math.abs(diffY) > 4) { if (onScaleMove) { double dist = 0; if (event.getPointerCount() >= 2) { dist = Math.sqrt(Math.pow(event.getX(0) - event.getX(1), 2) + Math.pow(event.getY(0) - event.getY(1), 2)); } if ((Math.abs(dist - lastdist) > 10) && (lastdist > 0) && (dist > 0)) { if (dist < lastdist) { onTouchListener.onPinchIn(); } else if (dist == lastdist) { // onTouchListener.onPinchStable(); } else { onTouchListener.onPinchOut(); } } else { onTouchListener.onTwoFingersDrag(); } lastdist = dist; return false; } else { onTouchListener.onMove();

←-

←-

SKY MAGZ  

This is Yahya Christian School Magazine

Read more
Read more
Similar to
Popular now
Just for you