Example usage for android.view MotionEvent getPointerId

List of usage examples for android.view MotionEvent getPointerId

Introduction

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

Prototype

public final int getPointerId(int pointerIndex) 

Source Link

Document

Return the pointer identifier associated with a particular pointer data index in this event.

Usage

From source file:com.wb.launcher3.Page.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    if (DISABLE_TOUCH_INTERACTION) {
        return false;
    }/*from   w  w  w .ja v a2  s.co  m*/

    super.onTouchEvent(ev);

    // Skip touch handling if there are no pages to swipe
    if (getChildCount() <= 0)
        return super.onTouchEvent(ev);

    acquireVelocityTrackerAndAddMovement(ev);

    final int action = ev.getAction();

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        /*
         * 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
        mDownMotionX = mLastMotionX = ev.getX();
        mDownMotionY = mLastMotionY = ev.getY();
        mDownScrollX = getScrollX();
        float[] p = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
        mParentDownMotionX = p[0];
        mParentDownMotionY = p[1];
        mLastMotionXRemainder = 0;
        mTotalMotionX = 0;
        mActivePointerId = ev.getPointerId(0);

        if (mTouchState == TOUCH_STATE_SCROLLING) {
            pageBeginMoving();
        }
        break;

    case MotionEvent.ACTION_MOVE:
        //*/zhangwuba add 2014-5-8
        if (getLauncherDeleteAppSate()) {
            return true;
        }
        //*/
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            // Scroll to follow the motion event
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);

            if (pointerIndex == -1)
                return true;

            final float x = ev.getX(pointerIndex);
            final float deltaX = mLastMotionX + mLastMotionXRemainder - x;

            mTotalMotionX += Math.abs(deltaX);

            // Only scroll and update mLastMotionX if we have moved some discrete amount.  We
            // keep the remainder because we are actually testing if we've moved from the last
            // scrolled position (which is discrete).
            if (Math.abs(deltaX) >= 1.0f) {
                mTouchX += deltaX;
                mSmoothingTime = System.nanoTime() / NANOTIME_DIV;
                if (!mDeferScrollUpdate) {
                    scrollBy((int) deltaX, 0);
                    if (DEBUG)
                        Log.d(TAG, "onTouchEvent().Scrolling: " + deltaX);
                } else {
                    invalidate();
                }
                mLastMotionX = x;
                mLastMotionXRemainder = deltaX - (int) deltaX;
            } else {
                awakenScrollBars();
            }
        } else if (mTouchState == TOUCH_STATE_REORDERING) {
            // Update the last motion position
            mLastMotionX = ev.getX();
            mLastMotionY = ev.getY();

            // Update the parent down so that our zoom animations take this new movement into
            // account
            float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
            mParentDownMotionX = pt[0];
            mParentDownMotionY = pt[1];
            updateDragViewTranslationDuringDrag();

            // Find the closest page to the touch point
            final int dragViewIndex = indexOfChild(mDragView);

            // Change the drag view if we are hovering over the drop target
            boolean isHoveringOverDelete = isHoveringOverDeleteDropTarget((int) mParentDownMotionX,
                    (int) mParentDownMotionY);
            setPageHoveringOverDeleteDropTarget(dragViewIndex, isHoveringOverDelete);

            if (DEBUG)
                Log.d(TAG, "mLastMotionX: " + mLastMotionX);
            if (DEBUG)
                Log.d(TAG, "mLastMotionY: " + mLastMotionY);
            if (DEBUG)
                Log.d(TAG, "mParentDownMotionX: " + mParentDownMotionX);
            if (DEBUG)
                Log.d(TAG, "mParentDownMotionY: " + mParentDownMotionY);

            final int pageUnderPointIndex = getNearestHoverOverPageIndex();
            if (pageUnderPointIndex > -1 && pageUnderPointIndex != indexOfChild(mDragView)
                    && !isHoveringOverDelete) {
                mTempVisiblePagesRange[0] = 0;
                mTempVisiblePagesRange[1] = getPageCount() - 1;
                getOverviewModePages(mTempVisiblePagesRange);
                if (mTempVisiblePagesRange[0] <= pageUnderPointIndex
                        && pageUnderPointIndex <= mTempVisiblePagesRange[1]
                        && pageUnderPointIndex != mSidePageHoverIndex && mScroller.isFinished()) {
                    mSidePageHoverIndex = pageUnderPointIndex;
                    mSidePageHoverRunnable = new Runnable() {
                        @Override
                        public void run() {
                            // Setup the scroll to the correct page before we swap the views
                            snapToPage(pageUnderPointIndex);

                            // For each of the pages between the paged view and the drag view,
                            // animate them from the previous position to the new position in
                            // the layout (as a result of the drag view moving in the layout)
                            int shiftDelta = (dragViewIndex < pageUnderPointIndex) ? -1 : 1;
                            int lowerIndex = (dragViewIndex < pageUnderPointIndex) ? dragViewIndex + 1
                                    : pageUnderPointIndex;
                            int upperIndex = (dragViewIndex > pageUnderPointIndex) ? dragViewIndex - 1
                                    : pageUnderPointIndex;
                            for (int i = lowerIndex; i <= upperIndex; ++i) {
                                View v = getChildAt(i);
                                // dragViewIndex < pageUnderPointIndex, so after we remove the
                                // drag view all subsequent views to pageUnderPointIndex will
                                // shift down.
                                int oldX = getViewportOffsetX() + getChildOffset(i);
                                int newX = getViewportOffsetX() + getChildOffset(i + shiftDelta);

                                // Animate the view translation from its old position to its new
                                // position
                                AnimatorSet anim = (AnimatorSet) v.getTag(ANIM_TAG_KEY);
                                if (anim != null) {
                                    anim.cancel();
                                }

                                v.setTranslationX(oldX - newX);
                                anim = new AnimatorSet();
                                anim.setDuration(REORDERING_REORDER_REPOSITION_DURATION);
                                anim.playTogether(ObjectAnimator.ofFloat(v, "translationX", 0f));
                                anim.start();
                                v.setTag(anim);
                            }

                            removeView(mDragView);
                            onRemoveView(mDragView, false);
                            addView(mDragView, pageUnderPointIndex);
                            onAddView(mDragView, pageUnderPointIndex);
                            mSidePageHoverIndex = -1;
                            mPageIndicator.setActiveMarker(getNextPage());
                        }
                    };
                    postDelayed(mSidePageHoverRunnable, REORDERING_SIDE_PAGE_HOVER_TIMEOUT);
                }
            } else {
                removeCallbacks(mSidePageHoverRunnable);
                mSidePageHoverIndex = -1;
            }
        } else {
            determineScrollingStart(ev);
        }
        break;

    case MotionEvent.ACTION_UP:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final int activePointerId = mActivePointerId;
            final int pointerIndex = ev.findPointerIndex(activePointerId);
            final float x = ev.getX(pointerIndex);
            final VelocityTracker velocityTracker = mVelocityTracker;
            velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity(activePointerId);
            final int deltaX = (int) (x - mDownMotionX);
            final int pageWidth = getPageAt(mCurrentPage).getMeasuredWidth();
            boolean isSignificantMove = Math.abs(deltaX) > pageWidth * SIGNIFICANT_MOVE_THRESHOLD;

            mTotalMotionX += Math.abs(mLastMotionX + mLastMotionXRemainder - x);

            boolean isFling = mTotalMotionX > MIN_LENGTH_FOR_FLING
                    && Math.abs(velocityX) > mFlingThresholdVelocity;

            if (!mFreeScroll) {
                // In the case that the page is moved far to one direction and then is flung
                // in the opposite direction, we use a threshold to determine whether we should
                // just return to the starting page, or if we should skip one further.
                boolean returnToOriginalPage = false;
                if (Math.abs(deltaX) > pageWidth * RETURN_TO_ORIGINAL_PAGE_THRESHOLD
                        && Math.signum(velocityX) != Math.signum(deltaX) && isFling) {
                    returnToOriginalPage = true;
                }

                //*/Added by tyd Greg 2014-03-20,for transition effect
                if (TydtechConfig.CYCLE_ROLL_PAGES_ENABLED) {
                    m_moveNextDeltaX = deltaX;
                    m_isSignificantMoveNext = (!returnToOriginalPage && (isSignificantMove || isFling));
                }
                //*/

                int finalPage;
                // We give flings precedence over large moves, which is why we short-circuit our
                // test for a large move if a fling has been registered. That is, a large
                // move to the left and fling to the right will register as a fling to the right.
                final boolean isRtl = isLayoutRtl();
                boolean isDeltaXLeft = isRtl ? deltaX > 0 : deltaX < 0;
                boolean isVelocityXLeft = isRtl ? velocityX > 0 : velocityX < 0;
                if (((isSignificantMove && !isDeltaXLeft && !isFling) || (isFling && !isVelocityXLeft))
                        && mCurrentPage > 0) {
                    finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage - 1;
                    snapToPageWithVelocity(finalPage, velocityX);
                } else if (((isSignificantMove && isDeltaXLeft && !isFling) || (isFling && isVelocityXLeft))
                        && mCurrentPage < getChildCount() - 1) {
                    finalPage = returnToOriginalPage ? mCurrentPage : mCurrentPage + 1;
                    snapToPageWithVelocity(finalPage, velocityX);
                } else {
                    snapToDestination();
                }
            } else if (mTouchState == TOUCH_STATE_PREV_PAGE) {
                // at this point we have not moved beyond the touch slop
                // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
                // we can just page
                int nextPage = Math.max(0, mCurrentPage - 1);
                if (nextPage != mCurrentPage) {
                    snapToPage(nextPage);
                } else {
                    snapToDestination();
                }
            } else {
                if (!mScroller.isFinished()) {
                    mScroller.abortAnimation();
                }

                float scaleX = getScaleX();
                int vX = (int) (-velocityX * scaleX);
                int initialScrollX = (int) (getScrollX() * scaleX);

                mScroller.fling(initialScrollX, getScrollY(), vX, 0, Integer.MIN_VALUE, Integer.MAX_VALUE, 0,
                        0);
                invalidate();
            }
        } else if (mTouchState == TOUCH_STATE_NEXT_PAGE) {
            // at this point we have not moved beyond the touch slop
            // (otherwise mTouchState would be TOUCH_STATE_SCROLLING), so
            // we can just page
            int nextPage = Math.min(getChildCount() - 1, mCurrentPage + 1);
            if (nextPage != mCurrentPage) {
                snapToPage(nextPage);
            } else {
                snapToDestination();
            }
        } else if (mTouchState == TOUCH_STATE_REORDERING) {
            // Update the last motion position
            mLastMotionX = ev.getX();
            mLastMotionY = ev.getY();

            // Update the parent down so that our zoom animations take this new movement into
            // account
            float[] pt = mapPointFromViewToParent(this, mLastMotionX, mLastMotionY);
            mParentDownMotionX = pt[0];
            mParentDownMotionY = pt[1];
            updateDragViewTranslationDuringDrag();
            boolean handledFling = false;
            if (!DISABLE_FLING_TO_DELETE) {
                // Check the velocity and see if we are flinging-to-delete
                PointF flingToDeleteVector = isFlingingToDelete();
                if (flingToDeleteVector != null) {
                    onFlingToDelete(flingToDeleteVector);
                    handledFling = true;
                }
            }
            if (!handledFling
                    && isHoveringOverDeleteDropTarget((int) mParentDownMotionX, (int) mParentDownMotionY)) {
                onDropToDelete();
            }
        }
        //*/Added by tyd Greg 2014-03-21,for support the touch swipe gesture
        else if (mTouchState == TOUCH_SWIPE_DOWN_GESTURE) {
            if (mOnTouchSwipeGestureListener != null) {
                mOnTouchSwipeGestureListener.fireSwipeDownAction();
            }
        } else if (mTouchState == TOUCH_SWIPE_UP_GESTURE) {
            if (mOnTouchSwipeGestureListener != null) {
                mOnTouchSwipeGestureListener.fireSwipeUpAction();
            }
        }
        //*/

        else {
            if (!mCancelTap) {
                onUnhandledTap(ev);
            }
        }

        // Remove the callback to wait for the side page hover timeout
        removeCallbacks(mSidePageHoverRunnable);
        // End any intermediate reordering states
        resetTouchState();
        break;

    case MotionEvent.ACTION_CANCEL:
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            snapToDestination();
        }
        resetTouchState();
        break;

    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        releaseVelocityTracker();
        break;
    }

    return true;
}

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

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    /*//from w w w .j  a v  a2s  .co m
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onMotionEvent will be called and we do the actual
     * scrolling there.
     */

    /*
    * Shortcut the most recurring case: the user is in the dragging
    * state and he is moving his finger.  We want to intercept this
    * motion.
    */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) {
        return true;
    }

    /*
     * Don't try to intercept touch if we can't scroll anyway.
     */
    if (getScrollX() == 0 && !canScrollHorizontally(1) && getScrollY() == 0 && !canScrollVertically(1)) {
        return false;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */

        /*
        * Locally do absolute value. mLastMotionY is set to the y value
        * of the down event.
        */
        final int activePointerId = mActivePointerId;
        if (activePointerId == INVALID_POINTER) {
            // If we don't have a valid id, the touch down wasn't on content.
            break;
        }

        final int pointerIndex = ev.findPointerIndex(activePointerId);
        if (pointerIndex == -1) {
            Log.e(TAG, "Invalid pointerId=" + activePointerId + " in onInterceptTouchEvent");
            break;
        }

        final int x = (int) ev.getX(pointerIndex);
        final int y = (int) ev.getY(pointerIndex);
        final int xDiff = Math.abs(x - mLastMotionX);
        final int yDiff = Math.abs(y - mLastMotionY);
        if (xDiff > mTouchSlop || yDiff > mTouchSlop) {
            mIsBeingDragged = true;
            mLastMotionX = x;
            mLastMotionY = y;
            initVelocityTrackerIfNotExists();
            mVelocityTracker.addMovement(ev);
            final ViewParent parent = getParent();
            if (parent != null) {
                parent.requestDisallowInterceptTouchEvent(true);
            }
        }

        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        if (!inChild(x, y)) {
            mIsBeingDragged = false;
            recycleVelocityTracker();
            break;
        }

        /*
         * Remember location of down touch.
         * ACTION_DOWN always refers to pointer index 0.
         */
        mLastMotionX = x;
        mLastMotionY = y;
        mActivePointerId = ev.getPointerId(0);

        initOrResetVelocityTracker();
        mVelocityTracker.addMovement(ev);

        /*
        * If being flinged and user touches the screen, initiate drag;
        * otherwise don't.  mScroller.isFinished should be false when
        * being flinged.
        */
        mIsBeingDragged = !mScroller.isFinished();
        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        /* Release the drag */
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0,
                getVerticalScrollRange())) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
        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;
    }

    /*
    * The only time we want to intercept motion events is if we are in the
    * drag mode.
    */
    return mIsBeingDragged;
}

From source file:cc.flydev.launcher.Page.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (DISABLE_TOUCH_INTERACTION) {
        return false;
    }/*from w ww.  j av a  2s .  c o m*/

    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onTouchEvent will be called and we do the actual
     * scrolling there.
     */
    acquireVelocityTrackerAndAddMovement(ev);

    // Skip touch handling if there are no pages to swipe
    if (getChildCount() <= 0)
        return super.onInterceptTouchEvent(ev);

    /*
     * Shortcut the most recurring case: the user is in the dragging
     * state and he is moving his finger.  We want to intercept this
     * motion.
     */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState == TOUCH_STATE_SCROLLING)) {
        return true;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */
        if (mActivePointerId != INVALID_POINTER) {
            determineScrollingStart(ev);
        }
        // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
        // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
        // i.e. fall through to the next case (don't break)
        // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
        // while it's small- this was causing a crash before we checked for INVALID_POINTER)
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        // Remember location of down touch
        mDownMotionX = x;
        mDownMotionY = y;
        mDownScrollX = getScrollX();
        mLastMotionX = x;
        mLastMotionY = y;
        float[] p = mapPointFromViewToParent(this, x, y);
        mParentDownMotionX = p[0];
        mParentDownMotionY = p[1];
        mLastMotionXRemainder = 0;
        mTotalMotionX = 0;
        mActivePointerId = ev.getPointerId(0);

        /*
         * If being flinged and user touches the screen, initiate drag;
         * otherwise don't.  mScroller.isFinished should be false when
         * being flinged.
         */
        final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
        final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
        if (finishedScrolling) {
            mTouchState = TOUCH_STATE_REST;
            mScroller.abortAnimation();
        } else {
            if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
                mTouchState = TOUCH_STATE_SCROLLING;
            } else {
                mTouchState = TOUCH_STATE_REST;
            }
        }

        // check if this can be the beginning of a tap on the side of the pages
        // to scroll the current page
        if (!DISABLE_TOUCH_SIDE_PAGES) {
            if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
                if (getChildCount() > 0) {
                    if (hitsPreviousPage(x, y)) {
                        mTouchState = TOUCH_STATE_PREV_PAGE;
                    } else if (hitsNextPage(x, y)) {
                        mTouchState = TOUCH_STATE_NEXT_PAGE;
                    }
                }
            }
        }
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        resetTouchState();
        break;

    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        releaseVelocityTracker();
        break;
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mTouchState != TOUCH_STATE_REST;
}

From source file:com.example.SmartBoard.DrawingView.java

public boolean onTouchDragEvent(MotionEvent event) {

    boolean handled = false;

    JSONObject mObjectTouched;/*from w  w  w .  ja  v  a  2s  . c o m*/
    int X;
    int Y;
    int pointerId;
    int actionIndex = event.getActionIndex();

    //get coordinates and make object transparent
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
        dragFinished = false;

        //it's the first pointer, so clear all existing pointers data
        mObjectPointers.clear();
        X = (int) event.getX(actionIndex);
        Y = (int) event.getY(actionIndex);
        dragX = X;
        dragY = Y;
        String objectType = "";
        //check if we've touched inside some object
        mObjectTouched = getTouchedObject(X, Y);

        if (mObjectTouched == null) {
            return true;
        } else {
            objectType = getObjectType(mObjectTouched);
        }

        if (objectType.compareTo("Circle") == 0) {
            try {
                mObjectTouched.put("x", X);
                mObjectTouched.put("y", Y);

            } catch (JSONException e) {
                e.printStackTrace();

            }
        } else if (objectType.compareTo("Rectangle") == 0) {
            Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens"));
            tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height());
            try {
                mObjectTouched.put("dimens", tempRect.flattenToString());
            } catch (JSONException e) {

            }

        } else if (objectType.compareTo("Line") == 0) {

            if (lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                    mObjectTouched.optInt("starty")) < lengthOfLine(X, Y, mObjectTouched.optInt("stopx"),
                            mObjectTouched.optInt("stopy"))) {

                try {
                    mObjectTouched.put("startx", X);
                    mObjectTouched.put("starty", Y);
                    mObjectTouched.put("length",
                            lengthOfLine(X, Y, mObjectTouched.optInt("stopx"), mObjectTouched.optInt("stopy")));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dragLineStart = true;
            } else {
                try {
                    mObjectTouched.put("stopx", X);
                    mObjectTouched.put("stopy", Y);
                    mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                            mObjectTouched.optInt("starty")));

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                dragLineStart = false;
            }

        } else if (objectType.compareTo("Text") == 0) {
            try {
                mObjectTouched.put("x", X);
                mObjectTouched.put("y", Y);
                Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region"));
                tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y);
                mObjectTouched.put("region", tempRect.flattenToString());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        mObjectPointers.put(event.getPointerId(0), mObjectTouched);
        if (mObjectTouched != null) {
            mqtt.publishObject(mObjectTouched);
        }
        invalidate();
        handled = true;
        break;

    case MotionEvent.ACTION_POINTER_DOWN:
        break;
    case MotionEvent.ACTION_MOVE:
        dragFinished = false;

        final int pointerCount = event.getPointerCount();

        for (actionIndex = 0; actionIndex < pointerCount; actionIndex++) {
            //some pointer has moved, search it by pointer id
            pointerId = event.getPointerId(actionIndex);
            X = (int) event.getX(actionIndex);
            Y = (int) event.getY(actionIndex);

            dragX = X;
            dragY = Y;

            mObjectTouched = mObjectPointers.get(pointerId);
            if (mObjectTouched == null)
                continue; // if null no object was touched so skip

            if (mObjectTouched.optString("type").compareTo("Circle") == 0) {
                try {
                    mObjectTouched.put("x", X);
                    mObjectTouched.put("y", Y);
                } catch (JSONException e) {
                }
            } else if (mObjectTouched.optString("type").compareTo("Rectangle") == 0) {

                Rect tempRect = Rect.unflattenFromString(mObjectTouched.optString("dimens"));
                tempRect.set(X, Y, X + tempRect.width(), Y + tempRect.height());
                try {
                    mObjectTouched.put("dimens", tempRect.flattenToString());
                } catch (JSONException e) {

                }

            } else if (mObjectTouched.optString("type").compareTo("Text") == 0) {
                try {
                    mObjectTouched.put("x", X);
                    mObjectTouched.put("y", Y);
                    Rect tempRect = Rect.unflattenFromString(mObjectTouched.getString("region"));
                    tempRect.set(X, Y, tempRect.width() + X, tempRect.height() + Y);
                    mObjectTouched.put("region", tempRect.flattenToString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }

            else if (mObjectTouched.optString("type").compareTo("Line") == 0) {

                if (dragLineStart) {
                    try {
                        mObjectTouched.put("startx", X);
                        mObjectTouched.put("starty", Y);
                        mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("stopx"),
                                mObjectTouched.optInt("stopy")));

                        //mObjectTouched.put("stopx", tempStopX);
                        // mObjectTouched.put("stopy", tempStopY);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        mObjectTouched.put("stopx", X);
                        mObjectTouched.put("stopy", Y);
                        mObjectTouched.put("length", lengthOfLine(X, Y, mObjectTouched.optInt("startx"),
                                mObjectTouched.optInt("starty")));

                        //mObjectTouched.put("stopx", tempStopX);
                        // mObjectTouched.put("stopy", tempStopY);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }

            if (mObjectTouched != null) {
                mqtt.publishObject(mObjectTouched);
            }
        }

        invalidate();
        handled = true;
        break;
    case MotionEvent.ACTION_UP:
        dragFinished = true;

        mObjectPointers.clear();
        invalidate();
        handled = true;
        break;
    case MotionEvent.ACTION_CANCEL:
        handled = true;
        break;

    default:
        // do nothing
        break;
    }

    return super.onTouchEvent(event) || handled;
}

From source file:com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView2.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  www  . j  a  v a 2s .com*/

    initVelocityTrackerIfNotExists();
    mVelocityTracker.addMovement(ev);

    final int actionMasked = ev.getActionMasked();
    switch (actionMasked) {
    case MotionEvent.ACTION_DOWN: {
        onTouchDown(ev);
        break;
    }

    case MotionEvent.ACTION_MOVE: {
        onTouchMove(ev);
        break;
    }

    case MotionEvent.ACTION_UP: {
        onTouchUp(ev);
        break;
    }

    case MotionEvent.ACTION_CANCEL: {
        onTouchCancel();
        break;
    }

    case MotionEvent.ACTION_POINTER_UP: {
        onSecondaryPointerUp(ev);
        final int x = (int) mLastTouchX;
        final int y = (int) mLastTouchY;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            final View child = getChildAt(motionPosition - mFirstPosition);
            //mMotionViewOriginalTop = child.getTop();
            mMotionPosition = motionPosition;
        }
        mTouchRemainderY = 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;
        mLastTouchX = x;
        mLastTouchY = y;
        final int motionPosition = pointToPosition(x, y);
        if (motionPosition >= 0) {
            // Remember where the motion event started
            final View child = getChildAt(motionPosition - mFirstPosition);
            //mMotionViewOriginalTop = child.getTop();
            mMotionPosition = motionPosition;
        }
        mTouchRemainderY = y;
        break;
    }
    }

    return true;
}

From source file:com.n2hsu.launcher.Page.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (DISABLE_TOUCH_INTERACTION) {
        return false;
    }// ww w  .jav  a2 s. c  om

    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onTouchEvent will be called and we do the actual
     * scrolling there.
     */
    acquireVelocityTrackerAndAddMovement(ev);

    // Skip touch handling if there are no pages to swipe
    if (getChildCount() <= 0)
        return super.onInterceptTouchEvent(ev);

    /*
     * Shortcut the most recurring case: the user is in the dragging state
     * and he is moving his finger. We want to intercept this motion.
     */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState == TOUCH_STATE_SCROLLING)) {
        return true;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {
        /*
         * mIsBeingDragged == false, otherwise the shortcut would have
         * caught it. Check whether the user has moved far enough from his
         * original down touch.
         */
        if (mActivePointerId != INVALID_POINTER) {
            determineScrollingStart(ev);
        }
        // if mActivePointerId is INVALID_POINTER, then we must have missed
        // an ACTION_DOWN
        // event. in that case, treat the first occurence of a move event as
        // a ACTION_DOWN
        // i.e. fall through to the next case (don't break)
        // (We sometimes miss ACTION_DOWN events in Workspace because it
        // ignores all events
        // while it's small- this was causing a crash before we checked for
        // INVALID_POINTER)
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        // Remember location of down touch
        mDownMotionX = x;
        mDownMotionY = y;
        mDownScrollX = getScrollX();
        mLastMotionX = x;
        mLastMotionY = y;
        float[] p = mapPointFromViewToParent(this, x, y);
        mParentDownMotionX = p[0];
        mParentDownMotionY = p[1];
        mLastMotionXRemainder = 0;
        mTotalMotionX = 0;
        mActivePointerId = ev.getPointerId(0);

        /*
         * If being flinged and user touches the screen, initiate drag;
         * otherwise don't. mScroller.isFinished should be false when being
         * flinged.
         */
        final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
        final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
        if (finishedScrolling) {
            mTouchState = TOUCH_STATE_REST;
            mScroller.abortAnimation();
        } else {
            if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
                mTouchState = TOUCH_STATE_SCROLLING;
            } else {
                mTouchState = TOUCH_STATE_REST;
            }
        }

        // check if this can be the beginning of a tap on the side of the
        // pages
        // to scroll the current page
        if (!DISABLE_TOUCH_SIDE_PAGES) {
            if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
                if (getChildCount() > 0) {
                    if (hitsPreviousPage(x, y)) {
                        mTouchState = TOUCH_STATE_PREV_PAGE;
                    } else if (hitsNextPage(x, y)) {
                        mTouchState = TOUCH_STATE_NEXT_PAGE;
                    }
                }
            }
        }
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        resetTouchState();
        break;

    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        releaseVelocityTracker();
        break;
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mTouchState != TOUCH_STATE_REST;
}

From source file:com.glview.widget.AbsListView.java

private void onTouchDown(MotionEvent ev) {
    mActivePointerId = ev.getPointerId(0);

    if (mTouchMode == TOUCH_MODE_OVERFLING) {
        // Stopped the fling. It is a scroll.
        mFlingRunnable.endFling();/*  www .  j av  a2s. c  o m*/
        if (mPositionScroller != null) {
            mPositionScroller.stop();
        }
        mTouchMode = TOUCH_MODE_OVERSCROLL;
        mMotionX = (int) ev.getX();
        mMotionY = (int) ev.getY();
        mLastY = mMotionY;
        mMotionCorrection = 0;
        mDirection = 0;
    } else {
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();
        int motionPosition = pointToPosition(x, y);

        if (!mDataChanged) {
            if (mTouchMode == TOUCH_MODE_FLING) {
                // Stopped a fling. It is a scroll.
                createScrollingCache();
                mTouchMode = TOUCH_MODE_SCROLL;
                mMotionCorrection = 0;
                motionPosition = findMotionRow(y);
                mFlingRunnable.flywheelTouch();
            } else if ((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();
                }

                mPendingCheckForTap.x = ev.getX();
                mPendingCheckForTap.y = ev.getY();
                postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
            }
        }

        if (motionPosition >= 0) {
            // Remember where the motion event started
            final View v = getChildAt(motionPosition - mFirstPosition);
            mMotionViewOriginalTop = v.getTop();
        }

        mMotionX = x;
        mMotionY = y;
        mMotionPosition = motionPosition;
        mLastY = Integer.MIN_VALUE;
    }

    if (mTouchMode == TOUCH_MODE_DOWN && mMotionPosition != INVALID_POSITION
            && performButtonActionOnTouchDown(ev)) {
        removeCallbacks(mPendingCheckForTap);
    }
}

From source file:com.wb.launcher3.Page.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (DISABLE_TOUCH_INTERACTION) {
        return false;
    }/*from  w ww.j  a va 2  s .c  o m*/

    /*
     * This method JUST determines whether we want to intercept the motion.
     * If we return true, onTouchEvent will be called and we do the actual
     * scrolling there.
     */
    acquireVelocityTrackerAndAddMovement(ev);

    // Skip touch handling if there are no pages to swipe
    if (getChildCount() <= 0)
        return super.onInterceptTouchEvent(ev);

    /*
     * Shortcut the most recurring case: the user is in the dragging
     * state and he is moving his finger.  We want to intercept this
     * motion.
     */
    final int action = ev.getAction();
    if ((action == MotionEvent.ACTION_MOVE) && (mTouchState == TOUCH_STATE_SCROLLING)) {
        return true;
    }

    switch (action & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_MOVE: {
        //*/zhangwuba add 2014-5-8
        if (getLauncherDeleteAppSate()) {
            return true;
        }
        //*/

        /*
         * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check
         * whether the user has moved far enough from his original down touch.
         */
        if (mActivePointerId != INVALID_POINTER) {
            determineScrollingStart(ev);
        }
        // if mActivePointerId is INVALID_POINTER, then we must have missed an ACTION_DOWN
        // event. in that case, treat the first occurence of a move event as a ACTION_DOWN
        // i.e. fall through to the next case (don't break)
        // (We sometimes miss ACTION_DOWN events in Workspace because it ignores all events
        // while it's small- this was causing a crash before we checked for INVALID_POINTER)
        break;
    }

    case MotionEvent.ACTION_DOWN: {
        final float x = ev.getX();
        final float y = ev.getY();
        // Remember location of down touch
        mDownMotionX = x;
        mDownMotionY = y;
        mDownScrollX = getScrollX();
        mLastMotionX = x;
        mLastMotionY = y;
        float[] p = mapPointFromViewToParent(this, x, y);
        mParentDownMotionX = p[0];
        mParentDownMotionY = p[1];
        mLastMotionXRemainder = 0;
        mTotalMotionX = 0;
        mActivePointerId = ev.getPointerId(0);

        /*
         * If being flinged and user touches the screen, initiate drag;
         * otherwise don't.  mScroller.isFinished should be false when
         * being flinged.
         */
        final int xDist = Math.abs(mScroller.getFinalX() - mScroller.getCurrX());
        final boolean finishedScrolling = (mScroller.isFinished() || xDist < mTouchSlop);
        if (finishedScrolling) {
            mTouchState = TOUCH_STATE_REST;
            mScroller.abortAnimation();
        } else {
            if (isTouchPointInViewportWithBuffer((int) mDownMotionX, (int) mDownMotionY)) {
                mTouchState = TOUCH_STATE_SCROLLING;
            } else {
                mTouchState = TOUCH_STATE_REST;
            }
        }

        // check if this can be the beginning of a tap on the side of the pages
        // to scroll the current page
        if (!DISABLE_TOUCH_SIDE_PAGES) {
            if (mTouchState != TOUCH_STATE_PREV_PAGE && mTouchState != TOUCH_STATE_NEXT_PAGE) {
                if (getChildCount() > 0) {
                    if (hitsPreviousPage(x, y)) {
                        mTouchState = TOUCH_STATE_PREV_PAGE;
                    } else if (hitsNextPage(x, y)) {
                        mTouchState = TOUCH_STATE_NEXT_PAGE;
                    }
                }
            }
        }
        break;
    }

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        resetTouchState();
        break;

    case MotionEvent.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        releaseVelocityTracker();
        break;
    }

    /*
     * The only time we want to intercept motion events is if we are in the
     * drag mode.
     */
    return mTouchState != TOUCH_STATE_REST;
}

From source file:com.glview.widget.AbsListView.java

private void onTouchMove(MotionEvent ev, MotionEvent vtev) {
    int pointerIndex = ev.findPointerIndex(mActivePointerId);
    if (pointerIndex == -1) {
        pointerIndex = 0;// w ww  .  j a v  a2s  .co  m
        mActivePointerId = ev.getPointerId(pointerIndex);
    }

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

    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. If so, we'll enter scrolling mode.
        if (startScrollIfNeeded((int) ev.getX(pointerIndex), y, vtev)) {
            break;
        }
        // Otherwise, check containment within list bounds. If we're
        // outside bounds, cancel any active presses.
        final float x = ev.getX(pointerIndex);
        if (!pointInView(x, y, mTouchSlop)) {
            setPressed(false);
            final View motionView = getChildAt(mMotionPosition - mFirstPosition);
            if (motionView != null) {
                motionView.setPressed(false);
            }
            removeCallbacks(mTouchMode == TOUCH_MODE_DOWN ? mPendingCheckForTap : mPendingCheckForLongPress);
            mTouchMode = TOUCH_MODE_DONE_WAITING;
            updateSelectorState();
        }
        break;
    case TOUCH_MODE_SCROLL:
    case TOUCH_MODE_OVERSCROLL:
        scrollIfNeeded((int) ev.getX(pointerIndex), y, vtev);
        break;
    }
}

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

private void onSecondaryPointerUp(MotionEvent ev) {
    final int pointerIndex = (ev.getAction()
            & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    final int pointerId = ev.getPointerId(pointerIndex);
    if (pointerId == mActivePointerId) {
        // This was our active pointer going up. Choose a new
        // active pointer and adjust accordingly.
        // TODO: Make this decision more intelligent.
        final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
        mMotionX = (int) ev.getX(newPointerIndex);
        mMotionY = (int) ev.getY(newPointerIndex);
        mMotionCorrection = 0;//from ww  w  .  ja  v  a  2 s .c o  m
        mActivePointerId = ev.getPointerId(newPointerIndex);
    }
}