Example usage for android.view MotionEvent ACTION_POINTER_UP

List of usage examples for android.view MotionEvent ACTION_POINTER_UP

Introduction

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

Prototype

int ACTION_POINTER_UP

To view the source code for android.view MotionEvent ACTION_POINTER_UP.

Click Source Link

Document

Constant for #getActionMasked : A non-primary pointer has gone up.

Usage

From source file:com.example.customview.DynamicListView.java

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mDownX = (int) event.getX();
        mDownY = (int) event.getY();
        mActivePointerId = event.getPointerId(0);
        break;//from   w w  w .j  ava  2s . c om
    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER_ID) {
            break;
        }
        int pointerIndex = event.findPointerIndex(mActivePointerId);
        mLastEventY = (int) event.getY(pointerIndex);
        int deltaY = mLastEventY - mDownY;

        if (mCellIsMobile) {
            mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left,
                    mHoverCellOriginalBounds.top + deltaY + mTotalOffset);
            mHoverCell.setBounds(mHoverCellCurrentBounds);
            invalidate();

            handleCellSwitch();

            mIsMobileScrolling = false;
            handleMobileCellScroll();

            return false;
        }
        break;
    case MotionEvent.ACTION_UP:
        touchEventsEnded();
        drawLayoutSetUnlock();
        break;
    case MotionEvent.ACTION_CANCEL:
        touchEventsCancelled();
        break;
    case MotionEvent.ACTION_POINTER_UP:
        /* If a multitouch event took place and the original touch dictating
         * the movement of the hover cell has ended, then the dragging event
         * ends and the hover cell is animated to its corresponding position
         * in the listview. */
        pointerIndex = (event.getAction()
                & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            touchEventsEnded();
        }
        drawLayoutSetUnlock();
        break;
    default:
        break;
    }

    return super.onTouchEvent(event);
}

From source file:com.gitstudy.rili.liarbry.CalendarLayout.java

@SuppressLint("ClickableViewAccessibility")
@Override/*w  w w  . j a v a  2  s  .c o  m*/
public boolean onTouchEvent(MotionEvent event) {

    if (mDelegate.isShowYearSelectedLayout) {
        return false;
    }

    if (mContentView == null) {
        return false;
    }

    int action = event.getAction();
    float y = event.getY();
    mVelocityTracker.addMovement(event);
    switch (action) {
    case MotionEvent.ACTION_DOWN:

        int index = MotionEventCompat.getActionIndex(event);
        mActivePointerId = MotionEventCompat.getPointerId(event, index);
        mLastY = downY = y;
        return true;
    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int indexx = MotionEventCompat.getActionIndex(event);
        mActivePointerId = MotionEventCompat.getPointerId(event, indexx);
        if (mActivePointerId == 0) {
            //?? dy = y- mLastY == 0??
            mLastY = MotionEventCompat.getY(event, mActivePointerId);
        }
        break;
    }
    case MotionEvent.ACTION_MOVE:
        if (mGestureMode == GESTURE_MODE_DISABLED || mCalendarShowMode == CALENDAR_SHOW_MODE_ONLY_MONTH_VIEW
                || mCalendarShowMode == CALENDAR_SHOW_MODE_ONLY_WEEK_VIEW) {//????
            return false;
        }

        getPointerIndex(event, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER) {
            //?mLastY?y????? dy== 0??
            mLastY = y;
            mActivePointerId = ACTIVE_POINTER;
        }

        float dy = y - mLastY;
        //?contentView?
        if (dy < 0 && mContentView.getTranslationY() == -mContentViewTranslateY) {
            //mContentView.onTouchEvent(event);
            showWeek();
            mLastY = y;
            return false;
        }
        hideWeek();

        //?contentView?
        if (dy > 0 && mContentView.getTranslationY() + dy >= 0) {
            mContentView.setTranslationY(0);
            translationViewPager();
            mLastY = y;
            return super.onTouchEvent(event);
        }

        //?contentView??contentView?
        if (dy < 0 && mContentView.getTranslationY() + dy <= -mContentViewTranslateY) {
            mContentView.setTranslationY(-mContentViewTranslateY);
            translationViewPager();
            mLastY = y;
            return super.onTouchEvent(event);
        }
        //?
        mContentView.setTranslationY(mContentView.getTranslationY() + dy);
        translationViewPager();
        mLastY = y;
        break;
    case MotionEvent.ACTION_CANCEL:

    case MotionEvent.ACTION_POINTER_UP:
        int pointerIndex = getPointerIndex(event, mActivePointerId);
        if (mActivePointerId == INVALID_POINTER)
            break;
        mLastY = MotionEventCompat.getY(event, pointerIndex);
        break;
    case MotionEvent.ACTION_UP:

        final VelocityTracker velocityTracker = mVelocityTracker;
        velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        float mYVelocity = velocityTracker.getYVelocity();
        if (mContentView.getTranslationY() == 0 || mContentView.getTranslationY() == mContentViewTranslateY) {
            break;
        }
        if (Math.abs(mYVelocity) >= 800) {
            if (mYVelocity < 0) {
                shrink();
            } else {
                expand();
            }
            return super.onTouchEvent(event);
        }
        if (event.getY() - downY > 0) {
            expand();
        } else {
            shrink();
        }
        break;
    }
    return super.onTouchEvent(event);
}

From source file:study.com.s_sxl.carelib.pullRefreshView.layout.FlingLayout.java

/******************************************************************/

@Override//from   w  w  w  .  j a  v a2 s. c o  m
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mPullView != null && !ViewCompat.isNestedScrollingEnabled(mPullView)) {
        float moveY = getMoveY();
        int pointerCount = ev.getPointerCount();
        int pointerIndex = ev.getActionIndex();
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            float x = ev.getX(pointerIndex);
            float y = ev.getY(pointerIndex);
            tempY = downY = y;
            tempX = downX = x;
            tempStateType = SCROLL_STATE_TOUCH_SCROLL;
            if (moveY != 0) {
                return true;
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            tempX = ev.getX(pointerIndex);
            tempY = ev.getY(pointerIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            pointerIndex = ev.findPointerIndex(mPointerId);
            float mx;
            float my;
            if (pointerCount > pointerIndex && pointerIndex >= 0) {
                mx = ev.getX(pointerIndex);
                my = ev.getY(pointerIndex);
            } else {
                mx = ev.getX();
                my = ev.getY();
            }
            //?????
            int dataX = (int) (mx - tempX);
            int dataY = (int) (my - tempY);
            tempX = mx;
            tempY = my;
            if (isScrolling || (Math.abs(dataY) > Math.abs(dataX))) {
                isScrolling = true;
                if (moveY == 0) {
                    // 0,0
                    //??
                    if ((dataY < 0 && canPullUp()) || (dataY > 0 && canPullDown())) {
                        moveBy(dataY);
                        return true;
                    }
                } else {
                    //?0,0
                    ev.setAction(MotionEvent.ACTION_CANCEL);//?

                    if ((moveY < 0 && moveY + dataY >= 0) || (moveY > 0 && moveY + dataY <= 0)) {
                        //0,0
                        ev.setAction(MotionEvent.ACTION_DOWN);
                        moveTo(0);
                    } else if ((moveY > 0 && dataY > 0) || (moveY < 0 && dataY < 0)) {
                        //??
                        if (maxDistance == 0 || Math.abs(moveY) < maxDistance) {
                            int ps = 0;
                            int hDataY = dataY / 2;
                            if (maxDistance == 0) {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) MAXDISTANCE) - hDataY;
                            } else {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) maxDistance) - hDataY;
                            }
                            moveBy(ps + dataY);
                        } else if (moveY > maxDistance) {
                            moveTo(maxDistance);
                        } else if (moveY < -maxDistance) {
                            moveTo(-maxDistance);
                        }
                    } else {
                        moveBy(dataY);
                    }
                }
            } else {
                ev.setLocation(mx, downY);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            startFling();
            isScrolling = false;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // ??
            int pointerIdLeave = ev.getPointerId(pointerIndex);
            if (mPointerId == pointerIdLeave) {
                // ??????VelocityTracker
                int reIndex = pointerIndex == 0 ? 1 : 0;
                mPointerId = ev.getPointerId(reIndex);
                // ?
                tempY = ev.getY(reIndex);
            }
        }
        return super.dispatchTouchEvent(ev) || isScrolling;
    } else {
        return super.dispatchTouchEvent(ev);
    }

}

From source file:com.leap.mini.widget.pullrefresh.base.layout.FlingLayout.java

/******************************************************************/

@Override//from w w w.j  a  va 2s .c  o  m
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mPullView != null && !ViewCompat.isNestedScrollingEnabled(mPullView)) {
        float moveY = getMoveY();
        int pointerCount = ev.getPointerCount();
        int pointerIndex = ev.getActionIndex();
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            float x = ev.getX(pointerIndex);
            float y = ev.getY(pointerIndex);
            tepmY = downY = y;
            tepmX = downX = x;
            tempStateType = SCROLL_STATE_TOUCH_SCROLL;
            if (moveY != 0) {
                return true;
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            tepmX = ev.getX(pointerIndex);
            tepmY = ev.getY(pointerIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            pointerIndex = ev.findPointerIndex(mPointerId);
            float mx;
            float my;
            if (pointerCount > pointerIndex && pointerIndex >= 0) {
                mx = ev.getX(pointerIndex);
                my = ev.getY(pointerIndex);
            } else {
                mx = ev.getX();
                my = ev.getY();
            }
            // ?????
            int dataX = (int) (mx - tepmX);
            int dataY = (int) (my - tepmY);
            tepmX = mx;
            tepmY = my;
            if (isScrolling || (Math.abs(dataY) > Math.abs(dataX))) {
                isScrolling = true;
                if (moveY == 0) {
                    //  0,0
                    // ??
                    if ((dataY < 0 && canPullUp()) || (dataY > 0 && canPullDown())) {
                        moveBy(dataY);
                        return true;
                    }
                } else {
                    // ?0,0
                    ev.setAction(MotionEvent.ACTION_CANCEL);// ?

                    if ((moveY < 0 && moveY + dataY >= 0) || (moveY > 0 && moveY + dataY <= 0)) {
                        // 0,0
                        ev.setAction(MotionEvent.ACTION_DOWN);
                        moveTo(0);
                    } else if ((moveY > 0 && dataY > 0) || (moveY < 0 && dataY < 0)) {
                        // ??
                        if (maxDistance == 0 || Math.abs(moveY) < maxDistance) {
                            int ps = 0;
                            int hDataY = dataY / 2;
                            if (maxDistance == 0) {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) MAXDISTANCE) - hDataY;
                            } else {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) maxDistance) - hDataY;
                            }
                            moveBy(ps + dataY);
                        } else if (moveY > maxDistance) {
                            moveTo(maxDistance);
                        } else if (moveY < -maxDistance) {
                            moveTo(-maxDistance);
                        }
                    } else {
                        moveBy(dataY);
                    }
                }
            } else {
                ev.setLocation(mx, downY);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            startFling();
            isScrolling = false;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // ??
            int pointerIdLeave = ev.getPointerId(pointerIndex);
            if (mPointerId == pointerIdLeave) {
                // ??????VelocityTracker
                int reIndex = pointerIndex == 0 ? 1 : 0;
                mPointerId = ev.getPointerId(reIndex);
                // ?
                tepmY = ev.getY(reIndex);
            }
        }
        return super.dispatchTouchEvent(ev) || isScrolling;
    } else {
        return super.dispatchTouchEvent(ev);
    }

}

From source file:com.pseudosurface.levels.template.SimulatorActivity.java

@Override
public boolean onTouchEvent(MotionEvent event) {

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

    // Remember some things for zooming
    PointF start = new PointF();
    PointF mid = new PointF();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        start.set(event.getX(), event.getY());
        //          Log.d(TAG, "mode=DRAG");

        break;/*from   w  ww .  j  av a 2  s  .  co  m*/
    case MotionEvent.ACTION_POINTER_DOWN:
        oldDist = spacing(event);
        if (oldDist > 10f) {
            midPoint(mid, event);
            mode = ZOOM;
            //             Log.d(TAG, "mode=ZOOM");
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        mode = NONE;
        oldDist = -1;
        //          Log.d(TAG, "mode=NONE");
        break;
    case MotionEvent.ACTION_MOVE:

        if (mode == ZOOM) {
            float newDist = spacing(event);
            if (newDist > simulation.minZoom && oldDist != -1) {
                simulation.zoom -= (newDist - oldDist) * .05;
            }
            oldDist = newDist;
        } else {
            float dz = x - mPreviousX;
            float dy = y - mPreviousY;

            float r = (float) Math.sqrt(dz * dz + dy * dy);

            double theta = Math.atan2(dy, dz) + Math.PI + SimulatorActivity.angleLR;

            try {
                simulation.world.objects[simulation.world.getPlayerIndex()].omega[0] += .05f * r
                        * Math.cos(theta);
                simulation.world.objects[simulation.world.getPlayerIndex()].omega[2] += .05f * r
                        * Math.sin(theta);
            } catch (Exception e) {
            }

            //MainActivity.angleLR += dx*.01;
            //simulation.zoom += dy*.01;
        }
        break;
    }

    mPreviousX = x;
    mPreviousY = y;

    gestureDetector.onTouchEvent(event);

    return true; // indicate event was handled
}

From source file:com.ybao.pullrefreshview.layout.FlingLayout.java

/******************************************************************/

@Override//from  w  w w. j  av a2s  .c  om
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mPullView != null && !ViewCompat.isNestedScrollingEnabled(mPullView)) {
        float moveY = getMoveY();
        int pointerCount = ev.getPointerCount();
        int pointerIndex = ev.getActionIndex();
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            float x = ev.getX(pointerIndex);
            float y = ev.getY(pointerIndex);
            tepmY = downY = y;
            tepmX = downX = x;
            tempStateType = SCROLL_STATE_TOUCH_SCROLL;
            if (moveY != 0) {
                return true;
            }
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            tepmX = ev.getX(pointerIndex);
            tepmY = ev.getY(pointerIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            pointerIndex = ev.findPointerIndex(mPointerId);
            float mx;
            float my;
            if (pointerCount > pointerIndex && pointerIndex >= 0) {
                mx = ev.getX(pointerIndex);
                my = ev.getY(pointerIndex);
            } else {
                mx = ev.getX();
                my = ev.getY();
            }
            //?????
            int dataX = (int) (mx - tepmX);
            int dataY = (int) (my - tepmY);
            tepmX = mx;
            tepmY = my;
            if (isScrolling || (Math.abs(dataY) > Math.abs(dataX))) {
                isScrolling = true;
                if (moveY == 0) {
                    // 0,0
                    //??
                    if ((dataY < 0 && canPullUp()) || (dataY > 0 && canPullDown())) {
                        moveBy(dataY);
                        return true;
                    }
                } else {
                    //?0,0
                    ev.setAction(MotionEvent.ACTION_CANCEL);//?

                    if ((moveY < 0 && moveY + dataY >= 0) || (moveY > 0 && moveY + dataY <= 0)) {
                        //0,0
                        ev.setAction(MotionEvent.ACTION_DOWN);
                        moveTo(0);
                    } else if ((moveY > 0 && dataY > 0) || (moveY < 0 && dataY < 0)) {
                        //??
                        if (maxDistance == 0 || Math.abs(moveY) < maxDistance) {
                            int ps = 0;
                            int hDataY = dataY / 2;
                            if (maxDistance == 0) {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) MAXDISTANCE) - hDataY;
                            } else {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) maxDistance) - hDataY;
                            }
                            moveBy(ps + dataY);
                        } else if (moveY > maxDistance) {
                            moveTo(maxDistance);
                        } else if (moveY < -maxDistance) {
                            moveTo(-maxDistance);
                        }
                    } else {
                        moveBy(dataY);
                    }
                }
            } else {
                ev.setLocation(mx, downY);
            }
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            startFling();
            isScrolling = false;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // ??
            int pointerIdLeave = ev.getPointerId(pointerIndex);
            if (mPointerId == pointerIdLeave) {
                // ??????VelocityTracker
                int reIndex = pointerIndex == 0 ? 1 : 0;
                mPointerId = ev.getPointerId(reIndex);
                // ?
                tepmY = ev.getY(reIndex);
            }
        }
        return super.dispatchTouchEvent(ev) || isScrolling;
    } else {
        return super.dispatchTouchEvent(ev);
    }

}

From source file:com.spatialnetworks.fulcrum.widget.DynamicListView.java

@Override
public boolean onTouchEvent(@NonNull MotionEvent event) {

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mDownX = (int) event.getX();
        mDownY = (int) event.getY();
        mActivePointerId = event.getPointerId(0);

        /*/* w ww.ja  v  a  2  s .co m*/
         * get the touch position. if it's valid and drag and drop is enabled, then get the view
         * and determine if the touch was on the drag and drop view. if so, set up for dragging
         */
        int position = pointToPosition((int) event.getX(), (int) event.getY());
        if (position != INVALID_POSITION && mDragAndDropEnabled) {
            View downView = getChildAt(position - getFirstVisiblePosition());
            assert downView != null;
            if (onDraggable(downView, event.getX() - downView.getX(), event.getY() - downView.getY())) {
                setUpDrag();
            }
        }

        break;
    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER_ID) {
            break;
        }

        int pointerIndex = event.findPointerIndex(mActivePointerId);

        mLastEventY = (int) event.getY(pointerIndex);
        int deltaY = mLastEventY - mDownY;

        if (mCellIsMobile) {
            mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left,
                    mHoverCellOriginalBounds.top + deltaY + mTotalOffset);
            mHoverCell.setBounds(mHoverCellCurrentBounds);
            invalidate();

            handleCellSwitch();

            mIsMobileScrolling = false;
            handleMobileCellScroll();

            return false;
        }
        break;
    case MotionEvent.ACTION_UP:
        touchEventsEnded();
        break;
    case MotionEvent.ACTION_CANCEL:
        touchEventsCancelled();
        break;
    case MotionEvent.ACTION_POINTER_UP:
        /* If a multitouch event took place and the original touch dictating
         * the movement of the hover cell has ended, then the dragging event
         * ends and the hover cell is animated to its corresponding position
         * in the listview. */
        pointerIndex = (event.getAction()
                & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            touchEventsEnded();
        }
        break;
    default:
        break;
    }

    return super.onTouchEvent(event);
}

From source file:ucsc.hci.rankit.DynamicListView.java

@Override
public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mDownX = (int) event.getX();
        mDownY = (int) event.getY();
        mActivePointerId = event.getPointerId(0);

        // This part has been copied from OnItemLongClickListener code above (which has now
        // been commented out).
        mTotalOffset = 0;/*from  www. j a  v  a2 s  . c  o m*/

        int position = pointToPosition(mDownX, mDownY);
        int itemNum = position - getFirstVisiblePosition();

        View selectedView = getChildAt(itemNum);
        mMobileItemId = getAdapter().getItemId(position);
        mHoverCell = getAndAddHoverView(selectedView);
        selectedView.setVisibility(INVISIBLE);

        mCellIsMobile = true;

        updateNeighborViewsForID(mMobileItemId);
        // End of copy-paste from OnItemLongClickListener

        break;
    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER_ID) {
            break;
        }

        int pointerIndex = event.findPointerIndex(mActivePointerId);

        mLastEventY = (int) event.getY(pointerIndex);
        int deltaY = mLastEventY - mDownY;

        if (mCellIsMobile) {
            mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left,
                    mHoverCellOriginalBounds.top + deltaY + mTotalOffset);
            try {
                mHoverCell.setBounds(mHoverCellCurrentBounds);
            } catch (Exception e) {
                e.printStackTrace();
            }
            invalidate();

            handleCellSwitch();

            mIsMobileScrolling = false;
            handleMobileCellScroll();

            return false;
        }
        break;
    case MotionEvent.ACTION_UP:
        touchEventsEnded();
        break;
    case MotionEvent.ACTION_CANCEL:
        touchEventsCancelled();
        break;
    case MotionEvent.ACTION_POINTER_UP:
        /* If a multitouch event took place and the original touch dictating
         * the movement of the hover cell has ended, then the dragging event
         * ends and the hover cell is animated to its corresponding position
         * in the listview. */
        pointerIndex = (event.getAction()
                & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
        final int pointerId = event.getPointerId(pointerIndex);
        if (pointerId == mActivePointerId) {
            touchEventsEnded();
        }
        break;
    default:
        break;
    }

    return super.onTouchEvent(event);
}

From source file:com.slushpupie.deskclock.DeskClock.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (!supportMultiTouch)
        return false;

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mode = DRAG;//w w  w . j a v  a 2 s .co  m
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        oldDist = spacing(event);
        if (oldDist > 10f) {
            mode = ZOOM;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        mode = NONE;
        break;
    case MotionEvent.ACTION_MOVE:
        if (mode == DRAG) {
            // ..
        } else if (mode == ZOOM) {
            float newDist = spacing(event);
            if (newDist > 10f) {
                float scaleF = newDist / oldDist;
                int scale = (int) (scaleF * 100.0);
                if (scale < 0)
                    scale = scale * -1;
                if (scale > 100)
                    scale = 100;

                // change font size
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
                prefs.edit().putInt("pref_scale", scale).commit();
            }
        }
        break;
    }
    if (mode == ZOOM) {
        return true;
    } else {
        return false;
    }
}

From source file:com.cocarechina.pullrefreshview.layout.FlingLayout.java

/******************************************************************/

@Override//from ww w.  java 2 s.c om
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (mPullView != null && !ViewCompat.isNestedScrollingEnabled(mPullView)) {
        float moveY = getMoveY();
        int pointerCount = ev.getPointerCount();
        int pointerIndex = ev.getActionIndex();
        if (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        switch (ev.getActionMasked()) {
        case MotionEvent.ACTION_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            float x = ev.getX(pointerIndex);
            float y = ev.getY(pointerIndex);
            tepmY = downY = y;
            tepmX = downX = x;
            tempStateType = SCROLL_STATE_TOUCH_SCROLL;
            if (moveY != 0) {
                return true;
            }
            lastY = ev.getY();
            Log.v("lastY", moveY + "");
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            mPointerId = ev.getPointerId(pointerIndex);
            tepmX = ev.getX(pointerIndex);
            tepmY = ev.getY(pointerIndex);
            break;
        case MotionEvent.ACTION_MOVE:
            pointerIndex = ev.findPointerIndex(mPointerId);
            float mx;
            float my;
            if (pointerCount > pointerIndex && pointerIndex >= 0) {
                mx = ev.getX(pointerIndex);
                my = ev.getY(pointerIndex);
            } else {
                mx = ev.getX();
                my = ev.getY();
            }
            //?????
            int dataX = (int) (mx - tepmX);
            int dataY = (int) (my - tepmY);
            tepmX = mx;
            tepmY = my;
            if (isScrolling || (Math.abs(dataY) > Math.abs(dataX))) {
                isScrolling = true;
                if (moveY == 0) {
                    // 0,0
                    //??
                    if ((dataY < 0 && canPullUp()) || (dataY > 0 && canPullDown())) {
                        moveBy(dataY);
                        return true;
                    }
                } else {
                    //?0,0
                    ev.setAction(MotionEvent.ACTION_CANCEL);//?

                    if ((moveY < 0 && moveY + dataY >= 0) || (moveY > 0 && moveY + dataY <= 0)) {
                        //0,0
                        ev.setAction(MotionEvent.ACTION_DOWN);
                        moveTo(0);
                    } else if ((moveY > 0 && dataY > 0) || (moveY < 0 && dataY < 0)) {
                        //??
                        if (maxDistance == 0 || Math.abs(moveY) < maxDistance) {
                            int ps = 0;
                            int hDataY = dataY / 2;
                            if (maxDistance == 0) {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) MAXDISTANCE) - hDataY;
                            } else {
                                ps = (int) (-hDataY * Math.abs(moveY) / (float) maxDistance) - hDataY;
                            }
                            moveBy(ps + dataY);
                        } else if (moveY > maxDistance) {

                            moveTo(maxDistance);
                        } else if (moveY < -maxDistance) {
                            moveTo(-maxDistance);
                        }
                    } else {
                        moveBy(dataY);
                    }
                }
            } else {
                ev.setLocation(mx, downY);
            }

            Log.v("flingLayout", "ev.getY()" + ev.getY() + "  ---  lastY " + lastY);
            if (ev.getY() - lastY > 5) {
                if (monScrollHeadListener != null) {
                    monScrollHeadListener.onScrollBottom();
                }
            } else if (ev.getY() - lastY < -10) {
                if (monScrollHeadListener != null) {
                    monScrollHeadListener.onScrollTop();
                }
            }

            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            startFling();
            isScrolling = false;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // ??
            int pointerIdLeave = ev.getPointerId(pointerIndex);
            if (mPointerId == pointerIdLeave) {
                // ??????VelocityTracker
                int reIndex = pointerIndex == 0 ? 1 : 0;
                mPointerId = ev.getPointerId(reIndex);
                // ?
                tepmY = ev.getY(reIndex);
            }
        }
        return super.dispatchTouchEvent(ev) || isScrolling;
    } else {
        return super.dispatchTouchEvent(ev);
    }

}