Example usage for android.support.v4.view MotionEventCompat getPointerId

List of usage examples for android.support.v4.view MotionEventCompat getPointerId

Introduction

In this page you can find the example usage for android.support.v4.view MotionEventCompat getPointerId.

Prototype

public static int getPointerId(MotionEvent event, int pointerIndex) 

Source Link

Document

Call MotionEvent#getPointerId(int) .

Usage

From source file:com.duy.pascal.ui.view.BaseRecycleView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent e) {
    final int action = MotionEventCompat.getActionMasked(e);
    final int actionIndex = MotionEventCompat.getActionIndex(e);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mScrollPointerId = MotionEventCompat.getPointerId(e, 0);
        mInitialTouchX = (int) (e.getX() + 0.5f);
        mInitialTouchY = (int) (e.getY() + 0.5f);
        return super.onInterceptTouchEvent(e);

    case MotionEventCompat.ACTION_POINTER_DOWN:
        mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex);
        mInitialTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f);
        mInitialTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f);
        return super.onInterceptTouchEvent(e);

    case MotionEvent.ACTION_MOVE: {
        final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId);
        if (index < 0) {
            return false;
        }//www.ja v  a 2 s  .  co  m

        final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f);
        final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f);
        if (getScrollState() != SCROLL_STATE_DRAGGING) {
            final int dx = x - mInitialTouchX;
            final int dy = y - mInitialTouchY;
            final boolean canScrollHorizontally = getLayoutManager().canScrollHorizontally();
            final boolean canScrollVertically = getLayoutManager().canScrollVertically();
            boolean startScroll = false;
            if (canScrollHorizontally && Math.abs(dx) > mTouchSlop
                    && (Math.abs(dx) >= Math.abs(dy) || canScrollVertically)) {
                startScroll = true;
            }
            if (canScrollVertically && Math.abs(dy) > mTouchSlop
                    && (Math.abs(dy) >= Math.abs(dx) || canScrollHorizontally)) {
                startScroll = true;
            }
            return startScroll && super.onInterceptTouchEvent(e);
        }
        return super.onInterceptTouchEvent(e);
    }

    default:
        return super.onInterceptTouchEvent(e);
    }
}

From source file:com.github.ppamorim.SlapBar.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        return false;
    }//from www. j a  v a 2s . co  m
    switch (MotionEventCompat.getActionMasked(ev)) {
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        dragHelper.cancel();
        return false;
    case MotionEvent.ACTION_DOWN:
        int index = MotionEventCompat.getActionIndex(ev);
        activePointerId = MotionEventCompat.getPointerId(ev, index);
        if (activePointerId == INVALID_POINTER) {
            return false;
        }
    default:
        return dragHelper.shouldInterceptTouchEvent(ev);
    }
}

From source file:com.github.ppamorim.SlapBar.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int actionMasked = MotionEventCompat.getActionMasked(ev);
    if ((actionMasked & MotionEventCompat.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
        activePointerId = MotionEventCompat.getPointerId(ev, actionMasked);
    }/*w w  w  .ja v  a  2  s.c o  m*/
    if (activePointerId == INVALID_POINTER) {
        return false;
    }
    dragHelper.processTouchEvent(ev);
    return isViewHit(dragView, (int) ev.getX(), (int) ev.getY());
}

From source file:com.greenkee.pokeADot.GameActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // tell system to use the layout defined in our XML file
    setContentView(com.greenkee.pokeADot.R.layout.activity_game_screen);

    mView = (GameView) findViewById(com.greenkee.pokeADot.R.id.gameScreen);
    mThread = mView.getThread();//w w  w  .  ja v  a 2  s. c om
    mView.setActivity(this);
    ((GameView) mView).setTextViews((TextView) findViewById(com.greenkee.pokeADot.R.id.status_display),
            (TextView) findViewById(com.greenkee.pokeADot.R.id.score_display),
            (TextView) findViewById(com.greenkee.pokeADot.R.id.combo_display));

    if (savedInstanceState == null) {
        System.out.println("STATE SET");
        mThread.doStart();
    } else {
        super.onRestoreInstanceState(savedInstanceState);
        mThread.restoreState(savedInstanceState);

    }
    mView.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = MotionEventCompat.getActionMasked(event);
            final int pointerIndex;
            final float x;
            final float y;
            switch (action) {
            case (MotionEvent.ACTION_DOWN): {
                //  System.out.println("ACTION_DOWN");
                if (((GameView.GameThread) mThread).getCurrentState() == GameView.GameThread.STATE_RUNNING) {
                    int index = MotionEventCompat.getActionIndex(event);
                    p1ID = MotionEventCompat.getPointerId(event, index);
                    if (p1ID != -1) {
                        int p1Index = MotionEventCompat.findPointerIndex(event, p1ID);
                        ((GameView.GameThread) mThread).checkTouch(MotionEventCompat.getX(event, p1Index),
                                MotionEventCompat.getY(event, p1Index));
                        //DO SOMETHING HERE

                    }
                } else if (!(mView.dialogOpen) && (!(((GameView.GameThread) mThread).gameOver))
                        && (((GameView.GameThread) mThread)
                                .getCurrentState() == GameView.GameThread.STATE_READY)
                        || (((GameView.GameThread) mThread)
                                .getCurrentState() == GameView.GameThread.STATE_LOSE)) {
                    ((GameView.GameThread) mThread).startGame();

                    // System.out.println("START GAME");

                } else if (((GameView.GameThread) mThread)
                        .getCurrentState() == GameView.GameThread.STATE_PAUSE) {
                    mThread.unpause();
                }

                return true;
            }
            case (MotionEvent.ACTION_MOVE): {
                //  System.out.println("ACTION_MOVE");
                if (((GameView.GameThread) mThread).getCurrentState() == GameView.GameThread.STATE_RUNNING) {
                    if (p1ID != -1) {
                        int p1Index = MotionEventCompat.findPointerIndex(event, p1ID);
                        //DO SOMETHING HERE

                    }
                }

                return true;
            }
            case (MotionEvent.ACTION_UP): {
                // System.out.println("ACTION_UP");

                reset();
                return true;
            }
            case (MotionEvent.ACTION_CANCEL): {
                // System.out.println("ACTION_CANCEL");
                reset();
                return true;
            }
            case (MotionEvent.ACTION_OUTSIDE): {
                return true;

            }
            }

            return false;
        }

        private void reset() {
            numTouches = 0;
            p1ID = -1;
            p2ID = -1;
        }
    }

    );
}

From source file:cn.goodjobs.common.view.photodraweeview.ScaleDragDetector.java

private void onTouchActivePointer(int action, MotionEvent ev) {
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = ev.getPointerId(0);
        break;/*from   www  .  ja  v  a  2s . c  om*/
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        mActivePointerId = INVALID_POINTER_ID;
        break;
    case MotionEvent.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = (pointerIndex == 0) ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
            mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
            mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
        }

        break;
    }

    mActivePointerIndex = MotionEventCompat.findPointerIndex(ev,
            mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0);
}

From source file:com.github.ppamorim.library.DraggerView.java

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if (!isEnabled()) {
        return false;
    }/*from ww w  .j a  va 2 s.c  o  m*/
    final int action = MotionEventCompat.getActionMasked(ev);
    switch (action) {
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        dragHelper.cancel();
        return false;
    case MotionEvent.ACTION_DOWN:
        int index = MotionEventCompat.getActionIndex(ev);
        activePointerId = MotionEventCompat.getPointerId(ev, index);
        if (activePointerId == INVALID_POINTER) {
            return false;
        }
    default:
        return dragHelper.shouldInterceptTouchEvent(ev);
    }
}

From source file:com.kuloud.android.unity.ui.widget.steprefresh.PullToRefreshView.java

@Override
public boolean onInterceptTouchEvent(@NonNull MotionEvent ev) {

    if (!isEnabled() || canChildScrollUp() || mRefreshing) {
        return false;
    }/*from w w  w .  java  2s.c o m*/

    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        setTargetOffsetTop(0);
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mIsBeingDragged = false;
        final float initialMotionY = getMotionEventY(ev, mActivePointerId);
        if (initialMotionY == -1) {
            return false;
        }
        mInitialMotionY = initialMotionY;
        break;
    case MotionEvent.ACTION_MOVE:
        if (mActivePointerId == INVALID_POINTER) {
            return false;
        }
        final float y = getMotionEventY(ev, mActivePointerId);
        if (y == -1) {
            return false;
        }
        final float yDiff = y - mInitialMotionY;
        if (yDiff > mTouchSlop && !mIsBeingDragged) {
            mIsBeingDragged = true;
        }
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mIsBeingDragged = false;
        mActivePointerId = INVALID_POINTER;
        break;
    case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
    }

    return mIsBeingDragged;
}

From source file:com.github.ppamorim.library.DraggerView.java

@Override
public boolean onTouchEvent(MotionEvent ev) {
    int actionMasked = MotionEventCompat.getActionMasked(ev);
    if ((actionMasked & MotionEventCompat.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
        activePointerId = MotionEventCompat.getPointerId(ev, actionMasked);
    }/*from w w w. j a  v a 2s.  c  o m*/
    if (activePointerId == INVALID_POINTER) {
        return false;
    }
    dragHelper.processTouchEvent(ev);
    boolean isDragViewHit = isViewHit(dragView, (int) ev.getX(), (int) ev.getY());
    boolean isSecondViewHit = isViewHit(shadowView, (int) ev.getX(), (int) ev.getY());
    return isDragViewHit || isSecondViewHit;
}

From source file:com.android.yijiang.kzx.widget.betterpickers.widget.UnderlinePageIndicatorPicker.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }/*from   w w  w  .  j av  a 2  s. co  m*/
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            final int count = mViewPager.getAdapter().getCount();
            final int width = getWidth();
            final float halfWidth = width / 2f;
            final float sixthWidth = width / 6f;

            if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage - 1);
                }
                return true;
            } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
                if (action != MotionEvent.ACTION_CANCEL) {
                    mViewPager.setCurrentItem(mCurrentPage + 1);
                }
                return true;
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging()) {
            mViewPager.endFakeDrag();
        }
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}

From source file:com.lee.sdk.widget.viewpager.DrawablePageIndicator.java

public boolean onTouchEvent(MotionEvent ev) {
    if (super.onTouchEvent(ev)) {
        return true;
    }//w w w .  j a v  a 2s .  c o  m
    if ((mViewPager == null) || (mViewPager.getAdapter().getCount() == 0)) {
        return false;
    }

    final int action = ev.getAction() & MotionEventCompat.ACTION_MASK;
    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
        mLastMotionX = ev.getX();
        break;

    case MotionEvent.ACTION_MOVE: {
        final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
        final float x = MotionEventCompat.getX(ev, activePointerIndex);
        final float deltaX = x - mLastMotionX;

        if (!mIsDragging) {
            if (Math.abs(deltaX) > mTouchSlop) {
                mIsDragging = true;
            }
        }

        if (mIsDragging) {
            mLastMotionX = x;
            if (mViewPager.isFakeDragging() || mViewPager.beginFakeDrag()) {
                mViewPager.fakeDragBy(deltaX);
            }
        }

        break;
    }

    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        if (!mIsDragging) {
            //                    final int count = mViewPager.getAdapter().getCount();
            //                    final int width = getWidth();
            //                    final float halfWidth = width / 2f;
            //                    final float sixthWidth = width / 6f;
            //
            //                    if ((mCurrentPage > 0) && (ev.getX() < halfWidth - sixthWidth)) {
            //                        if (action != MotionEvent.ACTION_CANCEL) {
            //                            mViewPager.setCurrentItem(mCurrentPage - 1);
            //                        }
            //                        return true;
            //                    } else if ((mCurrentPage < count - 1) && (ev.getX() > halfWidth + sixthWidth)) {
            //                        if (action != MotionEvent.ACTION_CANCEL) {
            //                            mViewPager.setCurrentItem(mCurrentPage + 1);
            //                        }
            //                        return true;
            //                    }
            if (action != MotionEvent.ACTION_CANCEL) {
                final int count = mViewPager.getAdapter().getCount();
                final int width = getWidth();
                final int pageWidth = width / count;
                int downPage = (int) (ev.getX() / pageWidth);
                if (downPage != mCurrentPage) {
                    mViewPager.setCurrentItem(downPage);
                    return true;
                }
            }
        }

        mIsDragging = false;
        mActivePointerId = INVALID_POINTER;
        if (mViewPager.isFakeDragging())
            mViewPager.endFakeDrag();
        break;

    case MotionEventCompat.ACTION_POINTER_DOWN: {
        final int index = MotionEventCompat.getActionIndex(ev);
        mLastMotionX = MotionEventCompat.getX(ev, index);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
    }

    case MotionEventCompat.ACTION_POINTER_UP:
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
        if (pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
        mLastMotionX = MotionEventCompat.getX(ev, MotionEventCompat.findPointerIndex(ev, mActivePointerId));
        break;
    }

    return true;
}