Java Code Examples for android.view.MotionEvent#findPointerIndex()

The following examples show how to use android.view.MotionEvent#findPointerIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JoystickView.java    From Multiwii-Remote with Apache License 2.0 6 votes vote down vote up
private boolean processMoveEvent(MotionEvent ev) {
	if (pointerId != INVALID_POINTER_ID) {
		final int pointerIndex = ev.findPointerIndex(pointerId);
		// Translate touch position to center of view
		float x = ev.getX(pointerIndex);
		touchX = x - cX - offsetX;
		float y = ev.getY(pointerIndex);
		touchY = y - cY - offsetY;

		// Log.d(TAG,
		// String.format("ACTION_MOVE: (%03.0f, %03.0f) => (%03.0f, %03.0f)",
		// x, y, touchX, touchY));

		reportOnMoved();
		invalidate();

		touchPressure = ev.getPressure(pointerIndex);
		reportOnPressure();

		return true;
	}
	return false;
}
 
Example 2
Source File: ZrcAbsListView.java    From AndroidStudyDemo with GNU General Public License v2.0 6 votes vote down vote up
private void onTouchMove(MotionEvent ev) {
    if (mTouchMode == TOUCH_MODE_INVALID) {
        mTouchMode = TOUCH_MODE_SCROLL;
    }
    int pointerIndex = ev.findPointerIndex(mActivePointerId);
    if (pointerIndex == -1) {
        pointerIndex = 0;
        mActivePointerId = ev.getPointerId(pointerIndex);
    }
    if (mDataChanged) {
        layoutChildren();
    }
    final int x = (int) ev.getX(pointerIndex);
    final int y = (int) ev.getY(pointerIndex);
    switch (mTouchMode) {
    case TOUCH_MODE_DOWN:
    case TOUCH_MODE_TAP:
    case TOUCH_MODE_DONE_WAITING:
        startScrollIfNeeded(x, y);
        break;
    case TOUCH_MODE_SCROLL:
        scrollIfNeeded(x, y);
        break;
    }
}
 
Example 3
Source File: ItemTouchHelper.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void onLongPress(MotionEvent e) {
    if (!mShouldReactToLongPress) {
        return;
    }
    View child = findChildView(e);
    if (child != null) {
        ViewHolder vh = mRecyclerView.getChildViewHolder(child);
        if (vh != null) {
            if (!mCallback.hasDragFlag(mRecyclerView, vh)) {
                return;
            }
            int pointerId = e.getPointerId(0);
            // Long press is deferred.
            // Check w/ active pointer id to avoid selecting after motion
            // event is canceled.
            if (pointerId == mActivePointerId) {
                final int index = e.findPointerIndex(mActivePointerId);
                final float x = e.getX(index);
                final float y = e.getY(index);
                mInitialTouchX = x;
                mInitialTouchY = y;
                mDx = mDy = 0f;
                if (DEBUG) {
                    Log.d(TAG,
                            "onlong press: x:" + mInitialTouchX + ",y:" + mInitialTouchY);
                }
                if (mCallback.isLongPressDragEnabled()) {
                    select(vh, ACTION_STATE_DRAG);
                }
            }
        }
    }
}
 
Example 4
Source File: TypewriterRefreshLayout.java    From Typewriter with MIT License 5 votes vote down vote up
private float getMotionEventY(MotionEvent motionEvent, int activePointerId) {
    final int index = motionEvent.findPointerIndex(activePointerId);
    if (index < 0) {
        return -1f;
    }
    return motionEvent.getY(index);
}
 
Example 5
Source File: ItemTouchHelper.java    From Carbon with Apache License 2.0 5 votes vote down vote up
@Override
public void onLongPress(MotionEvent e) {
    if (!mShouldReactToLongPress) {
        return;
    }
    View child = findChildView(e);
    if (child != null) {
        ViewHolder vh = mRecyclerView.getChildViewHolder(child);
        if (vh != null) {
            if (!mCallback.hasDragFlag(mRecyclerView, vh)) {
                return;
            }
            int pointerId = e.getPointerId(0);
            // Long press is deferred.
            // Check w/ active pointer id to avoid selecting after motion
            // event is canceled.
            if (pointerId == mActivePointerId) {
                final int index = e.findPointerIndex(mActivePointerId);
                final float x = e.getX(index);
                final float y = e.getY(index);
                mInitialTouchX = x;
                mInitialTouchY = y;
                mDx = mDy = 0f;
                if (DEBUG) {
                    Log.d(TAG,
                            "onlong press: x:" + mInitialTouchX + ",y:" + mInitialTouchY);
                }
                if (mCallback.isLongPressDragEnabled()) {
                    select(vh, ACTION_STATE_DRAG);
                }
            }
        }
    }
}
 
Example 6
Source File: PictureInPictureGestureHelper.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private boolean onGestureFinished(MotionEvent e) {
  final int pointerIndex = e.findPointerIndex(activePointerId);

  if (e.getActionIndex() == pointerIndex) {
    onFling(e, e, 0, 0);
    return true;
  }

  return false;
}
 
Example 7
Source File: EclairGestureDetector.java    From AndroidPickPhotoDialog with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = ev.getPointerId(0);
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            mActivePointerId = INVALID_POINTER_ID;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // Ignore deprecation, ACTION_POINTER_ID_MASK and
            // ACTION_POINTER_ID_SHIFT has same value and are deprecated
            // You can have either deprecation or lint target api warning
            final int pointerIndex = Compat.getPointerIndex(ev.getAction());
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = ev.getPointerId(newPointerIndex);
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
            }
            break;
    }

    mActivePointerIndex = ev
            .findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId
                    : 0);
    try {
        return super.onTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        // Fix for support lib bug, happening when onDestroy is
        return true;
    }
}
 
Example 8
Source File: EclairGestureDetector.java    From ImagePicker with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev)
{
    final int action = ev.getAction();
    switch (action & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_DOWN:
            mActivePointerId = ev.getPointerId(0);
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            mActivePointerId = INVALID_POINTER_ID;
            break;
        case MotionEvent.ACTION_POINTER_UP:
            // Ignore deprecation, ACTION_POINTER_ID_MASK and
            // ACTION_POINTER_ID_SHIFT has same value and are deprecated
            // You can have either deprecation or lint target api warning
            final int pointerIndex = CropCompat.getPointerIndex(ev.getAction());
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId)
            {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = ev.getPointerId(newPointerIndex);
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
            }
            break;
    }

    mActivePointerIndex = ev.findPointerIndex(mActivePointerId != INVALID_POINTER_ID ? mActivePointerId : 0);
    return super.onTouchEvent(ev);
}
 
Example 9
Source File: SwipeRefreshLayout.java    From HaoReader with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent ev) {
    int action = ev.getActionMasked();
    if (!this.mRefreshing && this.mReturningToStart && action == 0) {
        this.mReturningToStart = false;
    }

    if (this.isEnabled() && !this.mReturningToStart && !this.canChildScrollUp()) {
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                this.mLastMotionY = this.mInitialMotionY = ev.getY();
                this.mActivePointerId = ev.getPointerId(0);
                this.mIsBeingDragged = false;
                this.mCurrPercentage = 0.0F;
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                this.mIsBeingDragged = false;
                this.mActivePointerId = INVALID_POINTER;
                if(!this.mRefreshing && this.mCurrPercentage != 0.0F){
                    updatePositionTimeout(50L);
                }
                return false;
            case MotionEvent.ACTION_MOVE:
                int pointerIndex = ev.findPointerIndex(this.mActivePointerId);
                if (pointerIndex < 0) {
                    Log.e(LOG_TAG, "Got ACTION_MOVE event but have an invalid active pointer id.");
                    return false;
                }

                float y = ev.getY(pointerIndex);
                float yDiff = y - this.mInitialMotionY;
                if (!this.mIsBeingDragged && yDiff > (float) this.mTouchSlop) {
                    this.mIsBeingDragged = true;
                }

                if (this.mIsBeingDragged) {
                    if (yDiff > this.mDistanceToTriggerSync) {
                        this.startRefresh();
                    } else {
                        this.setTriggerPercentage(this.mAccelerateInterpolator.getInterpolation(yDiff / this.mDistanceToTriggerSync));
                        if (this.mLayoutDraggingTogether) {
                            this.updateContentOffsetTop((int) yDiff);
                        }
                        if (this.mLastMotionY > y && this.mTarget.getTop() == this.getPaddingTop()) {
                            this.removeCallbacks(this.mCancel);
                        } else {
                            this.updatePositionTimeout(RETURN_TO_ORIGINAL_POSITION_TIMEOUT);
                        }
                    }

                    this.mLastMotionY = y;
                }
            case MotionEvent.ACTION_OUTSIDE:
            default:
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                int index = ev.getActionIndex();
                this.mLastMotionY = ev.getY(index);
                this.mActivePointerId = ev.getPointerId(index);
                break;
            case MotionEvent.ACTION_POINTER_UP:
                this.onSecondaryPointerUp(ev);
        }

        return true;
    } else {
        return false;
    }
}
 
Example 10
Source File: MovableTouchListener.java    From Noyze with Apache License 2.0 4 votes vote down vote up
@Override
 public boolean onTouch(View v, MotionEvent event) {
 	if (null != mAnimator && mAnimator.isRunning()) return false;
 	if (null == mPopupWindow) return false;
 
 	switch (event.getAction() & MotionEvent.ACTION_MASK) {
 		case MotionEvent.ACTION_DOWN:
 			LayoutParams params = mPopupWindow.getWindowParams();
 			aPID = event.getPointerId(0);
 			downX = (int) (params.x - event.getRawX());
 			downY = (int) (params.y - event.getRawY());
	break;
case MotionEvent.ACTION_UP:
	// Handle "snapping" to the right/ left edge of the screen.
	if (null != mAnchor) {
		final Rect mBounds = mPopupWindow.getBounds();
		final int[] screen = mPopupWindow.getWindowDimensions();
		switch (mAnchor) {
			case AXIS_Y:
				if (mBounds.left + (mBounds.width() / 2) > (screen[0] / 2)) { // Snap right.
					snap(screen[0] - mBounds.width(), PopupWindow.POSITION_UNCHANGED);
				} else { // Snap left.
					snap(0, PopupWindow.POSITION_UNCHANGED);
				}
				break;
			case AXIS_X:
				if (mBounds.top + (mBounds.height() / 2) > (screen[1] / 2)) { // Snap bottom.
					snap(PopupWindow.POSITION_UNCHANGED, screen[1] - mBounds.height());
				} else { // Snap top.
					snap(PopupWindow.POSITION_UNCHANGED, 0);
				}
				break;
			case EDGES:
				if (mBounds.top + (mBounds.height() / 2) > (screen[1] / 2)) { // Snap bottom half.
					if (mBounds.left + (mBounds.width() / 2) > (screen[0] / 2)) // Snap bottom right.
						snap(screen[0] - mBounds.width(), screen[1] - mBounds.height());
					else // Snap bottom left.
						snap(0, screen[1] - mBounds.height());
				} else { // Snap top half.
					if (mBounds.left + (mBounds.width() / 2) > (screen[0] / 2)) // Snap top right.
						snap(screen[0] - mBounds.width(), 0);
					else // Snap top left.
						snap(0, 0);
				}
				break;
		}
	}
	break;
case MotionEvent.ACTION_POINTER_UP:
	// Extract the index of the pointer that left the touch sensor
	final int pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
	final int pointerId = event.getPointerId(pointerIndex);
	if (pointerId == aPID) {
		// This was our active pointer going up. Choose a new
		// active pointer and adjust accordingly.
		final int newPointerIndex = (pointerIndex == 0) ? 1 : 0;
		aPID = event.getPointerId(newPointerIndex);
	}
case MotionEvent.ACTION_MOVE:
	// Find the index of the active pointer and fetch its position
	final int mPID = event.findPointerIndex(aPID);
	float xMove = event.getRawX();
	float yMove = event.getRawY();

	// From http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html
	final int dx = (int) (xMove + downX);
	final int dy = (int) (yMove + downY);

	mPopupWindow.position(((lockX) ? PopupWindow.POSITION_UNCHANGED : dx),
					  ((lockY) ? PopupWindow.POSITION_UNCHANGED : dy));
					  
	break;
case MotionEvent.ACTION_CANCEL:
   			aPID = MotionEvent.INVALID_POINTER_ID;
   			break;
 	}
 	
 	return true;
 }
 
Example 11
Source File: FlipViewPager.java    From Travel-Mate with MIT License 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    // Custom code starts here
    View view = mCurrent.pageView;
    if (view instanceof ViewGroup) {
        ViewGroup viewGroup = (ViewGroup) view;
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            final View childView = viewGroup.getChildAt(i);
            checkIfChildWasClicked(ev, childView);
        }
    }

    // Custom code ends here
    int action = ev.getAction() & MotionEvent.ACTION_MASK;
    if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        toggleFlip(false);
        mActivePointerId = INVALID_POINTER;
        recycleVelocity();
        return false;
    }

    if (action != MotionEvent.ACTION_DOWN && !mFlipping) return false;

    switch (action) {
        case MotionEvent.ACTION_MOVE:
            int activePointerId = this.mActivePointerId;
            if (activePointerId == INVALID_POINTER) break;

            int pointerIndex = ev.findPointerIndex(activePointerId);
            if (pointerIndex == -1) {
                this.mActivePointerId = INVALID_POINTER;
                break;
            }

            float x = ev.getX(pointerIndex);
            float dx = x - mLastMotionX;
            float xDiff = Math.abs(dx);
            float y = ev.getY(pointerIndex);
            float dy = y - mLastMotionY;
            float yDiff = Math.abs(dy);

            if (xDiff > mTouchSlop && xDiff > yDiff) {
                toggleFlip(true);
                mLastMotionX = x;
                mLastMotionY = y;
            }
            break;

        case MotionEvent.ACTION_DOWN:
            this.mActivePointerId = ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK;
            mLastMotionX = ev.getX(this.mActivePointerId);
            mLastMotionY = ev.getY(this.mActivePointerId);
            toggleFlip(!mScroller.isFinished());
            break;
        case MotionEvent.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);
            break;
    }

    if (!mFlipping)
        trackVelocity(ev);
    return !mFlipping;
}
 
Example 12
Source File: ItemTouchHelper.java    From Carbon with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
    if (DEBUG) {
        Log.d(TAG, "intercept: x:" + event.getX() + ",y:" + event.getY() + ", " + event);
    }
    final int action = event.getActionMasked();
    if (action == MotionEvent.ACTION_DOWN) {
        mActivePointerId = event.getPointerId(0);
        mInitialTouchX = event.getX();
        mInitialTouchY = event.getY();
        obtainVelocityTracker();
        if (mSelected == null) {
            final RecoverAnimation animation = findAnimation(event);
            if (animation != null) {
                mInitialTouchX -= animation.mX;
                mInitialTouchY -= animation.mY;
                endRecoverAnimation(animation.mViewHolder, true);
                if (mPendingCleanup.remove(animation.mViewHolder.itemView)) {
                    mCallback.clearView(mRecyclerView, animation.mViewHolder);
                }
                select(animation.mViewHolder, animation.mActionState);
                updateDxDy(event, mSelectedFlags, 0);
            }
        }
    } else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
        mActivePointerId = ACTIVE_POINTER_ID_NONE;
        select(null, ACTION_STATE_IDLE);
    } else if (mActivePointerId != ACTIVE_POINTER_ID_NONE) {
        // in a non scroll orientation, if distance change is above threshold, we
        // can select the item
        final int index = event.findPointerIndex(mActivePointerId);
        if (DEBUG) {
            Log.d(TAG, "pointer index " + index);
        }
        if (index >= 0) {
            checkSelectForSwipe(action, event, index);
        }
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(event);
    }
    return mSelected != null;
}
 
Example 13
Source File: DynamicListView.java    From arcgis-runtime-samples-android with Apache License 2.0 4 votes vote down vote up
@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;
        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);
}
 
Example 14
Source File: ReItemTouchHelper.java    From ReSwipeCard with Apache License 2.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
    if (DEBUG) {
        Log.d(TAG,
                "on touch: x:" + mInitialTouchX + ",y:" + mInitialTouchY + ", :" + event);
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(event);
    }
    if (mActivePointerId == ACTIVE_POINTER_ID_NONE) {
        return;
    }
    final int action = MotionEventCompat.getActionMasked(event);
    final int activePointerIndex = event.findPointerIndex(mActivePointerId);
    if (activePointerIndex >= 0) {
        checkSelectForSwipe(action, event, activePointerIndex);
    }
    ViewHolder viewHolder = mSelected;
    if (viewHolder == null) {
        return;
    }
    switch (action) {
        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position
            if (activePointerIndex >= 0) {
                if (mCallback.enableHardWare() && viewHolder.itemView.getLayerType() != View.LAYER_TYPE_HARDWARE) {
                    viewHolder.itemView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
                }
                updateDxDy(event, mSelectedFlags, activePointerIndex);
                moveIfNecessary(viewHolder);
                mRecyclerView.removeCallbacks(mScrollRunnable);
                mScrollRunnable.run();
                mRecyclerView.invalidate();
            }
            break;
        }
        case MotionEvent.ACTION_CANCEL:
            if (mVelocityTracker != null) {
                mVelocityTracker.clear();
            }
            // fall through
        case MotionEvent.ACTION_UP:
            select(null, ACTION_STATE_IDLE);
            mActivePointerId = ACTIVE_POINTER_ID_NONE;
            break;
        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = MotionEventCompat.getActionIndex(event);
            final int pointerId = event.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = event.getPointerId(newPointerIndex);
                updateDxDy(event, mSelectedFlags, pointerIndex);
            }
            break;
        }
    }
}
 
Example 15
Source File: ItemTouchHelper.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
    if (DEBUG) {
        Log.d(TAG,
                "on touch: x:" + mInitialTouchX + ",y:" + mInitialTouchY + ", :" + event);
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(event);
    }
    if (mActivePointerId == ACTIVE_POINTER_ID_NONE) {
        return;
    }
    final int action = event.getActionMasked();
    final int activePointerIndex = event.findPointerIndex(mActivePointerId);
    if (activePointerIndex >= 0) {
        checkSelectForSwipe(action, event, activePointerIndex);
    }
    ViewHolder viewHolder = mSelected;
    if (viewHolder == null) {
        return;
    }
    switch (action) {
        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position
            if (activePointerIndex >= 0) {
                updateDxDy(event, mSelectedFlags, activePointerIndex);
                moveIfNecessary(viewHolder);
                mRecyclerView.removeCallbacks(mScrollRunnable);
                mScrollRunnable.run();
                mRecyclerView.invalidate();
            }
            break;
        }
        case MotionEvent.ACTION_CANCEL:
            if (mVelocityTracker != null) {
                mVelocityTracker.clear();
            }
            // fall through
        case MotionEvent.ACTION_UP:
            select(null, ACTION_STATE_IDLE);
            mActivePointerId = ACTIVE_POINTER_ID_NONE;
            break;
        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = event.getActionIndex();
            final int pointerId = event.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = event.getPointerId(newPointerIndex);
                updateDxDy(event, mSelectedFlags, pointerIndex);
            }
            break;
        }
    }
}
 
Example 16
Source File: PullToZoomListView.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public boolean onTouchEvent(MotionEvent paramMotionEvent) {
	Log.d("mmm", "" + (0xFF & paramMotionEvent.getAction()));
	switch (0xFF & paramMotionEvent.getAction()) {
	case 4:
	case 0:
		if (!this.mScalingRunnalable.mIsFinished) {
			this.mScalingRunnalable.abortAnimation();
		}
		this.mLastMotionY = paramMotionEvent.getY();
		this.mActivePointerId = paramMotionEvent.getPointerId(0);
		this.mMaxScale = (this.mScreenHeight / this.mHeaderHeight);
		this.mLastScale = (this.mHeaderContainer.getBottom() / this.mHeaderHeight);
		break;
	case 2:
		Log.d("mmm", "mActivePointerId" + mActivePointerId);
		int j = paramMotionEvent.findPointerIndex(this.mActivePointerId);
		if (j == -1) {
			Log.e("PullToZoomListView", "Invalid pointerId="
					+ this.mActivePointerId + " in onTouchEvent");
		} else {
			if (this.mLastMotionY == -1.0F)
				this.mLastMotionY = paramMotionEvent.getY(j);
			if (this.mHeaderContainer.getBottom() >= this.mHeaderHeight) {
				ViewGroup.LayoutParams localLayoutParams = this.mHeaderContainer
						.getLayoutParams();
				float f = ((paramMotionEvent.getY(j) - this.mLastMotionY + this.mHeaderContainer
						.getBottom()) / this.mHeaderHeight - this.mLastScale)
						/ 2.0F + this.mLastScale;
				if ((this.mLastScale <= 1.0D) && (f < this.mLastScale)) {
					localLayoutParams.height = this.mHeaderHeight;
					this.mHeaderContainer
							.setLayoutParams(localLayoutParams);
					return super.onTouchEvent(paramMotionEvent);
				}
				this.mLastScale = Math.min(Math.max(f, 1.0F),
						this.mMaxScale);
				localLayoutParams.height = ((int) (this.mHeaderHeight * this.mLastScale));
				if (localLayoutParams.height < this.mScreenHeight)
					this.mHeaderContainer
							.setLayoutParams(localLayoutParams);
				this.mLastMotionY = paramMotionEvent.getY(j);
				return true;
			}
			this.mLastMotionY = paramMotionEvent.getY(j);
		}
		break;
	case 1:
		reset();
		endScraling();
		break;
	case 3:
		int i = paramMotionEvent.getActionIndex();
		this.mLastMotionY = paramMotionEvent.getY(i);
		this.mActivePointerId = paramMotionEvent.getPointerId(i);
		break;
	case 5:
		onSecondaryPointerUp(paramMotionEvent);
		this.mLastMotionY = paramMotionEvent.getY(paramMotionEvent
				.findPointerIndex(this.mActivePointerId));
		break;
	case 6:
	}
	return super.onTouchEvent(paramMotionEvent);
}
 
Example 17
Source File: HeaderBehavior.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onTouchEvent(
    @NonNull CoordinatorLayout parent, @NonNull V child, @NonNull MotionEvent ev) {
  boolean consumeUp = false;
  switch (ev.getActionMasked()) {
    case MotionEvent.ACTION_MOVE:
      final int activePointerIndex = ev.findPointerIndex(activePointerId);
      if (activePointerIndex == -1) {
        return false;
      }

      final int y = (int) ev.getY(activePointerIndex);
      int dy = lastMotionY - y;
      lastMotionY = y;
      // We're being dragged so scroll the ABL
      scroll(parent, child, dy, getMaxDragOffset(child), 0);
      break;
    case MotionEvent.ACTION_POINTER_UP:
      int newIndex = ev.getActionIndex() == 0 ? 1 : 0;
      activePointerId = ev.getPointerId(newIndex);
      lastMotionY = (int) (ev.getY(newIndex) + 0.5f);
      break;
    case MotionEvent.ACTION_UP:
      if (velocityTracker != null) {
        consumeUp = true;
        velocityTracker.addMovement(ev);
        velocityTracker.computeCurrentVelocity(1000);
        float yvel = velocityTracker.getYVelocity(activePointerId);
        fling(parent, child, -getScrollRangeForDragFling(child), 0, yvel);
      }

      // $FALLTHROUGH
    case MotionEvent.ACTION_CANCEL:
      isBeingDragged = false;
      activePointerId = INVALID_POINTER;
      if (velocityTracker != null) {
        velocityTracker.recycle();
        velocityTracker = null;
      }
      break;
  }

  if (velocityTracker != null) {
    velocityTracker.addMovement(ev);
  }

  return isBeingDragged || consumeUp;
}
 
Example 18
Source File: PullToZoomListView.java    From UltimateAndroid with Apache License 2.0 4 votes vote down vote up
public boolean onTouchEvent(MotionEvent paramMotionEvent) {
    Log.d(TAG, "action = " + (0xFF & paramMotionEvent.getAction()));
    if (mHeaderView != null && !isHideHeader && isEnableZoom) {
        switch (0xFF & paramMotionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_OUTSIDE:
                if (!mScalingRunnable.isFinished()) {
                    mScalingRunnable.abortAnimation();
                }
                mLastMotionY = paramMotionEvent.getY();
                mActivePointerId = paramMotionEvent.getPointerId(0);
                mMaxScale = (mScreenHeight / mHeaderHeight);
                mLastScale = (mHeaderContainer.getBottom() / mHeaderHeight);
                break;
            case MotionEvent.ACTION_MOVE:
                Log.d(TAG, "mActivePointerId" + mActivePointerId);
                int j = paramMotionEvent.findPointerIndex(mActivePointerId);
                if (j == INVALID_VALUE) {
                    Log.e("PullToZoomListView", "Invalid pointerId=" + mActivePointerId + " in onTouchEvent");
                } else {
                    if (mLastMotionY == -1.0F) {
                        mLastMotionY = paramMotionEvent.getY(j);
                    }
                    if (mHeaderContainer.getBottom() >= mHeaderHeight) {
                        ViewGroup.LayoutParams localLayoutParams = mHeaderContainer.getLayoutParams();
                        float f = ((paramMotionEvent.getY(j) - mLastMotionY + mHeaderContainer.getBottom()) / mHeaderHeight - mLastScale) / 2.0F + mLastScale;
                        if ((mLastScale <= 1.0D) && (f < mLastScale)) {
                            localLayoutParams.height = mHeaderHeight;
                            mHeaderContainer.setLayoutParams(localLayoutParams);
                            return super.onTouchEvent(paramMotionEvent);
                        }
                        mLastScale = Math.min(Math.max(f, 1.0F), mMaxScale);
                        localLayoutParams.height = ((int) (mHeaderHeight * mLastScale));
                        if (localLayoutParams.height < mScreenHeight) {
                            mHeaderContainer.setLayoutParams(localLayoutParams);
                        }
                        mLastMotionY = paramMotionEvent.getY(j);
                        return true;
                    }
                    mLastMotionY = paramMotionEvent.getY(j);
                }
                break;
            case MotionEvent.ACTION_UP:
                reset();
                endScaling();
                break;
            case MotionEvent.ACTION_CANCEL:
                int i = paramMotionEvent.getActionIndex();
                mLastMotionY = paramMotionEvent.getY(i);
                mActivePointerId = paramMotionEvent.getPointerId(i);
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                onSecondaryPointerUp(paramMotionEvent);
                mLastMotionY = paramMotionEvent.getY(paramMotionEvent.findPointerIndex(mActivePointerId));
                break;
        }
    }
    return super.onTouchEvent(paramMotionEvent);
}
 
Example 19
Source File: ItemTouchHelper.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) {
    mGestureDetector.onTouchEvent(event);
    if (DEBUG) {
        Log.d(TAG,
                "on touch: x:" + mInitialTouchX + ",y:" + mInitialTouchY + ", :" + event);
    }
    if (mVelocityTracker != null) {
        mVelocityTracker.addMovement(event);
    }
    if (mActivePointerId == ACTIVE_POINTER_ID_NONE) {
        return;
    }
    final int action = event.getActionMasked();
    final int activePointerIndex = event.findPointerIndex(mActivePointerId);
    if (activePointerIndex >= 0) {
        checkSelectForSwipe(action, event, activePointerIndex);
    }
    ViewHolder viewHolder = mSelected;
    if (viewHolder == null) {
        return;
    }
    switch (action) {
        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position
            if (activePointerIndex >= 0) {
                updateDxDy(event, mSelectedFlags, activePointerIndex);
                moveIfNecessary(viewHolder);
                mRecyclerView.removeCallbacks(mScrollRunnable);
                mScrollRunnable.run();
                mRecyclerView.invalidate();
            }
            break;
        }
        case MotionEvent.ACTION_CANCEL:
            if (mVelocityTracker != null) {
                mVelocityTracker.clear();
            }
            // fall through
        case MotionEvent.ACTION_UP:
            select(null, ACTION_STATE_IDLE);
            mActivePointerId = ACTIVE_POINTER_ID_NONE;
            break;
        case MotionEvent.ACTION_POINTER_UP: {
            final int pointerIndex = event.getActionIndex();
            final int pointerId = event.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mActivePointerId = event.getPointerId(newPointerIndex);
                updateDxDy(event, mSelectedFlags, pointerIndex);
            }
            break;
        }
    }
}
 
Example 20
Source File: SwipeListener.java    From Swipe-Deck with MIT License 4 votes vote down vote up
@Override
public boolean onTouch(View v, MotionEvent event) {
    if (deactivated) return false;
    switch (event.getAction() & MotionEvent.ACTION_MASK) {

        case MotionEvent.ACTION_DOWN:
            click = true;
            //gesture has begun
            float x;
            float y;
            //cancel any current animations
            v.clearAnimation();

            mActivePointerId = event.getPointerId(0);

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

            if(event.findPointerIndex(mActivePointerId) == 0) {
                callback.cardActionDown();
            }

            initialXPress = x;
            initialYPress = y;
            break;

        case MotionEvent.ACTION_MOVE:
            //gesture is in progress

            final int pointerIndex = event.findPointerIndex(mActivePointerId);
            //Log.i("pointer index: " , Integer.toString(pointerIndex));
            if(pointerIndex < 0 || pointerIndex > 0 ){
                break;
            }

            final float xMove = event.getX(pointerIndex);
            final float yMove = event.getY(pointerIndex);

            //calculate distance moved
            final float dx = xMove - initialXPress;
            final float dy = yMove - initialYPress;

            //throw away the move in this case as it seems to be wrong
            //TODO: figure out why this is the case
            if((int)initialXPress == 0 && (int) initialYPress == 0){
                //makes sure the pointer is valid
                break;
            }
            //calc rotation here
            float posX = card.getX() + dx;
            float posY = card.getY() + dy;

            //in this circumstance consider the motion a click
            if (Math.abs(dx + dy) > 5) click = false;

            card.setX(posX);
            card.setY(posY);

            //card.setRotation
            float distobjectX = posX - initialX;
            float rotation = ROTATION_DEGREES * 2.f * distobjectX / parentWidth;
            card.setRotation(rotation);

            if (rightView != null && leftView != null){
                //set alpha of left and right image
                float alpha = (((posX - paddingLeft) / (parentWidth * OPACITY_END)));
                //float alpha = (((posX - paddingLeft) / parentWidth) * ALPHA_MAGNITUDE );
                //Log.i("alpha: ", Float.toString(alpha));
                rightView.setAlpha(alpha);
                leftView.setAlpha(-alpha);
            }

            break;

        case MotionEvent.ACTION_UP:
            //gesture has finished
            //check to see if card has moved beyond the left or right bounds or reset
            //card position
            checkCardForEvent();

            if(event.findPointerIndex(mActivePointerId) == 0) {
                callback.cardActionUp();
            }
            //check if this is a click event and then perform a click
            //this is a workaround, android doesn't play well with multiple listeners

            if (click) v.performClick();
            //if(click) return false;

            break;

        default:
            return false;
    }
    return true;
}