Example usage for android.view MotionEvent getActionIndex

List of usage examples for android.view MotionEvent getActionIndex

Introduction

In this page you can find the example usage for android.view MotionEvent getActionIndex.

Prototype

public final int getActionIndex() 

Source Link

Document

For #ACTION_POINTER_DOWN or #ACTION_POINTER_UP as returned by #getActionMasked , this returns the associated pointer index.

Usage

From source file:com.hippo.widget.BothScrollView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    initVelocityTrackerIfNotExists();/*  www  . j  a v  a2 s.  c  o  m*/
    mVelocityTracker.addMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        if (getChildCount() == 0) {
            return false;
        }
        if ((mIsBeingDragged = !mScroller.isFinished())) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        /*
         * If being flinged and user touches, stop the fling. isFinished
         * will be false if being flinged.
         */
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }

        // Remember where the motion event started
        mLastMotionX = (int) ev.getX();
        mLastMotionY = (int) ev.getY();
        mActivePointerId = ev.getPointerId(0);
        break;
    }
    case MotionEvent.ACTION_MOVE:
        final int activePointerIndex = ev.findPointerIndex(mActivePointerId);
        if (activePointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
            break;
        }
        final int x = (int) ev.getX(activePointerIndex);
        final int y = (int) ev.getY(activePointerIndex);
        int deltaX = mLastMotionX - x;
        int deltaY = mLastMotionY - y;
        if (!mIsBeingDragged && (Math.abs(deltaX) > mTouchSlop || Math.abs(deltaY) > mTouchSlop)) {
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
            mIsBeingDragged = true;
            if (deltaX > 0) {
                deltaX -= mTouchSlop;
                deltaX = Math.max(0, deltaX);
            } else {
                deltaX += mTouchSlop;
                deltaX = Math.min(0, deltaX);
            }
            if (deltaY > 0) {
                deltaY -= mTouchSlop;
                deltaY = Math.max(0, deltaY);
            } else {
                deltaY += mTouchSlop;
                deltaY = Math.min(0, deltaY);
            }
        }
        if (mIsBeingDragged) {
            // Scroll to follow the motion event
            mLastMotionX = x;
            mLastMotionY = y;

            final int oldX = getScrollX();
            final int oldY = getScrollY();
            final int horizontalRange = getHorizontalScrollRange();
            final int verticalRange = getVerticalScrollRange();
            final int overscrollMode = getOverScrollMode();
            final boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS
                    || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS
                            && (horizontalRange > 0 || verticalRange > 0));

            // Calling overScrollBy will call onOverScrolled, which
            // calls onScrollChanged if applicable.
            overScrollBy(deltaX, deltaY, oldX, oldY, horizontalRange, verticalRange, mOverscrollDistance,
                    mOverscrollDistance, true);

            if (canOverscroll) {
                final int pulledToX = oldX + deltaX;
                final int pulledToY = oldY + deltaY;
                if (pulledToX < 0) {
                    mEdgeGlowLeft.onPull((float) deltaX / getWidth());
                    if (!mEdgeGlowRight.isFinished()) {
                        mEdgeGlowRight.onRelease();
                    }
                } else if (pulledToX > horizontalRange) {
                    mEdgeGlowRight.onPull((float) deltaX / getWidth());
                    if (!mEdgeGlowLeft.isFinished()) {
                        mEdgeGlowLeft.onRelease();
                    }
                }
                if (pulledToY < 0) {
                    mEdgeGlowTop.onPull((float) deltaY / getHeight());
                    if (!mEdgeGlowBottom.isFinished()) {
                        mEdgeGlowBottom.onRelease();
                    }
                } else if (pulledToY > verticalRange) {
                    mEdgeGlowBottom.onPull((float) deltaY / getHeight());
                    if (!mEdgeGlowTop.isFinished()) {
                        mEdgeGlowTop.onRelease();
                    }
                }
                if (mEdgeGlowLeft != null && (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished()
                        || !mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) {
                    ViewCompat.postInvalidateOnAnimation(this);
                }
            }
        }
        break;
    case MotionEvent.ACTION_UP:
        if (mIsBeingDragged) {
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int initialVelocityX = (int) velocityTracker.getXVelocity(mActivePointerId);
            int initialVelocityY = (int) velocityTracker.getYVelocity(mActivePointerId);

            if (getChildCount() > 0) {
                if (Math.abs(initialVelocityX) > mMinimumVelocity
                        || Math.abs(initialVelocityY) > mMinimumVelocity) {
                    fling(-initialVelocityX, -initialVelocityY);
                } else {
                    if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0,
                            getVerticalScrollRange())) {
                        ViewCompat.postInvalidateOnAnimation(this);
                    }
                }
            }

            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_CANCEL:
        if (mIsBeingDragged && getChildCount() > 0) {
            if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0,
                    getVerticalScrollRange())) {
                ViewCompat.postInvalidateOnAnimation(this);
            }
            mActivePointerId = INVALID_POINTER;
            endDrag();
        }
        break;
    case MotionEvent.ACTION_POINTER_DOWN: {
        final int index = ev.getActionIndex();
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        mActivePointerId = ev.getPointerId(index);
        break;
    }
    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        final int index = ev.findPointerIndex(mActivePointerId);
        mLastMotionX = (int) ev.getX(index);
        mLastMotionY = (int) ev.getY(index);
        break;
    }
    return true;
}

From source file:com.test.xujixiao.xjx.widget.view.swipe_listview.SwipeListViewTouchListener.java

/**
 * @see View.OnTouchListener#onTouch(View, MotionEvent)
 *//*from  ww w. j a v  a2  s .  c o  m*/
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    if (!isSwipeEnabled()) {
        return false;
    }

    if (viewWidth < 2) {
        viewWidth = swipeListView.getWidth();
    }

    switch (motionEvent.getActionMasked()) {
    case MotionEvent.ACTION_DOWN: {
        if (paused && downPosition != ListView.INVALID_POSITION) {
            return false;
        }
        swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE;

        int childCount = swipeListView.getChildCount();
        int[] listViewCoords = new int[2];
        swipeListView.getLocationOnScreen(listViewCoords);
        int x = (int) motionEvent.getRawX() - listViewCoords[0];
        int y = (int) motionEvent.getRawY() - listViewCoords[1];
        View child;
        for (int i = 0; i < childCount; i++) {
            child = swipeListView.getChildAt(i);
            child.getHitRect(rect);

            int childPosition = swipeListView.getPositionForView(child);

            // dont allow swiping if this is on the header or footer or IGNORE_ITEM_VIEW_TYPE or enabled is false on the adapter
            boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition)
                    && swipeListView.getAdapter().getItemViewType(childPosition) >= 0;

            if (allowSwipe && rect.contains(x, y)) {
                setParentView(child);
                setFrontView(child.findViewById(swipeFrontView));

                downX = motionEvent.getRawX();
                downPosition = childPosition;

                frontView.setClickable(!opened.get(downPosition));
                frontView.setLongClickable(!opened.get(downPosition));

                velocityTracker = VelocityTracker.obtain();
                velocityTracker.addMovement(motionEvent);
                if (swipeBackView > 0) {
                    setBackView(child.findViewById(swipeBackView));
                }
                break;
            }
        }
        view.onTouchEvent(motionEvent);
        return true;
    }

    case MotionEvent.ACTION_UP: {
        if (velocityTracker == null || !swiping || downPosition == ListView.INVALID_POSITION) {
            break;
        }

        float deltaX = motionEvent.getRawX() - downX;
        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        if (!opened.get(downPosition)) {
            if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) {
                velocityX = 0;
            }
            if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && velocityTracker.getXVelocity() < 0) {
                velocityX = 0;
            }
        }
        float velocityY = Math.abs(velocityTracker.getYVelocity());
        boolean swap = false;
        boolean swapRight = false;
        if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY * 2 < velocityX) {
            swapRight = velocityTracker.getXVelocity() > 0;
            Log.d("SwipeListView", "swapRight: " + swapRight + " - swipingRight: " + swipingRight);
            if (swapRight != swipingRight && swipeActionLeft != swipeActionRight) {
                swap = false;
            } else if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) {
                swap = false;
            } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) {
                swap = false;
            } else {
                swap = true;
            }
        } else if (Math.abs(deltaX) > viewWidth / 2) {
            swap = true;
            swapRight = deltaX > 0;
        }
        generateAnimate(frontView, swap, swapRight, downPosition);
        if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
            swapChoiceState(downPosition);
        }

        velocityTracker.recycle();
        velocityTracker = null;
        downX = 0;
        // change clickable front view
        //                if (swap) {
        //                    frontView.setClickable(opened.get(downPosition));
        //                    frontView.setLongClickable(opened.get(downPosition));
        //                }
        swiping = false;
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) {
            break;
        }

        velocityTracker.addMovement(motionEvent);
        velocityTracker.computeCurrentVelocity(1000);
        float velocityX = Math.abs(velocityTracker.getXVelocity());
        float velocityY = Math.abs(velocityTracker.getYVelocity());

        float deltaX = motionEvent.getRawX() - downX;
        float deltaMode = Math.abs(deltaX);

        int swipeMode = this.swipeMode;
        int changeSwipeMode = swipeListView.changeSwipeMode(downPosition);
        if (changeSwipeMode >= 0) {
            swipeMode = changeSwipeMode;
        }

        if (swipeMode == SwipeListView.SWIPE_MODE_NONE) {
            deltaMode = 0;
        } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) {
            if (opened.get(downPosition)) {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) {
                    deltaMode = 0;
                }
            } else {
                if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) {
                    deltaMode = 0;
                } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) {
                    deltaMode = 0;
                }
            }
        }
        if (deltaMode > slop && swipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE
                && velocityY < velocityX) {
            swiping = true;
            swipingRight = (deltaX > 0);
            Log.d("SwipeListView", "deltaX: " + deltaX + " - swipingRight: " + swipingRight);
            if (opened.get(downPosition)) {
                swipeListView.onStartClose(downPosition, swipingRight);
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            } else {
                if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
                } else if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHOICE) {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
                } else {
                    swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
                }
                swipeListView.onStartOpen(downPosition, swipeCurrentAction, swipingRight);
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(MotionEvent.ACTION_CANCEL
                    | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
            if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
                backView.setVisibility(View.GONE);
            }
        }

        if (swiping && downPosition != ListView.INVALID_POSITION) {
            if (opened.get(downPosition)) {
                deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset;
            }
            move(deltaX);
            return true;
        }
        break;
    }
    }
    return false;
}

From source file:xiaofan.llongimageview.view.SubsamplingScaleImageView.java

/**
 * Handle touch events. One finger pans, and two finger pinch and zoom plus panning.
 *//*from  w w w .  j  a  v a2 s .  c o m*/
@Override
public boolean onTouchEvent(MotionEvent event) {
    PointF vCenterEnd;
    float vDistEnd;
    // During non-interruptible anims, ignore all touch events
    if (anim != null && !anim.interruptible) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return true;
    } else {
        anim = null;
    }

    // Abort if not ready
    if (vTranslate == null) {
        return true;
    }
    // Detect flings, taps and double taps
    if (detector == null || detector.onTouchEvent(event)) {
        return true;
    }

    int touchCount = event.getPointerCount();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_1_DOWN:
    case MotionEvent.ACTION_POINTER_2_DOWN:
        anim = null;
        getParent().requestDisallowInterceptTouchEvent(true);
        maxTouchCount = Math.max(maxTouchCount, touchCount);
        if (touchCount >= 2) {
            if (zoomEnabled) {
                // Start pinch to zoom. Calculate distance between touch points and center point of the pinch.
                float distance = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                scaleStart = scale;
                vDistStart = distance;
                vTranslateStart = new PointF(vTranslate.x, vTranslate.y);
                vCenterStart = new PointF((event.getX(0) + event.getX(1)) / 2,
                        (event.getY(0) + event.getY(1)) / 2);
            } else {
                // Abort all gestures on second touch
                maxTouchCount = 0;
            }
            // Cancel long click timer
            handler.removeMessages(MESSAGE_LONG_CLICK);
        } else {
            // Start one-finger pan
            vTranslateStart = new PointF(vTranslate.x, vTranslate.y);
            vCenterStart = new PointF(event.getX(), event.getY());

            // Start long click timer
            handler.sendEmptyMessageDelayed(MESSAGE_LONG_CLICK, 600);
        }
        return true;
    case MotionEvent.ACTION_MOVE:
        boolean consumed = false;
        if (maxTouchCount > 0) {
            if (touchCount >= 2) {
                // Calculate new distance between touch points, to scale and pan relative to start values.
                vDistEnd = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                vCenterEnd = new PointF((event.getX(0) + event.getX(1)) / 2,
                        (event.getY(0) + event.getY(1)) / 2);

                if (zoomEnabled && (distance(vCenterStart.x, vCenterEnd.x, vCenterStart.y, vCenterEnd.y) > 5
                        || Math.abs(vDistEnd - vDistStart) > 5 || isPanning)) {
                    isZooming = true;
                    isPanning = true;
                    consumed = true;

                    scale = Math.min(maxScale, (vDistEnd / vDistStart) * scaleStart);

                    if (scale <= minScale()) {
                        // Minimum scale reached so don't pan. Adjust start settings so any expand will zoom in.
                        vDistStart = vDistEnd;
                        scaleStart = minScale();
                        vCenterStart = vCenterEnd;
                        vTranslateStart = vTranslate;
                    } else if (panEnabled) {
                        // Translate to place the source image coordinate that was at the center of the pinch at the start
                        // at the center of the pinch now, to give simultaneous pan + zoom.
                        float vLeftStart = vCenterStart.x - vTranslateStart.x;
                        float vTopStart = vCenterStart.y - vTranslateStart.y;
                        float vLeftNow = vLeftStart * (scale / scaleStart);
                        float vTopNow = vTopStart * (scale / scaleStart);
                        vTranslate.x = vCenterEnd.x - vLeftNow;
                        vTranslate.y = vCenterEnd.y - vTopNow;
                    } else if (sRequestedCenter != null) {
                        // With a center specified from code, zoom around that point.
                        vTranslate.x = (getWidth() / 2) - (scale * sRequestedCenter.x);
                        vTranslate.y = (getHeight() / 2) - (scale * sRequestedCenter.y);
                    } else {
                        // With no requested center, scale around the image center.
                        vTranslate.x = (getWidth() / 2) - (scale * (sWidth() / 2));
                        vTranslate.y = (getHeight() / 2) - (scale * (sHeight() / 2));
                    }

                    fitToBounds(true);
                    refreshRequiredTiles(false);
                }
            } else if (!isZooming) {
                // One finger pan - translate the image. We do this calculation even with pan disabled so click
                // and long click behaviour is preserved.
                float dx = Math.abs(event.getX() - vCenterStart.x);
                float dy = Math.abs(event.getY() - vCenterStart.y);
                if (dx > 5 || dy > 5 || isPanning) {
                    consumed = true;
                    vTranslate.x = vTranslateStart.x + (event.getX() - vCenterStart.x);
                    vTranslate.y = vTranslateStart.y + (event.getY() - vCenterStart.y);

                    float lastX = vTranslate.x;
                    float lastY = vTranslate.y;
                    fitToBounds(true);
                    if (lastX == vTranslate.x || (lastY == vTranslate.y && dy > 10) || isPanning) {
                        isPanning = true;
                    } else if (dx > 5) {
                        // Haven't panned the image, and we're at the left or right edge. Switch to page swipe.
                        maxTouchCount = 0;
                        handler.removeMessages(MESSAGE_LONG_CLICK);
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    if (!panEnabled) {
                        vTranslate.x = vTranslateStart.x;
                        vTranslate.y = vTranslateStart.y;
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    refreshRequiredTiles(false);
                }
            }
        }
        if (consumed) {
            handler.removeMessages(MESSAGE_LONG_CLICK);
            invalidate();
            return true;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_POINTER_2_UP:
        handler.removeMessages(MESSAGE_LONG_CLICK);
        if (maxTouchCount > 0 && (isZooming || isPanning)) {
            if (isZooming && touchCount == 2) {
                // Convert from zoom to pan with remaining touch
                isPanning = true;
                vTranslateStart = new PointF(vTranslate.x, vTranslate.y);
                if (event.getActionIndex() == 1) {
                    vCenterStart = new PointF(event.getX(0), event.getY(0));
                } else {
                    vCenterStart = new PointF(event.getX(1), event.getY(1));
                }
            }
            if (touchCount < 3) {
                // End zooming when only one touch point
                isZooming = false;
            }
            if (touchCount < 2) {
                // End panning when no touch points
                isPanning = false;
                maxTouchCount = 0;
            }
            // Trigger load of tiles now required
            refreshRequiredTiles(true);
            return true;
        }
        if (touchCount == 1) {
            isZooming = false;
            isPanning = false;
            maxTouchCount = 0;
        }
        return true;
    }
    return super.onTouchEvent(event);
}

From source file:com.bizcom.vc.widget.cus.SubsamplingScaleImageView.java

/**
 * Handle touch events. One finger pans, and two finger pinch and zoom plus
 * panning.//ww w. ja v a 2s .  c o m
 */
@Override
public boolean onTouchEvent(MotionEvent event) {
    PointF vCenterEnd;
    float vDistEnd;
    // During non-interruptible anims, ignore all touch events
    if (anim != null && !anim.interruptible) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return true;
    } else {
        anim = null;
    }

    // Abort if not ready
    if (vTranslate == null) {
        return true;
    }
    // Detect flings, taps and double taps
    if (detector == null || detector.onTouchEvent(event)) {
        return true;
    }

    int touchCount = event.getPointerCount();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_1_DOWN:
    case MotionEvent.ACTION_POINTER_2_DOWN:
        anim = null;
        getParent().requestDisallowInterceptTouchEvent(true);
        maxTouchCount = Math.max(maxTouchCount, touchCount);
        if (touchCount >= 2) {
            if (zoomEnabled) {
                // Start pinch to zoom. Calculate distance between touch
                // points and center point of the pinch.
                float distance = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                scaleStart = scale;
                vDistStart = distance;
                vTranslateStart = new PointF(vTranslate.x, vTranslate.y);
                vCenterStart = new PointF((event.getX(0) + event.getX(1)) / 2,
                        (event.getY(0) + event.getY(1)) / 2);
            } else {
                // Abort all gestures on second touch
                maxTouchCount = 0;
            }
            // Cancel long click timer
            handler.removeMessages(MESSAGE_LONG_CLICK);
        } else {
            // Start one-finger pan
            vTranslateStart = new PointF(vTranslate.x, vTranslate.y);
            vCenterStart = new PointF(event.getX(), event.getY());

            // Start long click timer
            handler.sendEmptyMessageDelayed(MESSAGE_LONG_CLICK, 600);
        }
        return true;
    case MotionEvent.ACTION_MOVE:
        boolean consumed = false;
        if (maxTouchCount > 0) {
            if (touchCount >= 2) {
                // Calculate new distance between touch points, to scale and
                // pan relative to start values.
                vDistEnd = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                vCenterEnd = new PointF((event.getX(0) + event.getX(1)) / 2,
                        (event.getY(0) + event.getY(1)) / 2);

                if (zoomEnabled && (distance(vCenterStart.x, vCenterEnd.x, vCenterStart.y, vCenterEnd.y) > 5
                        || Math.abs(vDistEnd - vDistStart) > 5 || isPanning)) {
                    isZooming = true;
                    isPanning = true;
                    consumed = true;

                    scale = Math.min(maxScale, (vDistEnd / vDistStart) * scaleStart);

                    if (scale <= minScale()) {
                        // Minimum scale reached so don't pan. Adjust start
                        // settings so any expand will zoom in.
                        vDistStart = vDistEnd;
                        scaleStart = minScale();
                        vCenterStart = vCenterEnd;
                        vTranslateStart = vTranslate;
                    } else if (panEnabled) {
                        // Translate to place the source image coordinate
                        // that was at the center of the pinch at the start
                        // at the center of the pinch now, to give
                        // simultaneous pan + zoom.
                        float vLeftStart = vCenterStart.x - vTranslateStart.x;
                        float vTopStart = vCenterStart.y - vTranslateStart.y;
                        float vLeftNow = vLeftStart * (scale / scaleStart);
                        float vTopNow = vTopStart * (scale / scaleStart);
                        vTranslate.x = vCenterEnd.x - vLeftNow;
                        vTranslate.y = vCenterEnd.y - vTopNow;
                    } else if (sRequestedCenter != null) {
                        // With a center specified from code, zoom around
                        // that point.
                        vTranslate.x = (getWidth() / 2) - (scale * sRequestedCenter.x);
                        vTranslate.y = (getHeight() / 2) - (scale * sRequestedCenter.y);
                    } else {
                        // With no requested center, scale around the image
                        // center.
                        vTranslate.x = (getWidth() / 2) - (scale * (sWidth() / 2));
                        vTranslate.y = (getHeight() / 2) - (scale * (sHeight() / 2));
                    }

                    fitToBounds(true);
                    refreshRequiredTiles(false);
                }
            } else if (!isZooming) {
                // One finger pan - translate the image. We do this
                // calculation even with pan disabled so click
                // and long click behaviour is preserved.
                float dx = Math.abs(event.getX() - vCenterStart.x);
                float dy = Math.abs(event.getY() - vCenterStart.y);
                if (dx > 5 || dy > 5 || isPanning) {
                    consumed = true;
                    vTranslate.x = vTranslateStart.x + (event.getX() - vCenterStart.x);
                    vTranslate.y = vTranslateStart.y + (event.getY() - vCenterStart.y);

                    float lastX = vTranslate.x;
                    float lastY = vTranslate.y;
                    fitToBounds(true);
                    if (lastX == vTranslate.x || (lastY == vTranslate.y && dy > 10) || isPanning) {
                        isPanning = true;
                    } else if (dx > 5) {
                        // Haven't panned the image, and we're at the left
                        // or right edge. Switch to page swipe.
                        maxTouchCount = 0;
                        handler.removeMessages(MESSAGE_LONG_CLICK);
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    if (!panEnabled) {
                        vTranslate.x = vTranslateStart.x;
                        vTranslate.y = vTranslateStart.y;
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    refreshRequiredTiles(false);
                }
            }
        }
        if (consumed) {
            handler.removeMessages(MESSAGE_LONG_CLICK);
            invalidate();
            return true;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_POINTER_2_UP:
        handler.removeMessages(MESSAGE_LONG_CLICK);
        if (maxTouchCount > 0 && (isZooming || isPanning)) {
            if (isZooming && touchCount == 2) {
                // Convert from zoom to pan with remaining touch
                isPanning = true;
                vTranslateStart = new PointF(vTranslate.x, vTranslate.y);
                if (event.getActionIndex() == 1) {
                    vCenterStart = new PointF(event.getX(0), event.getY(0));
                } else {
                    vCenterStart = new PointF(event.getX(1), event.getY(1));
                }
            }
            if (touchCount < 3) {
                // End zooming when only one touch point
                isZooming = false;
            }
            if (touchCount < 2) {
                // End panning when no touch points
                isPanning = false;
                maxTouchCount = 0;
            }
            // Trigger load of tiles now required
            refreshRequiredTiles(true);
            return true;
        }
        if (touchCount == 1) {
            isZooming = false;
            isPanning = false;
            maxTouchCount = 0;
        }
        return true;
    }
    return super.onTouchEvent(event);
}

From source file:me.ccrama.redditslide.Views.SubsamplingScaleImageView.java

/**
 * Handle touch events. One finger pans, and two finger pinch and zoom plus panning.
 *//*from w  w w  . j a v  a2s  .c  o m*/
@Override
@SuppressWarnings("deprecation")
public boolean onTouchEvent(@NonNull MotionEvent event) {
    // During non-interruptible anims, ignore all touch events
    if (anim != null && !anim.interruptible) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return true;
    } else {
        if (anim != null && anim.listener != null) {
            try {
                anim.listener.onInterruptedByUser();
            } catch (Exception e) {
                Log.w(TAG, "Error thrown by animation listener", e);
            }
        }
        anim = null;
    }

    // Abort if not ready
    if (vTranslate == null) {
        return true;
    }
    // Detect flings, taps and double taps
    if (!isQuickScaling && (detector == null || detector.onTouchEvent(event))) {
        isZooming = false;
        isPanning = false;
        maxTouchCount = 0;
        return true;
    }

    if (vTranslateStart == null) {
        vTranslateStart = new PointF(0, 0);
    }
    if (vCenterStart == null) {
        vCenterStart = new PointF(0, 0);
    }

    int touchCount = event.getPointerCount();
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_1_DOWN:
    case MotionEvent.ACTION_POINTER_2_DOWN:
        anim = null;
        getParent().requestDisallowInterceptTouchEvent(true);
        maxTouchCount = Math.max(maxTouchCount, touchCount);
        if (touchCount >= 2) {
            if (zoomEnabled) {
                // Start pinch to zoom. Calculate distance between touch points and center point of the pinch.
                float distance = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                scaleStart = scale;
                vDistStart = distance;
                vTranslateStart.set(vTranslate.x, vTranslate.y);
                vCenterStart.set((event.getX(0) + event.getX(1)) / 2, (event.getY(0) + event.getY(1)) / 2);
            } else {
                // Abort all gestures on second touch
                maxTouchCount = 0;
            }
            // Cancel long click timer
            handler.removeMessages(MESSAGE_LONG_CLICK);
        } else if (!isQuickScaling) {
            // Start one-finger pan
            vTranslateStart.set(vTranslate.x, vTranslate.y);
            vCenterStart.set(event.getX(), event.getY());

            // Start long click timer
            handler.sendEmptyMessageDelayed(MESSAGE_LONG_CLICK, 600);
        }
        return true;
    case MotionEvent.ACTION_MOVE:
        boolean consumed = false;
        if (maxTouchCount > 0) {
            if (touchCount >= 2) {
                // Calculate new distance between touch points, to scale and pan relative to start values.
                float vDistEnd = distance(event.getX(0), event.getX(1), event.getY(0), event.getY(1));
                float vCenterEndX = (event.getX(0) + event.getX(1)) / 2;
                float vCenterEndY = (event.getY(0) + event.getY(1)) / 2;

                if (zoomEnabled && (distance(vCenterStart.x, vCenterEndX, vCenterStart.y, vCenterEndY) > 5
                        || Math.abs(vDistEnd - vDistStart) > 5 || isPanning)) {
                    isZooming = true;
                    isPanning = true;
                    consumed = true;

                    setScale(Math.min(maxScale, (vDistEnd / vDistStart) * scaleStart));

                    if (scale <= minScale()) {
                        // Minimum scale reached so don't pan. Adjust start settings so any expand will zoom in.
                        vDistStart = vDistEnd;
                        scaleStart = minScale();
                        vCenterStart.set(vCenterEndX, vCenterEndY);
                        vTranslateStart.set(vTranslate);
                    } else if (panEnabled) {
                        // Translate to place the source image coordinate that was at the center of the pinch at the start
                        // at the center of the pinch now, to give simultaneous pan + zoom.
                        float vLeftStart = vCenterStart.x - vTranslateStart.x;
                        float vTopStart = vCenterStart.y - vTranslateStart.y;
                        float vLeftNow = vLeftStart * (scale / scaleStart);
                        float vTopNow = vTopStart * (scale / scaleStart);
                        vTranslate.x = vCenterEndX - vLeftNow;
                        vTranslate.y = vCenterEndY - vTopNow;
                    } else if (sRequestedCenter != null) {
                        // With a center specified from code, zoom around that point.
                        vTranslate.x = (getWidth() / 2) - (scale * sRequestedCenter.x);
                        vTranslate.y = (getHeight() / 2) - (scale * sRequestedCenter.y);
                    } else {
                        // With no requested center, scale around the image center.
                        vTranslate.x = (getWidth() / 2) - (scale * (sWidth() / 2));
                        vTranslate.y = (getHeight() / 2) - (scale * (sHeight() / 2));
                    }

                    fitToBounds(true);
                    refreshRequiredTiles(false);
                }
            } else if (isQuickScaling) {
                // One finger zoom
                // Stole Google's Magical Formula to make sure it feels the exact same
                float dist = Math.abs(vCenterStart.y - event.getY()) * 2 + quickScaleThreshold;

                if (quickScaleLastDistance == -1F)
                    quickScaleLastDistance = dist;
                boolean isUpwards = event.getY() > quickScaleLastPoint.y;
                quickScaleLastPoint.set(0, event.getY());

                float spanDiff = (Math.abs(1 - (dist / quickScaleLastDistance)) * 0.5F);

                if (spanDiff > 0.03f || quickScaleMoved) {
                    quickScaleMoved = true;

                    float multiplier = 1;
                    if (quickScaleLastDistance > 0) {
                        multiplier = isUpwards ? (1 + spanDiff) : (1 - spanDiff);
                    }

                    setScale(Math.max(minScale(), Math.min(maxScale, scale * multiplier)));

                    if (panEnabled) {
                        float vLeftStart = vCenterStart.x - vTranslateStart.x;
                        float vTopStart = vCenterStart.y - vTranslateStart.y;
                        float vLeftNow = vLeftStart * (scale / scaleStart);
                        float vTopNow = vTopStart * (scale / scaleStart);
                        vTranslate.x = vCenterStart.x - vLeftNow;
                        vTranslate.y = vCenterStart.y - vTopNow;
                    } else if (sRequestedCenter != null) {
                        // With a center specified from code, zoom around that point.
                        vTranslate.x = (getWidth() / 2) - (scale * sRequestedCenter.x);
                        vTranslate.y = (getHeight() / 2) - (scale * sRequestedCenter.y);
                    } else {
                        // With no requested center, scale around the image center.
                        vTranslate.x = (getWidth() / 2) - (scale * (sWidth() / 2));
                        vTranslate.y = (getHeight() / 2) - (scale * (sHeight() / 2));
                    }
                }

                quickScaleLastDistance = dist;

                fitToBounds(true);
                refreshRequiredTiles(false);

                consumed = true;
            } else if (!isZooming) {
                // One finger pan - translate the image. We do this calculation even with pan disabled so click
                // and long click behaviour is preserved.
                float dx = Math.abs(event.getX() - vCenterStart.x);
                float dy = Math.abs(event.getY() - vCenterStart.y);
                if (dx > 5 || dy > 5 || isPanning) {
                    consumed = true;
                    vTranslate.x = vTranslateStart.x + (event.getX() - vCenterStart.x);
                    vTranslate.y = vTranslateStart.y + (event.getY() - vCenterStart.y);

                    float lastX = vTranslate.x;
                    float lastY = vTranslate.y;
                    fitToBounds(true);
                    boolean atXEdge = lastX != vTranslate.x;
                    boolean edgeXSwipe = atXEdge && dx > dy && !isPanning;
                    boolean yPan = lastY == vTranslate.y && dy > 15;
                    if (!edgeXSwipe && (!atXEdge || yPan || isPanning)) {
                        isPanning = true;
                    } else if (dx > 5) {
                        // Haven't panned the image, and we're at the left or right edge. Switch to page swipe.
                        maxTouchCount = 0;
                        handler.removeMessages(MESSAGE_LONG_CLICK);
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    if (!panEnabled) {
                        vTranslate.x = vTranslateStart.x;
                        vTranslate.y = vTranslateStart.y;
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }

                    refreshRequiredTiles(false);
                }
            }
        }
        if (consumed) {
            handler.removeMessages(MESSAGE_LONG_CLICK);
            invalidate();
            return true;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    case MotionEvent.ACTION_POINTER_2_UP:
        handler.removeMessages(MESSAGE_LONG_CLICK);
        if (isQuickScaling) {
            isQuickScaling = false;
            if (!quickScaleMoved) {
                doubleTapZoom(quickScaleCenter, vCenterStart);
            }
        }
        if (maxTouchCount > 0 && (isZooming || isPanning)) {
            if (isZooming && touchCount == 2) {
                // Convert from zoom to pan with remaining touch
                isPanning = true;
                vTranslateStart.set(vTranslate.x, vTranslate.y);
                if (event.getActionIndex() == 1) {
                    vCenterStart.set(event.getX(0), event.getY(0));
                } else {
                    vCenterStart.set(event.getX(1), event.getY(1));
                }
            }
            if (touchCount < 3) {
                // End zooming when only one touch point
                isZooming = false;
            }
            if (touchCount < 2) {
                // End panning when no touch points
                isPanning = false;
                maxTouchCount = 0;
            }
            // Trigger load of tiles now required
            refreshRequiredTiles(true);
            return true;
        }
        if (touchCount == 1) {
            isZooming = false;
            isPanning = false;
            maxTouchCount = 0;
        }
        return true;
    }
    return super.onTouchEvent(event);
}

From source file:com.example.libwidgettv.bak.AbsListView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }/*from   w  ww. j ava 2  s.  co  m*/

    if (mFastScroller != null) {
        boolean intercepted = mFastScroller.onTouchEvent(ev);
        if (intercepted) {
            return true;
        }
    }

    final int action = ev.getAction();

    View v;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        switch (mTouchMode) {
        case TOUCH_MODE_OVERFLING: {
            mFlingRunnable.endFling();
            if (mPositionScroller != null) {
                mPositionScroller.stop();
            }
            mTouchMode = TOUCH_MODE_OVERSCROLL;
            mMotionX = (int) ev.getX();
            mMotionY = mLastY = (int) ev.getY();
            mMotionCorrection = 0;
            mActivePointerId = ev.getPointerId(0);
            mDirection = 0;
            break;
        }

        default: {
            mActivePointerId = ev.getPointerId(0);
            final int x = (int) ev.getX();
            final int y = (int) ev.getY();
            int motionPosition = pointToPosition(x, y);
            if (!mDataChanged) {
                if ((mTouchMode != TOUCH_MODE_FLING) && (motionPosition >= 0)
                        && (getAdapter().isEnabled(motionPosition))) {
                    // User clicked on an actual view (and was not stopping
                    // a fling).
                    // It might be a click or a scroll. Assume it is a click
                    // until
                    // proven otherwise
                    mTouchMode = TOUCH_MODE_DOWN;
                    // FIXME Debounce
                    if (mPendingCheckForTap == null) {
                        mPendingCheckForTap = new CheckForTap();
                    }
                    postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
                } else {
                    if (mTouchMode == TOUCH_MODE_FLING) {
                        // Stopped a fling. It is a scroll.
                        createScrollingCache();
                        mTouchMode = TOUCH_MODE_SCROLL;
                        mMotionCorrection = 0;
                        motionPosition = findMotionRow(y);
                        mFlingRunnable.flywheelTouch();
                    }
                }
            }

            if (motionPosition >= 0) {
                // Remember where the motion event started
                v = getChildAt(motionPosition - mFirstPosition);
                mMotionViewOriginalTop = v.getTop();
            }
            mMotionX = x;
            mMotionY = y;
            mMotionPosition = motionPosition;
            mLastY = Integer.MIN_VALUE;
            break;
        }
        }

        // if (performButtonActionOnTouchDown(ev)) {
        // if (mTouchMode == TOUCH_MODE_DOWN) {
        // removeCallbacks(mPendingCheckForTap);
        // }
        // }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        int pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex == -1) {
            pointerIndex = 0;
            mActivePointerId = ev.getPointerId(pointerIndex);
        }
        final int y = (int) ev.getY(pointerIndex);
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            startScrollIfNeeded(y);
            break;
        case TOUCH_MODE_SCROLL:
        case TOUCH_MODE_OVERSCROLL:
            scrollIfNeeded(y);
            break;
        }
        break;
    }

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float x = ev.getX();
            final boolean inList = x > mListPadding.left && x < getWidth() - mListPadding.right;

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final AbsListView.PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    final Handler handler = getHandler();
                    if (handler != null) {
                        handler.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap
                                : mPendingCheckForLongPress);
                    }
                    mLayoutMode = LAYOUT_NORMAL;
                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;
                        setSelectedPositionInt(mMotionPosition);
                        layoutChildren();
                        child.setPressed(true);
                        positionSelector(mMotionPosition, child);
                        setPressed(true);
                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }
                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }
                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchMode = TOUCH_MODE_REST;
                                child.setPressed(false);
                                setPressed(false);
                                if (!mDataChanged) {
                                    performClick.run();
                                }
                            }
                        };
                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                    return true;
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }
            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();
            break;
        case TOUCH_MODE_SCROLL:
            final int childCount = getChildCount();
            if (childCount > 0) {
                final int firstChildTop = getChildAt(0).getTop();
                final int lastChildBottom = getChildAt(childCount - 1).getBottom();
                final int contentTop = mListPadding.top;
                final int contentBottom = getHeight() - mListPadding.bottom;
                if (mFirstPosition == 0 && firstChildTop >= contentTop
                        && mFirstPosition + childCount < mItemCount
                        && lastChildBottom <= getHeight() - contentBottom) {
                    mTouchMode = TOUCH_MODE_REST;
                    reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                } else {
                    final VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

                    final int initialVelocity = (int) (velocityTracker.getYVelocity(mActivePointerId)
                            * mVelocityScale);
                    // Fling if we have enough velocity and we aren't at a
                    // boundary.
                    // Since we can potentially overfling more than we can
                    // overscroll, don't
                    // allow the weird behavior where you can scroll to a
                    // boundary then
                    // fling further.
                    if (Math.abs(initialVelocity) > mMinimumVelocity
                            && !((mFirstPosition == 0 && firstChildTop == contentTop - mOverscrollDistance)
                                    || (mFirstPosition + childCount == mItemCount
                                            && lastChildBottom == contentBottom + mOverscrollDistance))) {
                        if (mFlingRunnable == null) {
                            mFlingRunnable = new FlingRunnable();
                        }
                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                        mFlingRunnable.start(-initialVelocity);
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                        if (mFlingRunnable != null) {
                            mFlingRunnable.endFling();
                        }
                        if (mPositionScroller != null) {
                            mPositionScroller.stop();
                        }
                    }
                }
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }
            break;

        case TOUCH_MODE_OVERSCROLL:
            if (mFlingRunnable == null) {
                mFlingRunnable = new FlingRunnable();
            }
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId);

            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
            if (Math.abs(initialVelocity) > mMinimumVelocity) {
                mFlingRunnable.startOverfling(-initialVelocity);
            } else {
                mFlingRunnable.startSpringback();
            }

            break;
        }

        setPressed(false);

        // Need to redraw since we probably aren't drawing the selector
        // anymore
        invalidate();

        final Handler handler = getHandler();
        if (handler != null) {
            handler.removeCallbacks(mPendingCheckForLongPress);
        }

        recycleVelocityTracker();

        mActivePointerId = INVALID_POINTER;

        if (PROFILE_SCROLLING) {
            if (mScrollProfilingStarted) {
                Debug.stopMethodTracing();
                mScrollProfilingStarted = false;
            }
        }
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        switch (mTouchMode) {
        case TOUCH_MODE_OVERSCROLL:
            if (mFlingRunnable == null) {
                mFlingRunnable = new FlingRunnable();
            }
            mFlingRunnable.startSpringback();
            break;

        case TOUCH_MODE_OVERFLING:
            // Do nothing - let it play out.
            break;

        default:
            mTouchMode = TOUCH_MODE_REST;
            setPressed(false);
            View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
            if (motionView != null) {
                motionView.setPressed(false);
            }
            clearScrollingCache();

            final Handler handler = getHandler();
            if (handler != null) {
                handler.removeCallbacks(mPendingCheckForLongPress);
            }

            recycleVelocityTracker();
        }

        mActivePointerId = INVALID_POINTER;
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        final int x = mMotionX;
        final int y = mMotionY;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            v = getChildAt(motionPosition - mFirstPosition);
            mMotionViewOriginalTop = v.getTop();
            mMotionPosition = motionPosition;
        }
        mLastY = y;
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        // New pointers take over dragging duties
        final int index = ev.getActionIndex();
        final int id = ev.getPointerId(index);
        final int x = (int) ev.getX(index);
        final int y = (int) ev.getY(index);
        mMotionCorrection = 0;
        mActivePointerId = id;
        mMotionX = x;
        mMotionY = y;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            v = getChildAt(motionPosition - mFirstPosition);
            mMotionViewOriginalTop = v.getTop();
            mMotionPosition = motionPosition;
        }
        mLastY = y;
        break;
    }
    }

    return true;
}

From source file:com.appunite.list.AbsHorizontalListView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        // A disabled view that is clickable still consumes the touch
        // events, it just doesn't respond to them.
        return isClickable() || isLongClickable();
    }// ww  w.  j a va  2s. co  m

    if (mPositionScroller != null) {
        mPositionScroller.stop();
    }

    if (!mIsAttached) {
        // Something isn't right.
        // Since we rely on being attached to get data set change notifications,
        // don't risk doing anything where we might try to resync and find things
        // in a bogus state.
        return false;
    }

    final int action = ev.getAction();

    View v;

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN: {
        switch (mTouchMode) {
        case TOUCH_MODE_OVERFLING: {
            mFlingRunnable.endFling();
            if (mPositionScroller != null) {
                mPositionScroller.stop();
            }
            mTouchMode = TOUCH_MODE_OVERSCROLL;
            mMotionX = mLastX = (int) ev.getX();
            mMotionY = (int) ev.getY();
            mMotionCorrection = 0;
            mActivePointerId = ev.getPointerId(0);
            mDirection = 0;
            break;
        }

        default: {
            mActivePointerId = ev.getPointerId(0);
            final int x = (int) ev.getX();
            final int y = (int) ev.getY();
            int motionPosition = pointToPosition(x, y);
            if (!mDataChanged) {
                if ((mTouchMode != TOUCH_MODE_FLING) && (motionPosition >= 0)
                        && (getAdapter().isEnabled(motionPosition))) {
                    // User clicked on an actual view (and was not stopping a fling).
                    // It might be a click or a scroll. Assume it is a click until
                    // proven otherwise
                    mTouchMode = TOUCH_MODE_DOWN;
                    // FIXME Debounce
                    if (mPendingCheckForTap == null) {
                        mPendingCheckForTap = new CheckForTap();
                    }
                    postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
                } else {
                    if (mTouchMode == TOUCH_MODE_FLING) {
                        // Stopped a fling. It is a scroll.
                        createScrollingCache();
                        mTouchMode = TOUCH_MODE_SCROLL;
                        mMotionCorrection = 0;
                        motionPosition = findMotionCol(x);
                        mFlingRunnable.flywheelTouch();
                    }
                }
            }

            if (motionPosition >= 0) {
                // Remember where the motion event started
                v = getChildAt(motionPosition - mFirstPosition);
                mMotionViewOriginalLeft = v.getLeft();
            }
            mMotionX = x;
            mMotionY = y;
            mMotionPosition = motionPosition;
            mLastX = Integer.MIN_VALUE;
            break;
        }
        }

        if (performButtonActionOnTouchDownUnhide(ev)) {
            if (mTouchMode == TOUCH_MODE_DOWN) {
                removeCallbacks(mPendingCheckForTap);
            }
        }
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        int pointerIndex = ev.findPointerIndex(mActivePointerId);
        if (pointerIndex == -1) {
            pointerIndex = 0;
            mActivePointerId = ev.getPointerId(pointerIndex);
        }
        final int x = (int) ev.getX(pointerIndex);

        if (mDataChanged) {
            // Re-sync everything if data has been changed
            // since the scroll operation can query the adapter.
            layoutChildren();
        }

        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            // Check if we have moved far enough that it looks more like a
            // scroll than a tap
            startScrollIfNeeded(x);
            break;
        case TOUCH_MODE_SCROLL:
        case TOUCH_MODE_OVERSCROLL:
            scrollIfNeeded(x);
            break;
        }
        break;
    }

    case MotionEvent.ACTION_UP: {
        switch (mTouchMode) {
        case TOUCH_MODE_DOWN:
        case TOUCH_MODE_TAP:
        case TOUCH_MODE_DONE_WAITING:
            final int motionPosition = mMotionPosition;
            final View child = getChildAt(motionPosition - mFirstPosition);

            final float y = ev.getY();
            final boolean inList = y > mListPadding.top && y < getHeight() - mListPadding.bottom;

            if (child != null && !child.hasFocusable() && inList) {
                if (mTouchMode != TOUCH_MODE_DOWN) {
                    child.setPressed(false);
                }

                if (mPerformClick == null) {
                    mPerformClick = new PerformClick();
                }

                final AbsHorizontalListView.PerformClick performClick = mPerformClick;
                performClick.mClickMotionPosition = motionPosition;
                performClick.rememberWindowAttachCount();

                mResurrectToPosition = motionPosition;

                if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) {
                    final Handler handler = getHandler();
                    if (handler != null) {
                        handler.removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap
                                : mPendingCheckForLongPress);
                    }
                    mLayoutMode = LAYOUT_NORMAL;
                    if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                        mTouchMode = TOUCH_MODE_TAP;
                        setSelectedPositionInt(mMotionPosition);
                        layoutChildren();
                        child.setPressed(true);
                        positionSelector(mMotionPosition, child);
                        setPressed(true);
                        if (mSelector != null) {
                            Drawable d = mSelector.getCurrent();
                            if (d != null && d instanceof TransitionDrawable) {
                                ((TransitionDrawable) d).resetTransition();
                            }
                        }
                        if (mTouchModeReset != null) {
                            removeCallbacks(mTouchModeReset);
                        }
                        mTouchModeReset = new Runnable() {
                            @Override
                            public void run() {
                                mTouchModeReset = null;
                                mTouchMode = TOUCH_MODE_REST;
                                child.setPressed(false);
                                setPressed(false);
                                if (!mDataChanged) {
                                    performClick.run();
                                }
                            }
                        };
                        postDelayed(mTouchModeReset, ViewConfiguration.getPressedStateDuration());
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        updateSelectorState();
                    }
                    return true;
                } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) {
                    performClick.run();
                }
            }
            mTouchMode = TOUCH_MODE_REST;
            updateSelectorState();
            break;
        case TOUCH_MODE_SCROLL:
            final int childCount = getChildCount();
            if (childCount > 0) {
                final int firstChildLeft = getChildAt(0).getLeft();
                final int lastChildRight = getChildAt(childCount - 1).getRight();
                final int contentLeft = mListPadding.left;
                final int contentRight = getWidth() - mListPadding.right;
                if (mFirstPosition == 0 && firstChildLeft >= contentLeft
                        && mFirstPosition + childCount < mItemCount
                        && lastChildRight <= getWidth() - contentRight) {
                    mTouchMode = TOUCH_MODE_REST;
                    reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                } else {
                    final VelocityTracker velocityTracker = mVelocityTracker;
                    velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);

                    final int initialVelocity = (int) (velocityTracker.getXVelocity(mActivePointerId)
                            * mVelocityScale);
                    // Fling if we have enough velocity and we aren't at a boundary.
                    // Since we can potentially overfling more than we can overscroll, don't
                    // allow the weird behavior where you can scroll to a boundary then
                    // fling further.
                    if (Math.abs(initialVelocity) > mMinimumVelocity
                            && !((mFirstPosition == 0 && firstChildLeft == contentLeft - mOverscrollDistance)
                                    || (mFirstPosition + childCount == mItemCount
                                            && lastChildRight == contentRight + mOverscrollDistance))) {
                        if (mFlingRunnable == null) {
                            mFlingRunnable = new FlingRunnable();
                        }
                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);

                        mFlingRunnable.start(-initialVelocity);
                    } else {
                        mTouchMode = TOUCH_MODE_REST;
                        reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
                        if (mFlingRunnable != null) {
                            mFlingRunnable.endFling();
                        }
                        if (mPositionScroller != null) {
                            mPositionScroller.stop();
                        }
                    }
                }
            } else {
                mTouchMode = TOUCH_MODE_REST;
                reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
            }
            break;

        case TOUCH_MODE_OVERSCROLL:
            if (mFlingRunnable == null) {
                mFlingRunnable = new FlingRunnable();
            }
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            final int initialVelocity = (int) velocityTracker.getXVelocity(mActivePointerId);

            reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING);
            if (Math.abs(initialVelocity) > mMinimumVelocity) {
                mFlingRunnable.startOverfling(-initialVelocity);
            } else {
                mFlingRunnable.startSpringback();
            }

            break;
        }

        setPressed(false);

        if (mEdgeGlowLeft != null) {
            mEdgeGlowLeft.onRelease();
            mEdgeGlowRight.onRelease();
        }

        // Need to redraw since we probably aren't drawing the selector anymore
        invalidate();

        final Handler handler = getHandler();
        if (handler != null) {
            handler.removeCallbacks(mPendingCheckForLongPress);
        }

        recycleVelocityTracker();

        mActivePointerId = INVALID_POINTER;

        if (PROFILE_SCROLLING) {
            if (mScrollProfilingStarted) {
                Debug.stopMethodTracing();
                mScrollProfilingStarted = false;
            }
        }

        // FIXME not needed bacaues we could not implement strict span (j.m.)
        //            if (mScrollStrictSpan != null) {
        //                mScrollStrictSpan.finish();
        //                mScrollStrictSpan = null;
        //            }
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        switch (mTouchMode) {
        case TOUCH_MODE_OVERSCROLL:
            if (mFlingRunnable == null) {
                mFlingRunnable = new FlingRunnable();
            }
            mFlingRunnable.startSpringback();
            break;

        case TOUCH_MODE_OVERFLING:
            // Do nothing - let it play out.
            break;

        default:
            mTouchMode = TOUCH_MODE_REST;
            setPressed(false);
            View motionView = this.getChildAt(mMotionPosition - mFirstPosition);
            if (motionView != null) {
                motionView.setPressed(false);
            }
            clearScrollingCache();

            final Handler handler = getHandler();
            if (handler != null) {
                handler.removeCallbacks(mPendingCheckForLongPress);
            }

            recycleVelocityTracker();
        }

        if (mEdgeGlowLeft != null) {
            mEdgeGlowLeft.onRelease();
            mEdgeGlowRight.onRelease();
        }
        mActivePointerId = INVALID_POINTER;
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        final int x = mMotionX;
        final int y = mMotionY;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            v = getChildAt(motionPosition - mFirstPosition);
            mMotionViewOriginalLeft = v.getLeft();
            mMotionPosition = motionPosition;
        }
        mLastX = x;
        break;
    }

    case MotionEvent.ACTION_POINTER_DOWN: {
        // New pointers take over dragging duties
        final int index = ev.getActionIndex();
        final int id = ev.getPointerId(index);
        final int x = (int) ev.getX(index);
        final int y = (int) ev.getY(index);
        mMotionCorrection = 0;
        mActivePointerId = id;
        mMotionX = x;
        mMotionY = y;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            v = getChildAt(motionPosition - mFirstPosition);
            mMotionViewOriginalLeft = v.getLeft();
            mMotionPosition = motionPosition;
        }
        mLastX = x;
        break;
    }
    }

    return true;
}

From source file:com.example.aaron.test.MyGLSurfaceView.java

@Override
public boolean onTouchEvent(MotionEvent e) {

    float x = e.getX();
    float y = e.getY();

    int cc = 0;/*from  ww  w.  ja  v a 2s .  co m*/

    /*
            float xGL=(width1/2-x)/(float)(height1/1.85);
            float yGL=( height1/2+30-y)/(float)(height1/1.85);
    */

    float xGL = (width1 / 2 - x) / (float) (height1 / 2);
    float yGL = (height1 / 2 - y) / (float) (height1 / 2);

    workspace = -(width1 - 115) / (height1 * 2) + .15f - mRenderer.slider;

    mRenderer.tempFun(xGL, yGL);

    switch (e.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        mRenderer.tToggle = 1;

        for (int i = 0; i < maxBots; i++) {
            if (tList[i].getOn() == 1) {
                cc = cc + 1;
                if (Math.abs(tList[i].getX() * getScale() + mRenderer.dragX * mRenderer.scale - xGL) < .1f
                        && Math.abs(
                                tList[i].getY() * getScale() + mRenderer.dragY * mRenderer.scale - yGL) < .1f) {
                    if (state[i] == 0) {
                        state[i] = 1;
                    } else {
                        state[i] = 0;
                    }
                    v.vibrate(50);
                }
            }
        }

        if (antispam > 1) {

            if (xGL < .85 && xGL > .65 && yGL > -.95 && yGL < -.75) {
                mRenderer.scale = mRenderer.scale + .5f;
                mRenderer.textList.get(1).setText("Scale: " + truncateDecimal(mRenderer.scale, 1) + "x");
                mRenderer.textList.get(2).setText(truncateDecimal(2 / mRenderer.scale, 2) + " ft");
                v.vibrate(75);
                break;
            }
            if (xGL < 1.2f && xGL > .95f && yGL > -.85f && yGL < -.75 && mRenderer.scale > .5f) {
                mRenderer.scale = mRenderer.scale - .5f;
                mRenderer.textList.get(1).setText("Scale: " + truncateDecimal(mRenderer.scale, 1) + "x");
                mRenderer.textList.get(2).setText(truncateDecimal(2 / mRenderer.scale, 2) + " ft");
                v.vibrate(75);
                break;
            }

            if (mRenderer.commit.blocked == false && xGL < mRenderer.commit.left - mRenderer.slider
                    && xGL > mRenderer.commit.right - mRenderer.slider && yGL > mRenderer.commit.down
                    && yGL < mRenderer.commit.up)
                if (mRenderer.commit.active == true) {
                    mRenderer.commit.active = false;
                    newAction = true;
                    v.vibrate(50);
                } else {
                    mRenderer.commit.active = true;
                    if (mRenderer.customFormation.active) {
                        mRenderer.justifyRelativePositions();
                        mRenderer.resetScaleAndRotation();
                        mRenderer.formation.send = true;
                    }
                    newAction = true;
                    v.vibrate(50);
                }

            //Turn on voronoi toggle
            if (buttonPush(xGL, yGL, mRenderer.vorToggle))
                if (mRenderer.getvToggle() == 1) {
                    mRenderer.setvToggle(0);
                    v.vibrate(500);
                } else {
                    mRenderer.setvToggle(1);
                    v.vibrate(500);
                }
            vFlag = mRenderer.getvToggle();

            //Turn on Free Draw Toggle
            if (buttonPush(xGL, yGL, mRenderer.freeDrawToggle))
                if (mRenderer.getfToggle() == 1) {
                    mRenderer.setfToggle(0);
                    fFlag = mRenderer.getfToggle();
                } else {
                    mRenderer.setfToggle(1);
                    mRenderer.eraseFreeLine();
                    freeDrawCount = 0;
                    previousy = 0;
                    previousx = 0;
                    connectable = 0;
                    firstPointFreeDraw[0] = -1000;
                    firstPointFreeDraw[1] = -1000;
                    fFlag = mRenderer.getfToggle();
                }

            //Toggable swarm for Path Follower
            /*if (mRenderer.getfToggle()==1 && xGL<mRenderer.swarmToggle.left- mRenderer.slider&& xGL>mRenderer.swarmToggle.right-mRenderer.slider&& yGL > mRenderer.swarmToggle.down && yGL < mRenderer.swarmToggle.up)
                if (mRenderer.swarmToggle.active == true) {
                    mRenderer.swarmToggle.active =  false;
                    v.vibrate(50);
                } else {
                    mRenderer.swarmToggle.active =  true;
                    v.vibrate(50);
                }
            */ //THIS FEATURE CAN BE ADDED LATER IF WE WANT

            //Turn on Way Point Toggle
            if (buttonPush(xGL, yGL, mRenderer.wayPointToggle))
                if (mRenderer.getpToggle() == 1) {
                    mRenderer.setpToggle(0);
                    pFlag2 = 0;
                    mRenderer.setpToggle2(pFlag2);
                    mRenderer.tToggle = 1;
                    pX = 0;
                    pY = 0;
                } else {
                    mRenderer.tToggle = 0;
                    mRenderer.setpToggle(1);
                    mRenderer.voronoiDeploymentToggle.active = false;
                    pX = 0;
                    pY = 0;
                }
            pFlag = mRenderer.getpToggle();

            //Turn on AndroneToggle
            if (buttonPush(xGL, yGL, mRenderer.ardronePrefToggle))
                if (mRenderer.getAPToggle() == 1) {
                    mRenderer.setAPToggle(0);
                    mRenderer.SINFO_FLAG = true;
                } else {
                    mRenderer.setAPToggle(1);
                    mRenderer.SINFO_FLAG = false;
                }

            if (buttonPush(xGL, yGL, mRenderer.gaussianTrackToggle)) {
                if (mRenderer.gaussianTrackToggle.active == true) {
                    mRenderer.gaussianTrackToggle.active = false;

                } else {
                    mRenderer.gaussianTrackToggle.active = true;

                }
                v.vibrate(50);
            }

            if (buttonPush(xGL, yGL, mRenderer.formation)) {
                if (mRenderer.formation.active == true) {
                    mRenderer.formation.active = false;
                    mRenderer.formation.send = false;
                } else {
                    mRenderer.formation.active = true;
                    mRenderer.formation.send = true;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.formationToggleTransform)) {
                if (mRenderer.formationToggleTransform.active == true) {
                    mRenderer.formationToggleTransform.active = false;
                } else {
                    mRenderer.formationToggleTransform.active = true;
                    mRenderer.customFormation.active = false;
                    mRenderer.commit.blocked = true;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.customFormation)) {
                if (mRenderer.customFormation.active == true) {
                    mRenderer.customFormation.active = false;
                    mRenderer.commit.blocked = true;
                } else {
                    mRenderer.customFormation.active = true;
                    mRenderer.resetFormation();
                    mRenderer.refreshCenter();
                    mRenderer.formationToggleTransform.active = false;
                    mRenderer.commit.blocked = false;
                    mRenderer.commit.active = false;
                    mRenderer.refreshFormationtext();
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.ardroneAddToggle)) {
                dummyFlag = 1;
            }

            if (buttonPush(xGL, yGL, mRenderer.addToggle)) {
                mRenderer.formation.val1 = mRenderer.formation.val1 + 1;
                mRenderer.refreshFormationtext();
                mRenderer.formation.send = true;
            }

            if (buttonPush(xGL, yGL, mRenderer.subtractToggle)) {
                mRenderer.formation.val1 = mRenderer.formation.val1 - 1;
                mRenderer.refreshFormationtext();
                mRenderer.formation.send = true;
            }

            if (buttonPush(xGL, yGL, mRenderer.freeBoundarytoggle)) {
                if (mRenderer.freeBoundarytoggle.active == true) {
                    mRenderer.freeBoundarytoggle.active = false;
                    freeBoundaryFlag = true;
                    closedBoundaryFlag = false;
                } else {
                    mRenderer.freeBoundarytoggle.active = true;
                    closedBoundaryFlag = true;
                    freeBoundaryFlag = false;
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.obstacleLineToggle)) {
                System.out.println("obstacle Line Pushed");

                addObstacleFlag = true;
                mRenderer.makeObstacle();
                mRenderer.fToggle = 0;
                fFlag = mRenderer.fToggle;
            }

            //IF GAUSS TOGGLE SELECTED
            if (buttonPush(xGL, yGL, mRenderer.gaussToggle))
                if (mRenderer.getgToggle() == 1) {
                    mRenderer.setgToggle(0);
                    gFlag2 = 0;
                    mRenderer.setgToggle2(gFlag2);
                } else {
                    mRenderer.setgToggle(1);
                }
            gFlag = mRenderer.getgToggle();

            //IF GAUSSPATH TOGGLE SELECTED
            /*if (xGL<mRenderer.temptoggle.left- mRenderer.slider&& xGL>mRenderer.temptoggle.right-mRenderer.slider&& yGL > mRenderer.temptoggle.down && yGL < mRenderer.temptoggle.up)
                if (mRenderer.getgpToggle() == 1) {
                    mRenderer.setgpToggle(0);
                    mRenderer.eraseGaussLine();
                    v.vibrate(50);
                } else {
                    mRenderer.setgpToggle(1);
                    mRenderer.makeGaussPoints();
                    v.vibrate(50);
                }*/
            gpFlag = mRenderer.getgpToggle();

            //Toggle for voronoi deployment
            if (buttonPush(xGL, yGL, mRenderer.voronoiDeploymentToggle))
                if (mRenderer.voronoiDeploymentToggle.active == true) {
                    mRenderer.voronoiDeploymentToggle.active = false;
                    newAction = true;
                } else {
                    newAction = true;
                    mRenderer.voronoiDeploymentToggle.active = true;
                    mRenderer.setpToggle(0);
                    mRenderer.setpToggle2(0);
                }

            if (buttonPush(xGL, yGL, mRenderer.dragToggle)) {
                addedTurtleNumber = 0;
                addedTurtleX = .1f;
                addedTurtleY = .1f;
                for (int j = 41; j < 44; j++) {
                    if (tList[j].getOn() != 1) {
                        addedTurtleNumber = j;
                    }
                }
                if (addedTurtleNumber == 0) {
                    addTurtleFlag = false;
                } else {
                    addTurtleFlag = true;

                }
            }

            if (buttonPush(xGL, yGL, mRenderer.addTurtle)) {
                addedTurtleNumber = 0;
                addedTurtleX = .1f;
                addedTurtleY = .1f;
                for (int j = 1; j < 11; j++) {
                    if (tList[j].getOn() != 1) {
                        addedTurtleNumber = j;
                    }
                }
                if (addedTurtleNumber == 0) {
                    addTurtleFlag = false;
                } else {
                    addTurtleFlag = true;

                }
            }

            if (buttonPush(xGL, yGL, mRenderer.dragRobotToggle)) {
                if (mRenderer.dragRobotToggle.active == false) {
                    mRenderer.dragRobotToggle.active = true;
                } else {
                    mRenderer.dragRobotToggle.active = false;
                }
            }

            if (buttonPush(xGL, yGL, mRenderer.gaussianPathToggle)) {
                if (mRenderer.gaussianPathToggle.active == true) {
                    mRenderer.gaussianPathToggle.active = false;
                    mRenderer.gaussPathPlay.active = false;
                } else {
                    mRenderer.gaussianPathToggle.active = true;
                    if (mRenderer.getfToggle() == 1) {//
                        mRenderer.gaussPathPlay.active = true;
                        mRenderer.updateGauss(
                                mRenderer.pathArray.pose[0].x * mRenderer.scale
                                        + mRenderer.dragX * mRenderer.scale,
                                mRenderer.pathArray.pose[0].y * mRenderer.scale
                                        + mRenderer.dragY * mRenderer.scale,
                                0);
                        gaussianPathArrayIteration = 1;
                        deltaX = (mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x)
                                / (float) Math.sqrt(Math
                                        .pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x, 2)
                                        + Math.pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y,
                                                2));
                        deltaY = (mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y)
                                / (float) Math.sqrt(Math
                                        .pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].x
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].x, 2)
                                        + Math.pow(mRenderer.pathArray.pose[gaussianPathArrayIteration].y
                                                - mRenderer.pathArray.pose[gaussianPathArrayIteration - 1].y,
                                                2));

                    }
                }
            }

            if (xGL < mRenderer.clearAll.left - mRenderer.slider
                    && xGL > mRenderer.clearAll.right - mRenderer.slider && yGL > mRenderer.clearAll.down
                    && yGL < mRenderer.clearAll.up) {
                if (mRenderer.clearAll.active == true) {
                    mRenderer.clearAll.active = false;
                    v.vibrate(50);
                } else {
                    mRenderer.clearAll.active = true;
                    v.vibrate(50);
                }
            }

            if (xGL < mRenderer.resetToggle.left - mRenderer.slider
                    && xGL > mRenderer.resetToggle.right - mRenderer.slider && yGL > mRenderer.resetToggle.down
                    && yGL < mRenderer.resetToggle.up) {
                if (mRenderer.resetToggle.active == true) {
                    mRenderer.resetToggle.active = false;
                    v.vibrate(50);
                } else {
                    mRenderer.resetToggle.active = true;
                    v.vibrate(50);
                }
            }

            /*                    if (xGL<-(width1-90)/height1+.05f && xGL>-(width1-90)/height1 && yGL >-(height1-10)/(height1)-mRenderer.slider  && yGL < -(height1-10)/(height1)+05f-mRenderer.slider ){
                    
            }*/

            //Clear button
            if (xGL < mRenderer.clear.left - mRenderer.slider && xGL > mRenderer.clear.right - mRenderer.slider
                    && yGL > mRenderer.clear.down && yGL < mRenderer.clear.up) {
                if (mRenderer.getgToggle() == 1) {
                    gInd = 0;
                    mRenderer.clearGauss();

                }
                if (mRenderer.dragToggle.active == true) {
                    obsticle.on = 0;
                }

                deleteTurtleNumber = -1;
                for (int i = 0; i < maxBots; i++) {
                    if (tList[i].getState() == 1) {
                        deleteTurtleNumber = i;
                    }
                }
                if (deleteTurtleNumber != -1) {
                    deleteTurtleFlag = true;
                } else {
                    deleteTurtleFlag = false;
                }

                v.vibrate(75);
                mRenderer.clear.active = true;

            }

        }

        if (pFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL - mRenderer.dragX * mRenderer.scale;
            pY = yGL - mRenderer.dragY * mRenderer.scale;
            pFlag2 = 1;
            mRenderer.setpToggle2(pFlag2);
            mRenderer.setWayPointValues(pX, pY);
        }

        if (gFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            //pX=xGL;
            //pY=yGL;
            gFlag2 = 1;
            mRenderer.setgToggle2(gFlag2);

            //System.out.println("GAUSSIAN INDEX: " +gInd);
            //mRenderer.setWayPointValues(xGL, yGL);
            //mRenderer.setGaussValues(xGL, yGL, gInd);
            //mRenderer.setGaussScale(1f);
            if (gInd < 99) {
                //mRenderer.addGaussStuff(xGL, yGL, 1f,gInd);
                //TEMP FIX
                mRenderer.addGaussStuff(xGL, yGL, 1f, 0);
            }
            gInd++;
        }

        if (mRenderer.gToggle == 0 && mRenderer.fToggle == 0 && mRenderer.directionalDrag.active == true
                && (mRenderer.directionalDrag.active == true
                        && xGL < mRenderer.directionalDrag.left - mRenderer.slider
                        && xGL > mRenderer.directionalDrag.right - mRenderer.slider
                        && yGL > mRenderer.directionalDrag.down && yGL < mRenderer.directionalDrag.up)) {
            v.vibrate(50);
            if (xGL > mRenderer.directionalDrag.left
                    + (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX - .01f;
            }
            if (xGL < mRenderer.directionalDrag.left
                    + 2 * (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX + .01f;
            }

            if (yGL > mRenderer.directionalDrag.up
                    + (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY - .01f;
            }
            if (yGL < mRenderer.directionalDrag.up
                    + 2 * (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY + .01f;
            }
        }

    case MotionEvent.ACTION_POINTER_DOWN:
        //System.out.println("GAUSS 0");
        if (e.getActionIndex() == 1) {

            if (gFlag == 1) {
                float gaussX = e.getX(1);

                float gaussY = e.getY(1);

                float gauss_xGL = (width1 / 2 - gaussX) / (float) (height1 / 1.85);
                float gauss_yGL = (height1 / 2 + 30 - gaussY) / (float) (height1 / 1.85);

                float gauss_dx = gauss_xGL - xGL;
                float gauss_dy = gauss_yGL - yGL;

                float dgauss = (float) Math.sqrt(Math.pow(gauss_dx, 2) + Math.pow(gauss_dy, 2));

                gaussScale = dgauss / .2f;
                //System.out.println("SCALE");
                //mRenderer.addGaussStuff(xGL, yGL, gaussScale,gInd-1);
                //mRenderer.setGaussScale(gInd-1, gaussScale);
                //TEMP FIX
                mRenderer.setGaussScale(0, gaussScale);
            }
        }

    case MotionEvent.ACTION_MOVE:

        mRenderer.tToggle = 1;
        float dx = x - mPreviousX;
        float dy = y - mPreviousY;

        if (mRenderer.dragRobotToggle.active) {
            for (int i = 0; i < maxBots; i++) {
                if (tList[i].getOn() == 1) {
                    if (Math.abs(tList[i].getX() * getScale() + mRenderer.dragX * mRenderer.scale - xGL) < .1f
                            && Math.abs(tList[i].getY() * getScale() + mRenderer.dragY * mRenderer.scale
                                    - yGL) < .1f) {
                        addedTurtleNumber = i;
                        addedTurtleX = xGL / getScale() - mRenderer.dragX;
                        addedTurtleY = yGL / getScale() - mRenderer.dragY;
                        addTurtleFlag = true;
                        v.vibrate(50);
                    }
                }
            }

        }
        //pX+(float)Math.cos(formation.val4)*.3f+dragX*scale, pY+(float)Math.sin(formation.val4)*.3f+dragY*scale, 0);
        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(mRenderer.formation.x * mRenderer.scale
                    + (float) Math.cos(mRenderer.formation.val4) * .3f * mRenderer.scale
                    + mRenderer.dragX * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale
                            + (float) Math.sin(mRenderer.formation.val4) * .3f * mRenderer.scale
                            + mRenderer.dragY * mRenderer.scale - yGL) < .15f) {
                mRenderer.formation.val4 = (float) Math.atan2(
                        yGL - (mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale),
                        xGL - (mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale));
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.rotation_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.rotation_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(
                    mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale
                            - yGL) < .15f) {
                pX = xGL - mRenderer.scale * mRenderer.dragX;
                pY = yGL - mRenderer.scale * mRenderer.dragY;
                mRenderer.setWayPointValues(pX, pY);
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.center_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.center_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.formationToggleTransform.active) {

            if (Math.abs(mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale
                    + mRenderer.formation.val3 * .2f * mRenderer.scale - xGL) < .15f
                    && Math.abs(mRenderer.formation.y * mRenderer.scale + mRenderer.dragY * mRenderer.scale
                            - yGL) < .15f) {
                mRenderer.formation.val3 = (xGL
                        - (mRenderer.formation.x * mRenderer.scale + mRenderer.dragX * mRenderer.scale)) / .2f
                        / mRenderer.scale;
                mRenderer.refreshFormationtext();
                mRenderer.justifyFormationLocations();
                mRenderer.scale_block.active = true;
                mRenderer.formation.send = true;
            } else {
                mRenderer.scale_block.active = false;
            }
        }

        if (mRenderer.formation.active && mRenderer.customFormation.active) {
            for (int i = 0; i < mRenderer.formation.val1; i++) {
                if (target(convertToScreenPosition(mRenderer.formation_locations.x[i], true), xGL,
                        convertToScreenPosition(mRenderer.formation_locations.y[i], false), yGL, .15f)) {
                    //mRenderer.formation.x=mRenderer.formation.x*mRenderer.formation.val1-mRenderer.formation_locations.x[i];
                    //mRenderer.formation.y=mRenderer.formation.y*mRenderer.formation.val1-mRenderer.formation_locations.y[i];

                    mRenderer.formation_locations.x[i] = convertToGlobalPosition(xGL, true);
                    mRenderer.formation_locations.y[i] = convertToGlobalPosition(yGL, false);

                    //mRenderer.formation.x=mRenderer.formation.x+mRenderer.formation_locations.x[i];
                    //mRenderer.formation.x=mRenderer.formation.x/mRenderer.formation.val1;

                    //mRenderer.formation.y=mRenderer.formation.y+mRenderer.formation_locations.y[i];
                    //mRenderer.formation.y=mRenderer.formation.y/mRenderer.formation.val1;
                    mRenderer.refreshCenter();
                    break;
                }
            }
        }

        if (mRenderer.dragToggle.active == true && xGL > workspace && xGL < mapLeft && yGL < mapTop
                && yGL > mapBottom) {
            obsticle.x = xGL / getScale() - mRenderer.dragX;
            obsticle.y = yGL / getScale() - mRenderer.dragY;
            obsticle.Aw = 1;
            obsticle.on = 1;
            newObstacle = true;
        }

        if (mRenderer.gToggle == 0 && mRenderer.fToggle == 0 && mRenderer.directionalDrag.active == true
                && (mRenderer.directionalDrag.active == true
                        && xGL < mRenderer.directionalDrag.left - mRenderer.slider
                        && xGL > mRenderer.directionalDrag.right - mRenderer.slider
                        && yGL > mRenderer.directionalDrag.down && yGL < mRenderer.directionalDrag.up)) {
            v.vibrate(50);
            if (xGL > mRenderer.directionalDrag.left
                    + (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX - .01f;
                System.out.println("LEFT");

            }
            if (xGL < mRenderer.directionalDrag.left
                    + 2 * (mRenderer.directionalDrag.right - mRenderer.directionalDrag.left) / 3
                    - mRenderer.slider) {
                mRenderer.dragX = mRenderer.dragX + .01f;
                System.out.println("RIGHT");
            }

            if (yGL > mRenderer.directionalDrag.up
                    + (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY - .01f;
                System.out.println("UP");
            }
            if (yGL < mRenderer.directionalDrag.up
                    + 2 * (mRenderer.directionalDrag.down - mRenderer.directionalDrag.up) / 3) {
                mRenderer.dragY = mRenderer.dragY + .01f;
                System.out.println("DOWN");
            }
        }

        if (fFlag == 1 && (Math.abs(xGL - previousx) > .03f || Math.abs(yGL - previousy) > .03f)
                && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            if (previousx != 0 && previousy != 0) {
                if (firstPointFreeDraw[0] == -1000) {
                    firstPointFreeDraw[0] = previousx;
                    firstPointFreeDraw[1] = previousy;
                } else if (xGL > firstPointFreeDraw[0] + .1f || xGL < firstPointFreeDraw[0] - .1f
                        || yGL > firstPointFreeDraw[1] + .1f || yGL < firstPointFreeDraw[1] - .1f) {
                    connectable = 1;
                } else if (connectable == 1) {
                    fFlag = 0;
                    setFreeDrawCoordinates(firstPointFreeDraw[0], firstPointFreeDraw[1], previousx, previousy,
                            true);
                    v.vibrate(50);
                }

                if (fFlag == 1) {
                    setFreeDrawCoordinates(xGL - mRenderer.dragX * mRenderer.scale,
                            yGL - mRenderer.dragY * mRenderer.scale,
                            previousx - mRenderer.dragX * mRenderer.scale,
                            previousy - mRenderer.dragY * mRenderer.scale, false);
                }

            }

            previousx = xGL;
            previousy = yGL;
        }

        // reverse direction of rotation above the mid-line
        if (y > getHeight() / 2) {
            dx = dx * -1;
        }

        // reverse direction of rotation to left of the mid-line
        if (x < getWidth() / 2) {
            dy = dy * -1;
        }

        if (yGL > -.15f && yGL < .15f && xGL > -(width1 - 115) / (height1 * 2) - mRenderer.slider
                && xGL < -(width1 - 115) / (height1 * 2) - mRenderer.slider + .15f) {
            mRenderer.slider = -(width1 - 115) / (height1 * 2) - xGL;
            if (mRenderer.slider < .1f) {
                mRenderer.slider = 0;
            } else if (mRenderer.slider > -.1f + (width1 - 115) / (height1 * 2)) {
                mRenderer.slider = (width1 - 115) / (height1 * 2);
            }
        }

        mRenderer.setAngle(mRenderer.getAngle() + ((dx + dy) * TOUCH_SCALE_FACTOR)); // = 180.0f / 320
        requestRender();

        if (pFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL - mRenderer.scale * mRenderer.dragX;
            pY = yGL - mRenderer.scale * mRenderer.dragY;
            pFlag2 = 1;
            mRenderer.setpToggle2(pFlag2);
            mRenderer.setWayPointValues(pX, pY);
        }

        //moving gauss
        if (gFlag == 1 && xGL > workspace && xGL < mapLeft && yGL < mapTop && yGL > mapBottom) {
            pX = xGL;
            pY = yGL;
            gFlag2 = 1;
            mRenderer.setpToggle2(gFlag2);
            //TEMP FIX
            mRenderer.updateGauss(xGL, yGL, 0);
            //mRenderer.updateGauss(xGL, yGL, gInd-1);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar.right - .04f - mRenderer.slider && yGL > mRenderer.dial.down
                && yGL < mRenderer.dial.up) {
            mRenderer.dial1 = -(xGL - mRenderer.bar.left - mRenderer.slider) / .6f;
            if (mRenderer.dial1 < 0.05) {
                mRenderer.dial1 = 0;
            } else if (mRenderer.dial1 > 1) {
                mRenderer.dial1 = 1;
            } else if (mRenderer.dial1 < .15) {
                mRenderer.dial1 = .1f;
            } else if (mRenderer.dial1 < .25) {
                mRenderer.dial1 = .2f;
            } else if (mRenderer.dial1 < .35) {
                mRenderer.dial1 = .3f;
            } else if (mRenderer.dial1 < .45) {
                mRenderer.dial1 = .4f;
            } else if (mRenderer.dial1 < .55) {
                mRenderer.dial1 = .5f;
            } else if (mRenderer.dial1 < .65) {
                mRenderer.dial1 = .6f;
            } else if (mRenderer.dial1 < .75) {
                mRenderer.dial1 = .7f;
            } else if (mRenderer.dial1 < .85) {
                mRenderer.dial1 = .8f;
            } else if (mRenderer.dial1 < .95) {
                mRenderer.dial1 = .9f;
            } else {
                mRenderer.dial1 = 1;
            }
            mRenderer.textListGaussian.get(1).setText(" " + truncateDecimal(mRenderer.dial1, 1));
            mRenderer.dial.active = true;
            v.vibrate(50);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar_2.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar_2.right - .04f - mRenderer.slider && yGL > mRenderer.dial_2.down
                && yGL < mRenderer.dial_2.up) {
            mRenderer.dial2 = -(xGL - mRenderer.bar_2.left - mRenderer.slider) / .6f;
            if (mRenderer.dial2 < 0.05) {
                mRenderer.dial2 = 0;
            } else if (mRenderer.dial2 > 1) {
                mRenderer.dial2 = 1;
            } else if (mRenderer.dial2 < .15) {
                mRenderer.dial2 = .1f;
            } else if (mRenderer.dial2 < .25) {
                mRenderer.dial2 = .2f;
            } else if (mRenderer.dial2 < .35) {
                mRenderer.dial2 = .3f;
            } else if (mRenderer.dial2 < .45) {
                mRenderer.dial2 = .4f;
            } else if (mRenderer.dial2 < .55) {
                mRenderer.dial2 = .5f;
            } else if (mRenderer.dial2 < .65) {
                mRenderer.dial2 = .6f;
            } else if (mRenderer.dial2 < .75) {
                mRenderer.dial2 = .7f;
            } else if (mRenderer.dial2 < .85) {
                mRenderer.dial2 = .8f;
            } else if (mRenderer.dial2 < .95) {
                mRenderer.dial2 = .9f;
            } else {
                mRenderer.dial2 = 1;
            }
            mRenderer.textListGaussian.get(3).setText(" " + truncateDecimal(mRenderer.dial2, 1));
            mRenderer.dial_2.active = true;
            v.vibrate(50);
        }

        if (mRenderer.gToggle == 1 && xGL < mRenderer.bar_3.left + .04f - mRenderer.slider
                && xGL > mRenderer.bar_3.right - .04f - mRenderer.slider && yGL > mRenderer.dial_3.down
                && yGL < mRenderer.dial_3.up) {
            mRenderer.dial3 = -(xGL - mRenderer.bar_3.left - mRenderer.slider) / .6f;
            if (mRenderer.dial3 < 0.05) {
                mRenderer.dial3 = 0;
            } else if (mRenderer.dial3 > 1) {
                mRenderer.dial3 = 1;
            } else if (mRenderer.dial3 < .15) {
                mRenderer.dial3 = .1f;
            } else if (mRenderer.dial3 < .25) {
                mRenderer.dial3 = .2f;
            } else if (mRenderer.dial3 < .35) {
                mRenderer.dial3 = .3f;
            } else if (mRenderer.dial3 < .45) {
                mRenderer.dial3 = .4f;
            } else if (mRenderer.dial3 < .55) {
                mRenderer.dial3 = .5f;
            } else if (mRenderer.dial3 < .65) {
                mRenderer.dial3 = .6f;
            } else if (mRenderer.dial3 < .75) {
                mRenderer.dial3 = .7f;
            } else if (mRenderer.dial3 < .85) {
                mRenderer.dial3 = .8f;
            } else if (mRenderer.dial3 < .95) {
                mRenderer.dial3 = .9f;
            } else {
                mRenderer.dial3 = 1;
            }
            mRenderer.textListGaussian.get(5).setText(" " + truncateDecimal(mRenderer.dial3, 1));
            mRenderer.dial_3.active = true;
            v.vibrate(50);
        }

    case MotionEvent.ACTION_UP:

        mRenderer.tToggle = 1;
        //mRenderer.clear.active = false;

    }

    mPreviousX = x;
    mPreviousY = y;

    antispam = 0;
    return true;
}