Java Code Examples for android.view.MotionEvent#ACTION_CANCEL
The following examples show how to use
android.view.MotionEvent#ACTION_CANCEL .
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: HorizontalListView.java From sealtalk-android with MIT License | 6 votes |
@Override public boolean onTouchEvent(MotionEvent event) { // Detect when the user lifts their finger off the screen after a touch if (event.getAction() == MotionEvent.ACTION_UP) { // If not flinging then we are idle now. The user just finished a finger scroll. if (mFlingTracker == null || mFlingTracker.isFinished()) { setCurrentScrollState(OnScrollStateChangedListener.ScrollState.SCROLL_STATE_IDLE); } // Allow the user to interact with parent views requestParentListViewToNotInterceptTouchEvents(false); releaseEdgeGlow(); } else if (event.getAction() == MotionEvent.ACTION_CANCEL) { unpressTouchedChild(); releaseEdgeGlow(); // Allow the user to interact with parent views requestParentListViewToNotInterceptTouchEvents(false); } return super.onTouchEvent(event); }
Example 2
Source File: CircleImageView.java From CircularImageView with Apache License 2.0 | 6 votes |
@Override public boolean onTouchEvent(MotionEvent event) { boolean processed = false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (!isInCircle(event.getX(), event.getY())) { return false; } processed = true; mPressed = true; invalidate(); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: processed = true; mPressed = false; invalidate(); if (!isInCircle(event.getX(), event.getY())) { return false; } break; } return super.onTouchEvent(event) || processed; }
Example 3
Source File: SwipeItemLayout.java From PocketEOS-Android with GNU Lesser General Public License v3.0 | 5 votes |
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (!mSwipeEnable) { return false; } int action = ev.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: mIsDragged = false; mDownX = ev.getX(); mDownY = ev.getY(); break; case MotionEvent.ACTION_MOVE: checkCanDragged(ev); break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (mIsDragged) { mDragHelper.processTouchEvent(ev); mIsDragged = false; } break; default: if (mIsDragged) { mDragHelper.processTouchEvent(ev); } break; } return mIsDragged || super.onInterceptTouchEvent(ev); }
Example 4
Source File: EclairGestureDetector.java From ZoomPreviewPicture with Apache License 2.0 | 5 votes |
@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 5
Source File: CounterDetailScreen.java From AndroidMvc with Apache License 2.0 | 5 votes |
@OnTouch(R.id.screen_detail_buttonIncrement) boolean onTouchIncrement(final View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: controller.startContinuousIncrement(); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: controller.stopContinuousIncrement(); break; } return false; }
Example 6
Source File: ControlPad.java From Kore with Apache License 2.0 | 5 votes |
private void setupListeners(Context context) { final Animation buttonInAnim = AnimationUtils.loadAnimation(context, R.anim.button_in); final Animation buttonOutAnim = AnimationUtils.loadAnimation(context, R.anim.button_out); RepeatListener repeatListener = new RepeatListener(initialButtonRepeatInterval, buttonRepeatInterval, this, buttonInAnim, buttonOutAnim, getContext()); OnTouchListener feedbackTouchListener = new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: buttonInAnim.setFillAfter(true); v.startAnimation(buttonInAnim); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: v.startAnimation(buttonOutAnim); break; } return false; } }; leftButton.setOnTouchListener(repeatListener); rightButton.setOnTouchListener(repeatListener); upButton.setOnTouchListener(repeatListener); downButton.setOnTouchListener(repeatListener); setupButton(selectButton, feedbackTouchListener); setupButton(backButton, feedbackTouchListener); setupButton(infoButton, feedbackTouchListener); setupButton(contextButton, feedbackTouchListener); setupButton(osdButton, feedbackTouchListener); }
Example 7
Source File: TurnCardListView.java From turncardlistview with Apache License 2.0 | 5 votes |
@Override public boolean dispatchTouchEvent(MotionEvent ev) { if (mIsAnimating || mAdapter == null || mAdapter.getCount() == 0) { return super.dispatchTouchEvent(ev); } switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: mStartX = ev.getRawX(); mStartY = ev.getRawY(); mIsTurning = false; mRatio = 0; break; case MotionEvent.ACTION_MOVE: int dx = (int) (ev.getRawX() - mStartX); int dy = (int) (ev.getRawY() - mStartY); if ((dx * dx + dy * dy) > mTouchSlop) { mRatio = dy * 1f / getMeasuredHeight(); turning(mRatio); mIsTurning = true; return true; } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (mIsTurning) { mIsTurning = false; float threshold = 0.1f; if (mRatio > threshold && mPosition < mAdapter.getCount() - 1) { // older new TurnAnimator().turn(true, mRatio); } else if (-mRatio > threshold && mPosition > 0) { // newer new TurnAnimator().turn(true, mRatio); } else { new TurnAnimator().turn(false, mRatio); } return true; } } return super.dispatchTouchEvent(ev); }
Example 8
Source File: ColorCircularSeekBar.java From Gizwits-SmartBuld_Android with MIT License | 5 votes |
@Override public boolean onTouchEvent(MotionEvent event) { if (!SHOW_SEEKBAR) { return true; } float markRange = DensityUtils.dp2px(getContext(), 60); float x = event.getX(); float y = event.getY(); boolean up = false; switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (x < markPointX + markRange && x > markPointX - markRange && y > markPointY - markRange && y < markPointY + markRange) { setParentScrollAble(false); } moved(x, y, up); break; case MotionEvent.ACTION_MOVE: moved(x, y, up); break; case MotionEvent.ACTION_UP: up = true; moved(x, y, up); break; case MotionEvent.ACTION_CANCEL: setParentScrollAble(true); up = true; moved(x, y, up); break; } return true; }
Example 9
Source File: WeiboTextView.java From BigApp_Discuz_Android with Apache License 2.0 | 4 votes |
@Override public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) { int action = event.getAction(); // LogUtils.printLog(WeiboTextView.class, "MotionEvent:" + action); // if (action == MotionEvent.ACTION_MOVE) { // isPressed = false; // } switch (action) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_CANCEL: buffer.setSpan(new BackgroundColorSpan(0x00000000), 0, weiboTextView.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Selection.setSelection(buffer, 0, weiboTextView.length()); break; } if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= widget.getTotalPaddingLeft(); y -= widget.getTotalPaddingTop(); x += widget.getScrollX(); y += widget.getScrollY(); Layout layout = widget.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { int start = buffer.getSpanStart(link[0]); int end = buffer.getSpanEnd(link[0]); // LogUtils.printLog(WebViewUtils.class, "start:" + start + " end:" + end); if (action == MotionEvent.ACTION_UP) { link[0].onClick(widget); buffer.setSpan(new BackgroundColorSpan(0x00000000), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Selection.setSelection(buffer, start, end); } else if (action == MotionEvent.ACTION_DOWN) { buffer.setSpan(new BackgroundColorSpan(weiboTextView.clickBgClolr), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); Selection.setSelection(buffer, start, end); } if (widget instanceof WeiboTextView) { ((WeiboTextView) widget).linkHit = true; } return true; } else { Selection.removeSelection(buffer); Touch.onTouchEvent(widget, buffer, event); return false; } } return Touch.onTouchEvent(widget, buffer, event); }
Example 10
Source File: NestedGeckoView.java From firefox-echo-show with Mozilla Public License 2.0 | 4 votes |
@Override public boolean onTouchEvent(MotionEvent ev) { boolean eventHandled = false; final MotionEvent event = MotionEvent.obtain(ev); final int action = event.getActionMasked(); if (action == MotionEvent.ACTION_DOWN) { mNestedOffsetY = 0; } final int eventY = (int) event.getY(); event.offsetLocation(0, mNestedOffsetY); switch (action) { case MotionEvent.ACTION_MOVE: int deltaY = mLastY - eventY; if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) { deltaY -= mScrollConsumed[1]; mLastY = eventY - mScrollOffset[1]; event.offsetLocation(0, -mScrollOffset[1]); mNestedOffsetY += mScrollOffset[1]; } eventHandled = super.onTouchEvent(event); if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) { event.offsetLocation(0, mScrollOffset[1]); mNestedOffsetY += mScrollOffset[1]; mLastY -= mScrollOffset[1]; } break; case MotionEvent.ACTION_DOWN: eventHandled = super.onTouchEvent(event); mLastY = eventY; startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: eventHandled = super.onTouchEvent(event); stopNestedScroll(); break; default: // We don't care about other touch events } return eventHandled; }
Example 11
Source File: ChandelierLayout.java From Chandelier with MIT License | 4 votes |
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { ensureTarget(); final int action = MotionEventCompat.getActionMasked(ev); if (isReturningToStart && action == MotionEvent.ACTION_DOWN) { isReturningToStart = false; } if (!isEnabled() || isReturningToStart || canChildScrollUp()) { // Fail fast if we're not in a state where a swipe is possible return false; } switch (action) { case MotionEvent.ACTION_DOWN: setTargetOffsetTopAndBottom(originalOffsetTop - ornamentLayout.getTop()); activePointerId = MotionEventCompat.getPointerId(ev, 0); isBeingDragged = false; final float initialDownY = getMotionEventY(ev, activePointerId); if (initialDownY == -1) { return false; } this.initialDownY = initialDownY; break; case MotionEvent.ACTION_MOVE: if (activePointerId == INVALID_POINTER) { Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id."); return false; } final float y = getMotionEventY(ev, activePointerId); if (y == -1) { return false; } final float yDiff = y - this.initialDownY; if (yDiff > touchSlop && !isBeingDragged) { initialMotionY = this.initialDownY + touchSlop; isBeingDragged = true; } break; case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: isBeingDragged = false; activePointerId = INVALID_POINTER; break; } return isBeingDragged; }
Example 12
Source File: TwoDScrollView.java From AndroidTreeView with Apache License 2.0 | 4 votes |
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { /* * 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; } if (!canScroll()) { mIsBeingDragged = false; return false; } final float y = ev.getY(); final float x = ev.getX(); switch (action) { 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 yDiff = (int) Math.abs(y - mLastMotionY); final int xDiff = (int) Math.abs(x - mLastMotionX); if (yDiff > mTouchSlop || xDiff > mTouchSlop) { mIsBeingDragged = true; } break; case MotionEvent.ACTION_DOWN: /* Remember location of down touch */ mLastMotionY = y; mLastMotionX = x; /* * 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; break; } /* * The only time we want to intercept motion events is if we are in the * drag mode. */ return mIsBeingDragged; }
Example 13
Source File: RecyclerListView.java From TelePlus-Android with GNU General Public License v2.0 | 4 votes |
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: float x = event.getX(); lastY = event.getY(); float currectY = (float) Math.ceil((getMeasuredHeight() - AndroidUtilities.dp(24 + 30)) * progress) + AndroidUtilities.dp(12); if (LocaleController.isRTL && x > AndroidUtilities.dp(25) || !LocaleController.isRTL && x < AndroidUtilities.dp(107) || lastY < currectY || lastY > currectY + AndroidUtilities.dp(30)) { return false; } startDy = lastY - currectY; pressed = true; lastUpdateTime = System.currentTimeMillis(); getCurrentLetter(); invalidate(); return true; case MotionEvent.ACTION_MOVE: if (!pressed) { return true; } float newY = event.getY(); float minY = AndroidUtilities.dp(12) + startDy; float maxY = getMeasuredHeight() - AndroidUtilities.dp(12 + 30) + startDy; if (newY < minY) { newY = minY; } else if (newY > maxY) { newY = maxY; } float dy = newY - lastY; lastY = newY; progress += dy / (getMeasuredHeight() - AndroidUtilities.dp(24 + 30)); if (progress < 0) { progress = 0; } else if (progress > 1) { progress = 1; } getCurrentLetter(); invalidate(); return true; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: pressed = false; lastUpdateTime = System.currentTimeMillis(); invalidate(); return true; } return super.onTouchEvent(event); }
Example 14
Source File: ZoomAspectScaledTextureView.java From libcommon with Apache License 2.0 | 4 votes |
@SuppressLint({"ClickableViewAccessibility", "SwitchIntDef"}) @Override public boolean onTouchEvent(final MotionEvent event) { if (DEBUG) Log.v(TAG, "onTouchEvent:"); if (handleOnTouchEvent(event)) { return true; // 処理済み } if (mHandleTouchEvent == TOUCH_DISABLED) { return super.onTouchEvent(event); } final int actionCode = event.getActionMasked(); // >= API8 switch (actionCode) { case MotionEvent.ACTION_DOWN: // single touch startWaiting(event); return true; case MotionEvent.ACTION_POINTER_DOWN: { // マルチタッチ時の処理 switch (mState) { case STATE_WAITING: // 最初のマルチタッチ → 拡大縮小・回転操作待機開始 removeCallbacks(mWaitImageReset); // pass through case STATE_DRAGGING: if (event.getPointerCount() > 1) { startCheck(event); return true; } break; } break; } case MotionEvent.ACTION_MOVE: { // moving with single and multi touch switch (mState) { case STATE_WAITING: if (((mHandleTouchEvent & TOUCH_ENABLED_MOVE) == TOUCH_ENABLED_MOVE) && checkTouchMoved(event)) { removeCallbacks(mWaitImageReset); setState(STATE_DRAGGING); return true; } break; case STATE_DRAGGING: if (processDrag(event)) return true; break; case STATE_CHECKING: if (checkTouchMoved(event) && ((mHandleTouchEvent & TOUCH_ENABLED_ZOOM) == TOUCH_ENABLED_ZOOM)) { startZoom(event); return true; } break; case STATE_ZOOMING: if (processZoom(event)) return true; break; case STATE_ROTATING: if (processRotate(event)) return true; break; } break; } case MotionEvent.ACTION_CANCEL: // pass through case MotionEvent.ACTION_UP: removeCallbacks(mWaitImageReset); removeCallbacks(mStartCheckRotate); if ((actionCode == MotionEvent.ACTION_UP) && (mState == STATE_WAITING)) { final long downTime = SystemClock.uptimeMillis() - event.getDownTime(); if (downTime > LONG_PRESS_TIMEOUT) { performLongClick(); } else if (downTime < TAP_TIMEOUT) { performClick(); } } // pass through case MotionEvent.ACTION_POINTER_UP: setState(STATE_NON); break; } return super.onTouchEvent(event); }
Example 15
Source File: LongClickableLinkMovementMethod.java From iBeebo with GNU General Public License v3.0 | 4 votes |
@Override public boolean onTouchEvent(@NonNull TextView widget, @NonNull Spannable buffer, @NonNull MotionEvent event) { int action = event.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { Layout layout = widget.getLayout(); if (layout == null) { return super.onTouchEvent(widget, buffer, event); } int x = (int) event.getX(); int y = (int) event.getY(); x -= widget.getTotalPaddingLeft(); y -= widget.getTotalPaddingTop(); x += widget.getScrollX(); y += widget.getScrollY(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); MyURLSpan[] link = buffer.getSpans(off, off, MyURLSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { if (!mHasPerformedLongPress) { link[0].onClick(widget); } pressed = false; lastEvent = new float[2]; } else { pressed = true; lastEvent[0] = event.getX(); lastEvent[1] = event.getY(); checkForLongClick(link, widget); } return true; } } else if (action == MotionEvent.ACTION_MOVE) { float[] position = { event.getX(), event.getY() }; // int slop = // ViewConfiguration.get(widget.getContext()).getScaledTouchSlop(); int slop = 6; float xInstance = Math.abs(lastEvent[0] - position[0]); float yInstance = Math.abs(lastEvent[1] - position[1]); double instance = Math.sqrt(Math.hypot(xInstance, yInstance)); if (instance > slop) { pressed = false; } } else if (action == MotionEvent.ACTION_CANCEL) { pressed = false; lastEvent = new float[2]; } else { pressed = false; lastEvent = new float[2]; } return super.onTouchEvent(widget, buffer, event); }
Example 16
Source File: VideoSeekBarView.java From TelePlus-Android with GNU General Public License v2.0 | 4 votes |
@Override public boolean onTouchEvent(MotionEvent event) { if (event == null) { return false; } float x = event.getX(); float y = event.getY(); float thumbX = (int)((getMeasuredWidth() - thumbWidth) * progress); if (event.getAction() == MotionEvent.ACTION_DOWN) { int additionWidth = (getMeasuredHeight() - thumbWidth) / 2; if (thumbX - additionWidth <= x && x <= thumbX + thumbWidth + additionWidth && y >= 0 && y <= getMeasuredHeight()) { pressed = true; thumbDX = (int)(x - thumbX); getParent().requestDisallowInterceptTouchEvent(true); invalidate(); return true; } } else if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL) { if (pressed) { if (event.getAction() == MotionEvent.ACTION_UP && delegate != null) { delegate.onSeekBarDrag(thumbX / (float)(getMeasuredWidth() - thumbWidth)); } pressed = false; invalidate(); return true; } } else if (event.getAction() == MotionEvent.ACTION_MOVE) { if (pressed) { thumbX = (int)(x - thumbDX); if (thumbX < 0) { thumbX = 0; } else if (thumbX > getMeasuredWidth() - thumbWidth) { thumbX = getMeasuredWidth() - thumbWidth; } progress = thumbX / (getMeasuredWidth() - thumbWidth); invalidate(); return true; } } return false; }
Example 17
Source File: LinePageIndicator.java From android-discourse with Apache License 2.0 | 4 votes |
public boolean onTouchEvent(android.view.MotionEvent ev) { if (super.onTouchEvent(ev)) { return true; } 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; }
Example 18
Source File: ScrubGestureDetector.java From spark with Apache License 2.0 | 4 votes |
@Override public boolean onTouch(View v, MotionEvent event) { if (!enabled) return false; final float x = event.getX(); final float y = event.getY(); switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: // store the time to compute whether future events are 'long presses' downX = x; downY = y; handler.postDelayed(longPressRunnable, LONG_PRESS_TIMEOUT_MS); return true; case MotionEvent.ACTION_MOVE: // calculate the elapsed time since the down event float timeDelta = event.getEventTime() - event.getDownTime(); // if the user has intentionally long-pressed if (timeDelta >= LONG_PRESS_TIMEOUT_MS) { handler.removeCallbacks(longPressRunnable); scrubListener.onScrubbed(x, y); } else { // if we moved before longpress, remove the callback if we exceeded the tap slop float deltaX = x - downX; float deltaY = y - downY; if (deltaX >= touchSlop || deltaY >= touchSlop) { handler.removeCallbacks(longPressRunnable); // We got a MOVE event that exceeded tap slop but before the long-press // threshold, we don't care about this series of events anymore. return false; } } return true; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: handler.removeCallbacks(longPressRunnable); scrubListener.onScrubEnded(); return true; default: return false; } }
Example 19
Source File: MySwipeRefreshLayout.java From Cotable with Apache License 2.0 | 4 votes |
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { ensureTarget(); final int action = MotionEventCompat.getActionMasked(ev); if (mReturningToStart && action == MotionEvent.ACTION_DOWN) { mReturningToStart = false; } if (!isEnabled() || mReturningToStart || canChildScrollUp() || mRefreshing) { // Fail fast if we're not in a state where a swipe is possible return false; } switch (action) { case MotionEvent.ACTION_DOWN: setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop(), true); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); mIsBeingDragged = false; final float initialMotionY = getMotionEventY(ev, mActivePointerId); if (initialMotionY == -1) { return false; } mInitialMotionY = initialMotionY; case MotionEvent.ACTION_MOVE: if (mActivePointerId == INVALID_POINTER) { Log.e(LOG_TAG, "Got ACTION_MOVE event but don't have an active pointer id."); return false; } final float y = getMotionEventY(ev, mActivePointerId); if (y == -1) { return false; } final float yDiff = y - mInitialMotionY; if (yDiff > mTouchSlop && !mIsBeingDragged) { mIsBeingDragged = true; mProgress.setAlpha(STARTING_PROGRESS_ALPHA); } break; case MotionEventCompat.ACTION_POINTER_UP: onSecondaryPointerUp(ev); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: mIsBeingDragged = false; mActivePointerId = INVALID_POINTER; break; } return mIsBeingDragged; }
Example 20
Source File: CustomGestureDetector.java From PictureSelector with Apache License 2.0 | 4 votes |
private boolean processTouchEvent(MotionEvent ev) { final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mActivePointerId = ev.getPointerId(0); mVelocityTracker = VelocityTracker.obtain(); if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); } mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); mIsDragging = false; break; case MotionEvent.ACTION_MOVE: final float x = getActiveX(ev); final float y = getActiveY(ev); final float dx = x - mLastTouchX, dy = y - mLastTouchY; if (!mIsDragging) { // Use Pythagoras to see if drag length is larger than // touch slop mIsDragging = Math.sqrt((dx * dx) + (dy * dy)) >= mTouchSlop; } if (mIsDragging) { mListener.onDrag(dx, dy); mLastTouchX = x; mLastTouchY = y; if (null != mVelocityTracker) { mVelocityTracker.addMovement(ev); } } break; case MotionEvent.ACTION_CANCEL: mActivePointerId = INVALID_POINTER_ID; // Recycle Velocity Tracker if (null != mVelocityTracker) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; case MotionEvent.ACTION_UP: mActivePointerId = INVALID_POINTER_ID; if (mIsDragging) { if (null != mVelocityTracker) { mLastTouchX = getActiveX(ev); mLastTouchY = getActiveY(ev); // Compute velocity within the last 1000ms mVelocityTracker.addMovement(ev); mVelocityTracker.computeCurrentVelocity(1000); final float vX = mVelocityTracker.getXVelocity(), vY = mVelocityTracker .getYVelocity(); // If the velocity is greater than minVelocity, call // listener if (Math.max(Math.abs(vX), Math.abs(vY)) >= mMinimumVelocity) { mListener.onFling(mLastTouchX, mLastTouchY, -vX, -vY); } } } // Recycle Velocity Tracker if (null != mVelocityTracker) { mVelocityTracker.recycle(); mVelocityTracker = null; } break; case MotionEvent.ACTION_POINTER_UP: final int pointerIndex = Util.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 true; }