Code example for ScaleGestureDetector

Methods: getFocusX, getFocusY, isInProgress, onTouchEvent

0
 
 
    @Override 
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events. 
        mScaleDetector.onTouchEvent(ev);
 
        final int action = ev.getAction();
        switch (action & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                if (!mScaleDetector.isInProgress()) {
                    final float x = ev.getX();
                    final float y = ev.getY();
 
                    mLastTouchX = x;
                    mLastTouchY = y;
                    mActivePointerId = ev.getPointerId(0);
                } 
                break; 
            } 
            case MotionEvent.ACTION_POINTER_1_DOWN: {
                if (mScaleDetector.isInProgress()) {
                    final float gx = mScaleDetector.getFocusX();
                    final float gy = mScaleDetector.getFocusY();
                    mLastGestureX = gx;
                    mLastGestureY = gy;
                } 
                break; 
            } 
            case MotionEvent.ACTION_MOVE: {
 
                // Only move if the ScaleGestureDetector isn't processing a gesture. 
                if (!mScaleDetector.isInProgress()) {
                    final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                    final float x = ev.getX(pointerIndex);
                    final float y = ev.getY(pointerIndex);
 
                    final float dx = x - mLastTouchX;
                    final float dy = y - mLastTouchY;
 
                    mPosX += dx;
                    mPosY += dy;
 
                    invalidate();
 
                    mLastTouchX = x;
                    mLastTouchY = y;
                } 
                else{ 
                    final float gx = mScaleDetector.getFocusX();
                    final float gy = mScaleDetector.getFocusY();
 
                    final float gdx = gx - mLastGestureX;
                    final float gdy = gy - mLastGestureY;
 
                    mPosX += gdx;