Code example for VelocityTracker

Methods: addMovementcomputeCurrentVelocitygetXVelocitygetYVelocityrecycle

0
        final float y = event.getY();
         
        if(mVelocityTracker == null) {
            mVelocityTracker = VelocityTracker.obtain();
        } 
        mVelocityTracker.addMovement(event);
         
        switch(action) {
            case MotionEvent.ACTION_DOWN:
                periodicTableView.onDown(x, y);
                mZoomControl.stopFling();
                v.postDelayed(mLongPressRunnable, mLongPressTimeout);
                mDownX = x;
                mDownY = y;
                mX = x;
                mY = y;
                break; 
            case MotionEvent.ACTION_MOVE:
                final double dx = (x - mX) / v.getWidth();
                final double dy = (y - mY) / v.getHeight();
                 
                if(mMode == Mode.ZOOM) {
                    periodicTableView.clearSelection();
                    mZoomControl.zoom(Math.pow(20, -dy),
                            mDownX / v.getWidth(), mDownY / v.getHeight());
                } else if(mMode == Mode.PAN) {
                    periodicTableView.clearSelection();
                    mZoomControl.pan(-dx, -dy);
                } else { 
                    final double scrollX = mDownX - x;
                    final double scrollY = mDownY - y;
                     
                    final double dist = Math.sqrt(scrollX * scrollX + scrollY * scrollY);
                     
                    if(dist >= mScaledTouchSlop) {
                        v.removeCallbacks(mLongPressRunnable);
                        mMode = Mode.PAN;
                    } 
                } 
                 
                mX = x;
                mY = y;
                break; 
            case MotionEvent.ACTION_UP:
                if(mMode == Mode.PAN) {
                    periodicTableView.clearSelection();
                    mVelocityTracker.computeCurrentVelocity(1000, mScaledMaximumFlingVelocity);
                    mZoomControl.startFling(-mVelocityTracker.getXVelocity() / v.getWidth(),
                            -mVelocityTracker.getYVelocity() / v.getHeight());
                } else { 
                    mZoomControl.startFling(0, 0);
                } 
                periodicTableView.onClick();
            default: 
                mVelocityTracker.recycle();
                mVelocityTracker = null;
                v.removeCallbacks(mLongPressRunnable);
                mMode = Mode.UNDEFINED;
                break; 
        }