List of usage examples for android.view MotionEvent ACTION_MASK
int ACTION_MASK
To view the source code for android.view MotionEvent ACTION_MASK.
Click Source Link
From source file:it.mb.whatshare.Utils.java
/** * Returns a string that represents the symbolic name of the specified * action such as "ACTION_DOWN", "ACTION_POINTER_DOWN(3)" or an equivalent * numeric constant such as "35" if unknown. By Google. * /*from w w w .jav a 2 s . c om*/ * @param action * The action. * @return The symbolic name of the specified action. */ public static String actionToString(int action) { switch (action) { case MotionEvent.ACTION_DOWN: return "ACTION_DOWN"; case MotionEvent.ACTION_UP: return "ACTION_UP"; case MotionEvent.ACTION_CANCEL: return "ACTION_CANCEL"; case MotionEvent.ACTION_OUTSIDE: return "ACTION_OUTSIDE"; case MotionEvent.ACTION_MOVE: return "ACTION_MOVE"; case MotionEvent.ACTION_HOVER_MOVE: return "ACTION_HOVER_MOVE"; case MotionEvent.ACTION_SCROLL: return "ACTION_SCROLL"; case MotionEvent.ACTION_HOVER_ENTER: return "ACTION_HOVER_ENTER"; case MotionEvent.ACTION_HOVER_EXIT: return "ACTION_HOVER_EXIT"; } int index = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: return "ACTION_POINTER_DOWN(" + index + ")"; case MotionEvent.ACTION_POINTER_UP: return "ACTION_POINTER_UP(" + index + ")"; default: return Integer.toString(action); } }
From source file:com.android.leanlauncher.Workspace.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: break;/*from ww w . j a v a 2s. c om*/ case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_UP: if (mTouchState == PagedView.TOUCH_STATE_REST) { final CellLayout currentPage = mWorkspace; if (currentPage != null) { onWallpaperTap(ev); } } } return super.onInterceptTouchEvent(ev); }
From source file:org.mitre.svmp.activities.AppRTCVideoActivity.java
@Override public boolean onTouchEvent(MotionEvent ev) { Log.e(TAG, "inside activity on touch."); Vibrator vb = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); switch (ev.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mDownX = ev.getX();// ww w . ja v a 2 s. c o m mDownY = ev.getY(); isOnClick = true; break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: if (isOnClick) { Log.i(TAG, "onClick "); // pauseVsv(); vb.vibrate(50); // return touchHandler.onTouchEvent(ev); // TODO onClick code } break; case MotionEvent.ACTION_MOVE: if (isOnClick && (Math.abs(mDownX - ev.getX()) > SCROLL_THRESHOLD || Math.abs(mDownY - ev.getY()) > SCROLL_THRESHOLD)) { Log.i(TAG, "movement detected"); isOnClick = false; //vsv.onPause(); } break; default: break; } vsvProgrssBar.setVisibility(View.VISIBLE); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { vsvProgrssBar.setVisibility(View.INVISIBLE); //vsv.onResume(); } }, 1000); Log.e(TAG, "on activity touch is finishing"); return touchHandler.onTouchEvent(ev); }
From source file:com.wunderlist.slidinglayer.SlidingLayer.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN && ev.getEdgeFlags() != 0) { return false; }//from ww w .j a v a 2s. c o m if (!mEnabled || !mIsDragging && !touchPointIsWithinBounds(mInitialX, mInitialY)) { return false; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { completeScroll(); // Remember where the motion event started mLastX = mInitialRawX = getViewX(ev); mLastY = mInitialRawY = getViewY(ev); mInitialX = ev.getX(); mInitialY = ev.getY(); mActivePointerId = ev.getPointerId(0); break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (!touchPointIsWithinBounds(ev.getX(), ev.getY(), false)) return false; final float x = getViewX(ev); final float y = getViewY(ev); final float deltaX = mLastX - x; final float deltaY = mLastY - y; mLastX = x; mLastY = y; if (!mIsDragging) { final float xDiff = Math.abs(x - mInitialRawX); final float yDiff = Math.abs(y - mInitialRawY); final boolean validHorizontalDrag = xDiff > mTouchSlop && xDiff > yDiff; final boolean validVerticalDrag = yDiff > mTouchSlop && yDiff > xDiff; if (validHorizontalDrag || validVerticalDrag) { mIsDragging = true; setDrawingCacheEnabled(true); } } if (mIsDragging) { final float oldScrollX = getScrollX(); final float oldScrollY = getScrollY(); float scrollX = oldScrollX + deltaX; float scrollY = oldScrollY + deltaY; // Log.d("Layer", String.format("Layer scrollX[%f],scrollY[%f]", scrollX, scrollY)); final float leftBound, rightBound; final float bottomBound, topBound; switch (mScreenSide) { case STICK_TO_LEFT: topBound = bottomBound = rightBound = 0; leftBound = getWidth(); // How far left we can scroll break; case STICK_TO_RIGHT: rightBound = -getWidth(); topBound = bottomBound = leftBound = 0; break; case STICK_TO_TOP: topBound = getHeight(); bottomBound = rightBound = leftBound = 0; break; case STICK_TO_BOTTOM: bottomBound = -getHeight(); topBound = rightBound = leftBound = 0; break; default: topBound = bottomBound = rightBound = leftBound = 0; break; } if (scrollX > leftBound) { scrollX = leftBound; } else if (scrollX < rightBound) { scrollX = rightBound; } if (scrollY > topBound) { scrollY = topBound; } else if (scrollY < bottomBound) { scrollY = bottomBound; } // Keep the precision mLastX += scrollX - (int) scrollX; mLastY += scrollY - (int) scrollY; scrollToAndNotify((int) scrollX, (int) scrollY); } break; } case MotionEvent.ACTION_UP: { if (mIsDragging) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); final int initialVelocityX = (int) VelocityTrackerCompat.getXVelocity(velocityTracker, mActivePointerId); final int initialVelocityY = (int) VelocityTrackerCompat.getYVelocity(velocityTracker, mActivePointerId); final int scrollX = getScrollX(); final int scrollY = getScrollY(); final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); final float x = getViewX(ev); final float y = getViewY(ev); int nextState = determineNextStateForDrag(scrollX, scrollY, initialVelocityX, initialVelocityY, (int) mInitialRawX, (int) mInitialRawY, (int) x, (int) y); setLayerState(nextState, true, true, initialVelocityX, initialVelocityY); mActivePointerId = INVALID_VALUE; endDrag(); } else if (changeStateOnTap) { int nextState = determineNextStateAfterTap(); setLayerState(nextState, true, true); } break; } case MotionEvent.ACTION_CANCEL: if (mIsDragging) { setLayerState(mCurrentState, true, true); mActivePointerId = INVALID_VALUE; endDrag(); } break; case MotionEvent.ACTION_POINTER_DOWN: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); mActivePointerId = ev.getPointerId(pointerIndex); mLastX = getViewX(ev); mLastY = getViewY(ev); break; } case MotionEvent.ACTION_POINTER_UP: { onSecondaryPointerUp(ev); final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); mLastX = getViewX(ev); mLastY = getViewY(ev); break; } } return true; }
From source file:com.dirkgassen.wator.ui.view.RangeSlider.java
/** * Handle touch events: drag the thumb to new values. This method also detects clicks. * * @param event the touch event/* w ww .j a va 2s . c o m*/ * @return {@code true} if the event was handled; {@code false} otherwise */ @Override public boolean onTouchEvent(@NonNull MotionEvent event) { if (Log.isLoggable("Wa-Tor", Log.DEBUG)) { Log.d("Wa-Tor", "Got touch event: " + event); } int paddingLeft = getPaddingLeft(); int paddingRight = getPaddingRight(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: touchEventStartTime = System.currentTimeMillis(); valueBeforeDragging = value; activePointerId = event.getPointerId(0); float valuePos = positionFromValue(paddingLeft, paddingRight); float x = event.getX(); if (x < valuePos - thumbSize / 2 || x > valuePos + thumbSize / 2) { int newValue = valueFromPosition(x, paddingLeft, paddingRight); if (Log.isLoggable("Wa-Tor", Log.DEBUG)) { Log.d("Wa-Tor", "Starting to drag thumb OUTSIDE of thumb"); } if (newValue != value) { touchEventStartTime = 0L; // Not a click updateValue(newValue, true /* from user */); } touchOffset = 0f; } else { touchOffset = x - valuePos; if (Log.isLoggable("Wa-Tor", Log.DEBUG)) { Log.d("Wa-Tor", "Starting to drag thumb INSIDE of thumb; offset = " + touchOffset); } } isDragging = true; return true; case MotionEvent.ACTION_MOVE: if (activePointerId != -1) { final int pointerIndex = event.findPointerIndex(activePointerId); float currentPos = event.getX(pointerIndex) - touchOffset; int newValue = valueFromPosition(currentPos, paddingLeft, paddingRight); if (newValue != value) { if (Log.isLoggable("Wa-Tor", Log.DEBUG)) { Log.d("Wa-Tor", "Got new value " + newValue + " (old = " + value + ")"); } touchEventStartTime = 0L; // Not a click updateValue(newValue, true /* from user */); } } return true; case MotionEvent.ACTION_UP: if (touchEventStartTime > 0L && System.currentTimeMillis() - touchEventStartTime < MAX_CLICK_DURATION) { performClick(); } isDragging = false; break; case MotionEvent.ACTION_CANCEL: isDragging = false; updateValue(valueBeforeDragging, true /* from user */); break; case MotionEvent.ACTION_POINTER_UP: isDragging = false; break; } return super.onTouchEvent(event); }
From source file:com.bulletnoid.android.widget.StaggeredGridView.StaggeredGridView3.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (!isEnabled()) { // A disabled view that is clickable still consumes the touch // events, it just doesn't respond to them. return isClickable() || isLongClickable(); }/*from ww w . j a va 2 s. co m*/ final int action = ev.getAction(); View v; int deltaY; if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { mActivePointerId = ev.getPointerId(0); final int x = (int) ev.getX(); final int y = (int) ev.getY(); int motionPosition = pointToPosition(x, y); if (!mDataChanged) { if ((mTouchMode != TOUCH_MODE_FLINGING) && (motionPosition >= 0) && (getAdapter().isEnabled(motionPosition))) { // User clicked on an actual view (and was not stopping a fling). It might be a // click or a scroll. Assume it is a click until proven otherwise mTouchMode = TOUCH_MODE_DOWN; // FIXME Debounce if (mPendingCheckForTap == null) { mPendingCheckForTap = new CheckForTap(); } postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout()); } else { if (ev.getEdgeFlags() != 0 && motionPosition < 0) { // If we couldn't find a view to click on, but the down event was touching // the edge, we will bail out and try again. This allows the edge correcting // code in ViewRoot to try to find a nearby view to select return false; } if (mTouchMode == TOUCH_MODE_FLINGING) { // Stopped a fling. It is a scroll. //createScrollingCache(); mTouchMode = TOUCH_MODE_DRAGGING; mMotionCorrection = 0; motionPosition = findMotionRow(y); reportScrollStateChange(OnScrollListener.SCROLL_STATE_TOUCH_SCROLL); } } } if (motionPosition >= 0) { // Remember where the motion event started v = getChildAt(motionPosition - mFirstPosition); //mMotionViewOriginalTop=v.getTop(); } mLastTouchX = x; mLastTouchY = y; mMotionPosition = motionPosition; mTouchRemainderY = Integer.MIN_VALUE; break; } case MotionEvent.ACTION_MOVE: { final int pointerIndex = ev.findPointerIndex(mActivePointerId); final int y = (int) ev.getY(pointerIndex); deltaY = (int) (y - mLastTouchY); switch (mTouchMode) { case TOUCH_MODE_DOWN: case TOUCH_MODE_TAP: case TOUCH_MODE_DONE_WAITING: // Check if we have moved far enough that it looks more like a // scroll than a tap startScrollIfNeeded(deltaY); break; case TOUCH_MODE_DRAGGING: /*if (PROFILE_SCROLLING) { if (!mScrollProfilingStarted) { Debug.startMethodTracing("AbsListViewScroll"); mScrollProfilingStarted=true; } }*/ if (y != mTouchRemainderY) { deltaY -= mMotionCorrection; int incrementalDeltaY = mTouchRemainderY != Integer.MIN_VALUE ? (int) (y - mTouchRemainderY) : deltaY; // No need to do all this work if we're not going to move anyway boolean atEdge = false; if (incrementalDeltaY != 0) { atEdge = trackMotionScroll(deltaY, true); } // Check to see if we have bumped into the scroll limit if (atEdge && getChildCount() > 0) { // Treat this like we're starting a new scroll from the current // position. This will let the user start scrolling back into // content immediately rather than needing to scroll back to the // point where they hit the limit first. int motionPosition = findMotionRow(y); if (motionPosition >= 0) { final View motionView = getChildAt(motionPosition - mFirstPosition); //mMotionViewOriginalTop=motionView.getTop(); } mLastTouchY = y; mMotionPosition = motionPosition; invalidate(); } mTouchRemainderY = y; } break; } break; } case MotionEvent.ACTION_UP: { switch (mTouchMode) { case TOUCH_MODE_DOWN: case TOUCH_MODE_TAP: case TOUCH_MODE_DONE_WAITING: final int motionPosition = mMotionPosition; final View child = getChildAt(motionPosition - mFirstPosition); System.out.println("child:" + child + " motionPosition:" + motionPosition); if (child != null && !child.hasFocusable()) { if (mTouchMode != TOUCH_MODE_DOWN) { child.setPressed(false); } if (mPerformClick == null) { mPerformClick = new PerformClick(); } final PerformClick performClick = mPerformClick; //performClick.mChild=child; performClick.mClickMotionPosition = motionPosition; performClick.rememberWindowAttachCount(); //mResurrectToPosition=motionPosition; if (mTouchMode == TOUCH_MODE_DOWN || mTouchMode == TOUCH_MODE_TAP) { //mLayoutMode=LAYOUT_NORMAL; if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { mTouchMode = TOUCH_MODE_TAP; layoutChildren(mDataChanged); child.setPressed(true); positionSelector(mMotionPosition, child); setPressed(true); if (mSelector != null) { Drawable d = mSelector.getCurrent(); if (d != null && d instanceof TransitionDrawable) { ((TransitionDrawable) d).resetTransition(); } } postDelayed(new Runnable() { public void run() { child.setPressed(false); setPressed(false); if (!mDataChanged) { post(performClick); } mTouchMode = TOUCH_MODE_REST; } }, ViewConfiguration.getPressedStateDuration()); } else { mTouchMode = TOUCH_MODE_REST; } return true; } else if (!mDataChanged && mAdapter.isEnabled(motionPosition)) { post(performClick); } } mTouchMode = TOUCH_MODE_REST; break; case TOUCH_MODE_DRAGGING: final int childCount = getChildCount(); if (childCount > 0) { /*int top=getFillChildTop(); int bottom=getFillChildBottom(); if (mFirstPosition==0&&top>=mListPadding.top&& mFirstPosition+childCount<mItemCount&& bottom<=getHeight()-getPaddingBottom()mListPadding.bottom) { mTouchMode=TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); } else*/ { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); final int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId); if (Math.abs(initialVelocity) > mMinimumVelocity) { if (mFlingRunnable == null) { mFlingRunnable = new FlingRunnable(); } reportScrollStateChange(OnScrollListener.SCROLL_STATE_FLING); mFlingRunnable.start(-initialVelocity); } else { mTouchMode = TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); } } } else { mTouchMode = TOUCH_MODE_REST; reportScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE); } break; } setPressed(false); // Need to redraw since we probably aren't drawing the selector anymore invalidate(); if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } mActivePointerId = INVALID_POINTER; /*if (PROFILE_SCROLLING) { if (mScrollProfilingStarted) { Debug.stopMethodTracing(); mScrollProfilingStarted=false; } }*/ break; } case MotionEvent.ACTION_CANCEL: { mTouchMode = TOUCH_MODE_REST; setPressed(false); View motionView = this.getChildAt(mMotionPosition - mFirstPosition); if (motionView != null) { motionView.setPressed(false); } clearScrollingCache(); if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } mActivePointerId = INVALID_POINTER; break; } case MotionEvent.ACTION_POINTER_UP: { onSecondaryPointerUp(ev); final int x = (int) mLastTouchX; final int y = (int) mLastTouchY; final int motionPosition = pointToPosition(x, y); if (motionPosition >= 0) { // Remember where the motion event started v = getChildAt(motionPosition - mFirstPosition); //mMotionViewOriginalTop=v.getTop(); mMotionPosition = motionPosition; } mTouchRemainderY = y; break; } } return true; }
From source file:com.albedinsky.android.ui.widget.SeekBarWidget.java
/** *///www . jav a 2 s . co m @Override @SuppressLint("NewApi") public boolean onTouchEvent(@NonNull MotionEvent event) { final boolean processed = super.onTouchEvent(event); final int progress = getProgress(); if (processed) { if (progress != mProgress) { this.handleProgressChange(progress); } this.ensureDecorator(); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: if (mDecorator.hasPrivateFlag(PFLAG_DISCRETE_PREVIEW_ENABLED)) { this.revealDiscreteComponents(); } break; case MotionEvent.ACTION_MOVE: final Drawable background = getBackground(); if (background != null && mAnimations.shouldDraw() && UiConfig.MATERIALIZED) { // Cancel the revealed circle around the thumb. background.setHotspotBounds(0, 0, 0, 0); } break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: if (mDecorator.hasPrivateFlag(PFLAG_DISCRETE_PREVIEW_ENABLED)) { this.concealDiscreteComponents(); } break; } } return processed; }
From source file:org.florescu.android.rangeseekbar.RangeSeekBar.java
/** * Handles thumb selection and movement. Notifies listener callback on certain events. *///from w ww .j av a2 s .c o m @Override public boolean onTouchEvent(@NonNull MotionEvent event) { if (!isEnabled()) { return false; } int pointerIndex; final int action = event.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: // Remember where the motion event started mActivePointerId = event.getPointerId(event.getPointerCount() - 1); pointerIndex = event.findPointerIndex(mActivePointerId); mDownMotionX = event.getX(pointerIndex); pressedThumb = evalPressedThumb(mDownMotionX); // Only handle thumb presses. if (pressedThumb == null) { pressedThumb = getClosestThumb(mDownMotionX); if (pressedThumb == null) { return false; } } if (listener != null) { listener.onRangeSeekBarPressed(this, Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(), pressedThumb); } setPressed(true); invalidate(); onStartTrackingTouch(); trackTouchEvent(event); attemptClaimDrag(); break; case MotionEvent.ACTION_MOVE: if (pressedThumb != null) { if (mIsDragging) { trackTouchEvent(event); } else { // Scroll to follow the motion event pointerIndex = event.findPointerIndex(mActivePointerId); final float x = event.getX(pointerIndex); if (Math.abs(x - mDownMotionX) > mScaledTouchSlop) { setPressed(true); invalidate(); onStartTrackingTouch(); trackTouchEvent(event); attemptClaimDrag(); } } if (notifyWhileDragging && listener != null) { listener.onRangeSeekBarValueChanged(this, Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(), pressedThumb); } } break; case MotionEvent.ACTION_UP: if (mIsDragging) { trackTouchEvent(event); onStopTrackingTouch(); setPressed(false); } else { // Touch up when we never crossed the touch slop threshold // should be interpreted as a tap-seek to that location. onStartTrackingTouch(); trackTouchEvent(event); onStopTrackingTouch(); } if (listener != null) { listener.onRangeSeekBarValueChanged(this, Thumb.MAX.equals(pressedThumb) ? getSelectedMaxValue() : getSelectedMinValue(), pressedThumb); } pressedThumb = null; invalidate(); break; case MotionEvent.ACTION_POINTER_DOWN: { final int index = event.getPointerCount() - 1; // final int index = ev.getActionIndex(); mDownMotionX = event.getX(index); mActivePointerId = event.getPointerId(index); invalidate(); break; } case MotionEvent.ACTION_POINTER_UP: onSecondaryPointerUp(event); invalidate(); break; case MotionEvent.ACTION_CANCEL: if (mIsDragging) { onStopTrackingTouch(); setPressed(false); } invalidate(); // see above explanation break; } return true; }
From source file:com.hippo.widget.BothScrollView.java
@Override public boolean onTouchEvent(MotionEvent ev) { initVelocityTrackerIfNotExists();/*from w w w . j a v a 2s . co m*/ mVelocityTracker.addMovement(ev); final int action = ev.getAction(); switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: { if (getChildCount() == 0) { return false; } if ((mIsBeingDragged = !mScroller.isFinished())) { final ViewParent parent = getParent(); if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); } } /* * If being flinged and user touches, stop the fling. isFinished * will be false if being flinged. */ if (!mScroller.isFinished()) { mScroller.abortAnimation(); } // Remember where the motion event started mLastMotionX = (int) ev.getX(); mLastMotionY = (int) ev.getY(); mActivePointerId = ev.getPointerId(0); break; } case MotionEvent.ACTION_MOVE: final int activePointerIndex = ev.findPointerIndex(mActivePointerId); if (activePointerIndex == -1) { Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent"); break; } final int x = (int) ev.getX(activePointerIndex); final int y = (int) ev.getY(activePointerIndex); int deltaX = mLastMotionX - x; int deltaY = mLastMotionY - y; if (!mIsBeingDragged && (Math.abs(deltaX) > mTouchSlop || Math.abs(deltaY) > mTouchSlop)) { final ViewParent parent = getParent(); if (parent != null) { parent.requestDisallowInterceptTouchEvent(true); } mIsBeingDragged = true; if (deltaX > 0) { deltaX -= mTouchSlop; deltaX = Math.max(0, deltaX); } else { deltaX += mTouchSlop; deltaX = Math.min(0, deltaX); } if (deltaY > 0) { deltaY -= mTouchSlop; deltaY = Math.max(0, deltaY); } else { deltaY += mTouchSlop; deltaY = Math.min(0, deltaY); } } if (mIsBeingDragged) { // Scroll to follow the motion event mLastMotionX = x; mLastMotionY = y; final int oldX = getScrollX(); final int oldY = getScrollY(); final int horizontalRange = getHorizontalScrollRange(); final int verticalRange = getVerticalScrollRange(); final int overscrollMode = getOverScrollMode(); final boolean canOverscroll = overscrollMode == OVER_SCROLL_ALWAYS || (overscrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && (horizontalRange > 0 || verticalRange > 0)); // Calling overScrollBy will call onOverScrolled, which // calls onScrollChanged if applicable. overScrollBy(deltaX, deltaY, oldX, oldY, horizontalRange, verticalRange, mOverscrollDistance, mOverscrollDistance, true); if (canOverscroll) { final int pulledToX = oldX + deltaX; final int pulledToY = oldY + deltaY; if (pulledToX < 0) { mEdgeGlowLeft.onPull((float) deltaX / getWidth()); if (!mEdgeGlowRight.isFinished()) { mEdgeGlowRight.onRelease(); } } else if (pulledToX > horizontalRange) { mEdgeGlowRight.onPull((float) deltaX / getWidth()); if (!mEdgeGlowLeft.isFinished()) { mEdgeGlowLeft.onRelease(); } } if (pulledToY < 0) { mEdgeGlowTop.onPull((float) deltaY / getHeight()); if (!mEdgeGlowBottom.isFinished()) { mEdgeGlowBottom.onRelease(); } } else if (pulledToY > verticalRange) { mEdgeGlowBottom.onPull((float) deltaY / getHeight()); if (!mEdgeGlowTop.isFinished()) { mEdgeGlowTop.onRelease(); } } if (mEdgeGlowLeft != null && (!mEdgeGlowLeft.isFinished() || !mEdgeGlowRight.isFinished() || !mEdgeGlowTop.isFinished() || !mEdgeGlowBottom.isFinished())) { ViewCompat.postInvalidateOnAnimation(this); } } } break; case MotionEvent.ACTION_UP: if (mIsBeingDragged) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocityX = (int) velocityTracker.getXVelocity(mActivePointerId); int initialVelocityY = (int) velocityTracker.getYVelocity(mActivePointerId); if (getChildCount() > 0) { if (Math.abs(initialVelocityX) > mMinimumVelocity || Math.abs(initialVelocityY) > mMinimumVelocity) { fling(-initialVelocityX, -initialVelocityY); } else { if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0, getVerticalScrollRange())) { ViewCompat.postInvalidateOnAnimation(this); } } } mActivePointerId = INVALID_POINTER; endDrag(); } break; case MotionEvent.ACTION_CANCEL: if (mIsBeingDragged && getChildCount() > 0) { if (mScroller.springBack(getScrollX(), getScrollY(), 0, getHorizontalScrollRange(), 0, getVerticalScrollRange())) { ViewCompat.postInvalidateOnAnimation(this); } mActivePointerId = INVALID_POINTER; endDrag(); } break; case MotionEvent.ACTION_POINTER_DOWN: { final int index = ev.getActionIndex(); mLastMotionX = (int) ev.getX(index); mLastMotionY = (int) ev.getY(index); mActivePointerId = ev.getPointerId(index); break; } case MotionEvent.ACTION_POINTER_UP: onSecondaryPointerUp(ev); final int index = ev.findPointerIndex(mActivePointerId); mLastMotionX = (int) ev.getX(index); mLastMotionY = (int) ev.getY(index); break; } return true; }
From source file:com.android.gallery3d.filtershow.imageshow.ImageShow.java
@Override public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event); int action = event.getAction(); action = action & MotionEvent.ACTION_MASK; mGestureDetector.onTouchEvent(event); boolean scaleInProgress = scaleInProgress(); mScaleGestureDetector.onTouchEvent(event); if (mInteractionMode == InteractionMode.SCALE) { return true; }//from w w w . j a va 2 s . co m if (!scaleInProgress() && scaleInProgress) { // If we were scaling, the scale will stop but we will // still issue an ACTION_UP. Let the subclasses know. mFinishedScalingOperation = true; } int ex = (int) event.getX(); int ey = (int) event.getY(); if (action == MotionEvent.ACTION_DOWN) { mInteractionMode = InteractionMode.MOVE; mTouchDown.x = ex; mTouchDown.y = ey; mTouchShowOriginalDate = System.currentTimeMillis(); mShowOriginalDirection = 0; MasterImage.getImage().setOriginalTranslation(MasterImage.getImage().getTranslation()); } if (action == MotionEvent.ACTION_MOVE && mInteractionMode == InteractionMode.MOVE) { mTouch.x = ex; mTouch.y = ey; float scaleFactor = MasterImage.getImage().getScaleFactor(); if (scaleFactor > 1 && (!ENABLE_ZOOMED_COMPARISON || event.getPointerCount() == 2)) { float translateX = (mTouch.x - mTouchDown.x) / scaleFactor; float translateY = (mTouch.y - mTouchDown.y) / scaleFactor; Point originalTranslation = MasterImage.getImage().getOriginalTranslation(); Point translation = MasterImage.getImage().getTranslation(); translation.x = (int) (originalTranslation.x + translateX); translation.y = (int) (originalTranslation.y + translateY); MasterImage.getImage().setTranslation(translation); mTouchShowOriginal = false; } else if (enableComparison() && !mOriginalDisabled && (System.currentTimeMillis() - mTouchShowOriginalDate > mTouchShowOriginalDelayMin) && event.getPointerCount() == 1) { mTouchShowOriginal = true; } } if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_OUTSIDE) { mInteractionMode = InteractionMode.NONE; mTouchShowOriginal = false; mTouchDown.x = 0; mTouchDown.y = 0; mTouch.x = 0; mTouch.y = 0; if (MasterImage.getImage().getScaleFactor() <= 1) { MasterImage.getImage().setScaleFactor(1); MasterImage.getImage().resetTranslation(); } } float scaleFactor = MasterImage.getImage().getScaleFactor(); Point translation = MasterImage.getImage().getTranslation(); constrainTranslation(translation, scaleFactor); MasterImage.getImage().setTranslation(translation); invalidate(); return true; }