List of usage examples for android.view MotionEvent ACTION_POINTER_UP
int ACTION_POINTER_UP
To view the source code for android.view MotionEvent ACTION_POINTER_UP.
Click Source Link
From source file:ch.tutti.android.bottomsheet.ResolverDrawerLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { final int action = ev.getActionMasked(); if (canChildScrollUp()) { // Fail fast if we're not in a state where a swipe is possible return super.onTouchEvent(ev); }/* ww w . jav a 2s . com*/ mVelocityTracker.addMovement(ev); boolean handled = false; switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); mInitialTouchX = x; mInitialTouchY = mLastTouchY = y; mActivePointerId = ev.getPointerId(0); if (findChildUnder(mInitialTouchX, mInitialTouchY) == null && mClickOutsideListener != null) { mIsDragging = handled = true; } handled |= mCollapsibleHeight > 0; mScroller.abortAnimation(); } break; case MotionEvent.ACTION_MOVE: { int index = ev.findPointerIndex(mActivePointerId); if (index < 0) { Log.e(TAG, "Bad pointer id " + mActivePointerId + ", resetting"); index = 0; mActivePointerId = ev.getPointerId(0); mInitialTouchX = ev.getX(); mInitialTouchY = mLastTouchY = ev.getY(); } final float x = ev.getX(index); final float y = ev.getY(index); if (!mIsDragging) { final float dy = y - mInitialTouchY; if (Math.abs(dy) > mTouchSlop && findChildUnder(x, y) != null) { handled = mIsDragging = true; mLastTouchY = Math.max(mLastTouchY - mTouchSlop, Math.min(mLastTouchY + dy, mLastTouchY + mTouchSlop)); } } if (mIsDragging) { final float dy = y - mLastTouchY; performDrag(dy); } mLastTouchY = y; } break; case MotionEvent.ACTION_POINTER_DOWN: { final int pointerIndex = ev.getActionIndex(); final int pointerId = ev.getPointerId(pointerIndex); mActivePointerId = pointerId; mInitialTouchX = ev.getX(pointerIndex); mInitialTouchY = mLastTouchY = ev.getY(pointerIndex); } break; case MotionEvent.ACTION_POINTER_UP: { onSecondaryPointerUp(ev); } break; case MotionEvent.ACTION_UP: { mIsDragging = false; if (!mIsDragging && findChildUnder(mInitialTouchX, mInitialTouchY) == null && findChildUnder(ev.getX(), ev.getY()) == null) { if (mClickOutsideListener != null) { mClickOutsideListener.onClick(this); resetTouch(); return true; } } if (mOpenOnClick && Math.abs(ev.getX() - mInitialTouchX) < mTouchSlop && Math.abs(ev.getY() - mInitialTouchY) < mTouchSlop) { smoothScrollTo(0, 0); return true; } mVelocityTracker.computeCurrentVelocity(1000); final float yvel = mVelocityTracker.getYVelocity(mActivePointerId); if (Math.abs(yvel) > mMinFlingVelocity) { smoothScrollTo(yvel < 0 ? 0 : mCollapsibleHeight, yvel); } else { smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0); } resetTouch(); } break; case MotionEvent.ACTION_CANCEL: { if (mIsDragging) { smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0); } resetTouch(); return true; } } return handled || (action == MotionEvent.ACTION_MOVE && mCollapseOffset > 0); }
From source file:org.witness.informacam.app.editors.image.ImageRegion.java
public boolean onTouch(View v, MotionEvent event) { fingerCount = event.getPointerCount(); // Log.v(LOGTAG,"onTouch: fingers=" + fingerCount); switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mImageEditor.doRealtimePreview = true; mImageEditor.updateDisplayImage(); //mTmpBounds = new RectF(mBounds); if (fingerCount == 1) { //float[] points = {event.getX(), event.getY()}; //iMatrix.mapPoints(points); //mStartPoint = new PointF(points[0],points[1]); mStartPoint = new PointF(event.getX(), event.getY()); //Log.v(LOGTAG,"startPoint: " + mStartPoint.x + " " + mStartPoint.y); }//from w ww . j av a 2 s . c om moved = false; return false; case MotionEvent.ACTION_POINTER_UP: Log.v(LOGTAG, "second finger removed - pointer up!"); return moved; case MotionEvent.ACTION_UP: mImageEditor.doRealtimePreview = true; mImageEditor.updateDisplayImage(); //mTmpBounds = null; return moved; case MotionEvent.ACTION_MOVE: if (fingerCount > 1) { float[] points = { event.getX(0), event.getY(0), event.getX(1), event.getY(1) }; iMatrix.mapPoints(points); mStartPoint = new PointF(points[0], points[1]); RectF newBox = new RectF(); newBox.left = Math.min(points[0], points[2]); newBox.top = Math.min(points[1], points[3]); newBox.right = Math.max(points[0], points[2]); newBox.bottom = Math.max(points[1], points[3]); moved = true; if (newBox.left != newBox.right && newBox.top != newBox.bottom) { updateBounds(newBox.left, newBox.top, newBox.right, newBox.bottom); } } else if (fingerCount == 1) { if (Math.abs(mStartPoint.x - event.getX()) > MIN_MOVE) { moved = true; float[] points = { mStartPoint.x, mStartPoint.y, event.getX(), event.getY() }; iMatrix.mapPoints(points); float diffX = points[0] - points[2]; float diffY = points[1] - points[3]; float left = 0, top = 0, right = 0, bottom = 0; if (cornerMode == CORNER_NONE) { left = mBounds.left - diffX; top = mBounds.top - diffY; right = mBounds.right - diffX; bottom = mBounds.bottom - diffY; } else if (cornerMode == CORNER_UPPER_LEFT) { left = mBounds.left - diffX; top = mBounds.top - diffY; right = mBounds.right; bottom = mBounds.bottom; } else if (cornerMode == CORNER_LOWER_LEFT) { left = mBounds.left - diffX; top = mBounds.top; right = mBounds.right; bottom = mBounds.bottom - diffY; } else if (cornerMode == CORNER_UPPER_RIGHT) { left = mBounds.left; top = mBounds.top - diffY; right = mBounds.right - diffX; bottom = mBounds.bottom; } else if (cornerMode == CORNER_LOWER_RIGHT) { left = mBounds.left; top = mBounds.top; right = mBounds.right - diffX; bottom = mBounds.bottom - diffY; } if ((left + CORNER_MAX) > right || (top + CORNER_MAX) > bottom) return false; //updateBounds(Math.min(left, right), Math.min(top,bottom), Math.max(left, right), Math.max(top, bottom)); updateBounds(left, top, right, bottom); mStartPoint = new PointF(event.getX(), event.getY()); } else { moved = false; } } mImageEditor.updateDisplayImage(); return true; } return false; }
From source file:org.que.activities.fragments.MapImageFragment.java
/** * The onTouch method which will be called if an onTouch event appears. * /*ww w.j av a2 s .co m*/ * @param view the view which was touched * @param event the touch event * @return true if the touch event was handled, false otherwise */ public boolean onTouch(View view, MotionEvent event) { Boolean consumed; ImageView image = (ImageView) view; switch (event.getAction() & MotionEvent.ACTION_MASK) //bitwise and with mask { case MotionEvent.ACTION_DOWN: saved.set(m); p.set(event.getX(), event.getY()); mode = TOUCH_MODE.DRAG; consumed = true; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = TOUCH_MODE.NONE; consumed = true; break; case MotionEvent.ACTION_POINTER_DOWN: saved.set(m); mode = TOUCH_MODE.ZOOM; oldDis = euclideanDistance(event); p = calcMidPoint(p, event); consumed = true; break; case MotionEvent.ACTION_MOVE: if (mode == TOUCH_MODE.ZOOM) { m.set(saved); newDis = euclideanDistance(event); scale = newDis / oldDis; m.postScale(scale, scale, p.x, p.y); float[] values = new float[9]; m.getValues(values); float mscale = values[Matrix.MSCALE_X]; if (mscale < MIN_ZOOM) { m.setScale(MIN_ZOOM, MIN_ZOOM, p.x, p.y); } else if (mscale > MAX_ZOOM) { m.setScale(MAX_ZOOM, MAX_ZOOM, p.x, p.y); } } else if (mode == TOUCH_MODE.DRAG) { m.set(saved); m.postTranslate(event.getX() - p.x, event.getY() - p.y); } consumed = true; break; default: consumed = false; } image.setImageMatrix(m); return consumed; }
From source file:com.tasomaniac.openwith.resolver.ResolverDrawerLayout.java
@Override public boolean onTouchEvent(MotionEvent ev) { final int action = MotionEventCompat.getActionMasked(ev); mVelocityTracker.addMovement(ev);// ww w. j a va2 s. co m boolean handled = false; switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(0); final float y = ev.getY(0); mInitialTouchX = x; mInitialTouchY = mLastTouchY = y; mActivePointerId = ev.getPointerId(0); final boolean hitView = findChildUnder(mInitialTouchX, mInitialTouchY) != null; handled = (!hitView && mOnDismissedListener != null) || mCollapsibleHeight > 0; mIsDragging = hitView && handled; abortAnimation(); } break; case MotionEvent.ACTION_MOVE: { int index = ev.findPointerIndex(mActivePointerId); if (index < 0) { Timber.e("Bad pointer id %d, resetting", mActivePointerId); index = 0; mActivePointerId = ev.getPointerId(0); mInitialTouchX = ev.getX(0); mInitialTouchY = mLastTouchY = ev.getY(0); } final float x = ev.getX(index); final float y = ev.getY(index); if (!mIsDragging) { final float dy = y - mInitialTouchY; if (Math.abs(dy) > mTouchSlop && findChildUnder(x, y) != null) { handled = mIsDragging = true; mLastTouchY = Math.max(mLastTouchY - mTouchSlop, Math.min(mLastTouchY + dy, mLastTouchY + mTouchSlop)); } } if (mIsDragging) { final float dy = y - mLastTouchY; performDrag(dy); } mLastTouchY = y; } break; case MotionEvent.ACTION_POINTER_DOWN: { final int pointerIndex = MotionEventCompat.getActionIndex(ev); mActivePointerId = ev.getPointerId(pointerIndex); mInitialTouchX = ev.getX(pointerIndex); mInitialTouchY = mLastTouchY = ev.getY(pointerIndex); } break; case MotionEvent.ACTION_POINTER_UP: { onSecondaryPointerUp(ev); } break; case MotionEvent.ACTION_UP: { final boolean wasDragging = mIsDragging; mIsDragging = false; if (!wasDragging && findChildUnder(mInitialTouchX, mInitialTouchY) == null && findChildUnder(ev.getX(0), ev.getY(0)) == null) { if (mOnDismissedListener != null) { dispatchOnDismissed(); resetTouch(); return true; } } if (mOpenOnClick && Math.abs(ev.getX(0) - mInitialTouchX) < mTouchSlop && Math.abs(ev.getY(0) - mInitialTouchY) < mTouchSlop) { smoothScrollTo(0, 0); return true; } mVelocityTracker.computeCurrentVelocity(1000); final float yvel = VelocityTrackerCompat.getYVelocity(mVelocityTracker, mActivePointerId); if (Math.abs(yvel) > mMinFlingVelocity) { if (mOnDismissedListener != null && yvel > 0 && mCollapseOffset > mCollapsibleHeight) { smoothScrollTo(mCollapsibleHeight + mUncollapsibleHeight, yvel); mDismissOnScrollerFinished = true; } else { smoothScrollTo(yvel < 0 ? 0 : mCollapsibleHeight, yvel); } } else { smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0); } resetTouch(); } break; case MotionEvent.ACTION_CANCEL: { if (mIsDragging) { smoothScrollTo(mCollapseOffset < mCollapsibleHeight / 2 ? 0 : mCollapsibleHeight, 0); } resetTouch(); return true; } } return handled; }
From source file:com.google.blockly.android.ui.VirtualWorkspaceView.java
@Override public boolean onTouchEvent(MotionEvent event) { mScaleGestureDetector.onTouchEvent(event); if (mScaleGestureDetector.isInProgress()) { // If the scale gesture detector is handling a scale-and-pan gesture, then exit here // since otherwise we would also be generating dragging events below. return true; }/*from w w w . ja v a2 s. c o m*/ final int action = MotionEventCompat.getActionMasked(event); switch (action) { case MotionEvent.ACTION_DOWN: { clearFocus(); mImeManager.hideSoftInputFromWindow(getWindowToken(), 0); if (mScrollable) { final int pointerIdx = MotionEventCompat.getActionIndex(event); mPanningPointerId = MotionEventCompat.getPointerId(event, pointerIdx); mPanningStart.set((int) MotionEventCompat.getX(event, pointerIdx), (int) MotionEventCompat.getY(event, pointerIdx)); mOriginalScrollX = getScrollX(); mOriginalScrollY = getScrollY(); } return true; } case MotionEvent.ACTION_MOVE: { if (mPanningPointerId != MotionEvent.INVALID_POINTER_ID) { final int pointerIdx = MotionEventCompat.findPointerIndex(event, mPanningPointerId); if (pointerIdx == -1) { // TODO: (#319) remove when we clean up multi-touch handling. Log.w(TAG, "Got an invalid pointer idx for the panning pointer."); return false; } scrollTo(mOriginalScrollX + mPanningStart.x - (int) MotionEventCompat.getX(event, pointerIdx), mOriginalScrollY + mPanningStart.y - (int) MotionEventCompat.getY(event, pointerIdx)); return true; } else { return false; } } case MotionEvent.ACTION_POINTER_UP: { // Some pointer went up - check whether it was the one used for panning. final int pointerIdx = MotionEventCompat.getActionIndex(event); final int pointerId = MotionEventCompat.getPointerId(event, pointerIdx); if (pointerId != mPanningPointerId) { return false; } // Pointer that went up was used for panning - treat like ACTION_UP. // FALLTHROUGH INTENDED. } case MotionEvent.ACTION_UP: { if (mPanningPointerId != MotionEvent.INVALID_POINTER_ID) { mPanningPointerId = MotionEvent.INVALID_POINTER_ID; return true; } else { return false; } } case MotionEvent.ACTION_CANCEL: { if (mPanningPointerId != MotionEvent.INVALID_POINTER_ID) { // When cancelled, reset to original scroll position. scrollTo(mOriginalScrollX, mOriginalScrollY); mPanningPointerId = MotionEvent.INVALID_POINTER_ID; return true; } else { return false; } } default: { break; } } return false; }
From source file:com.csounds.examples.tests.SynthActivity.java
/** Called when the activity is first created. */ @Override/*from w w w. ja v a 2 s.c o m*/ public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); for (int i = 0; i < touchIds.length; i++) { touchIds[i] = -1; touchX[i] = -1; touchY[i] = -1; } multiTouchView = new View(this); setContentView(R.layout.synth2); multiTouchView = (RelativeLayout) findViewById(R.id.synth); octPlusButton = (Button) findViewById(R.id.plusoctavebutton); octMinusButton = (Button) findViewById(R.id.minusoctavebutton); diode4 = (ToggleButton) findViewById(R.id.diode4); diode3 = (ToggleButton) findViewById(R.id.diode3); LcdScreenView lcd = (LcdScreenView) findViewById(R.id.lcd_screen); new DrumMachineLcdUpdater(lcd.getModel()); this.createLoadListener((View) lcd); keyboardLayout = (RelativeLayout) findViewById(R.id.keyboard); keyboardLayout.setOnTouchListener(new OnTouchListener() { /* (non-Javadoc) * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent) */ public boolean onTouch(View v, MotionEvent event) { final int action = event.getAction() & MotionEvent.ACTION_MASK; float[] touchArrayDown = new float[2]; switch (action) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_POINTER_DOWN: for (int i = 0; i < event.getPointerCount(); i++) { int pointerId = event.getPointerId(i); int id = getTouchId(pointerId); if (id == -1) { id = getTouchIdAssignment(); if (id != -1) { touchIds[id] = pointerId; touchX[id] = event.getX(i) / keyboardLayout.getWidth(); touchY[id] = 1 - (event.getY(i) / keyboardLayout.getHeight()); //TODO calculte interval Y if (touchXPtr[id] != null) { //TODO swtich touchid // Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id] + " multitouchview width " + multiTouchView.getWidth() + " multitouchview width " + multiTouchView.getHeight()); // Log.d("touchXtouchY","keyboardLayout width " + keyboardLayout.getWidth() + " keyboardLayout width " + keyboardLayout.getHeight()); // get the key from the touch coordinates float[] touchArray = new float[2]; touchArray = evaluateTouchKey(touchX[id], touchY[id]); touchArrayDown[0] = touchX[id]; touchArrayDown[1] = touchY[id]; touchX[id] = touchArray[0]; touchY[id] = touchArray[1]; // Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]); touchXPtr[id].SetValue(0, touchX[id]); touchYPtr[id].SetValue(0, touchY[id]); csoundObj.sendScore(String.format("i1.%d 0 -2 %d", id, id)); } } } } break; case MotionEvent.ACTION_MOVE: for (int i = 0; i < event.getPointerCount(); i++) { int pointerId = event.getPointerId(i); int id = getTouchId(pointerId); if (id != -1) { touchIds[id] = pointerId; touchX[id] = event.getX(i) / keyboardLayout.getWidth(); touchY[id] = 1 - (event.getY(i) / keyboardLayout.getHeight()); //TODO calculte interval Y if (touchXPtr[id] != null) { //TODO swtich touchid // Log.d("aaa","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id] + " multitouchview width " + multiTouchView.getWidth() + " multitouchview width " + multiTouchView.getHeight()); // Log.d("aaa","keyboardLayout width " + keyboardLayout.getWidth() + " keyboardLayout width " + keyboardLayout.getHeight()); // get the key from the touch coordinates float[] touchArray = new float[2]; touchArray = evaluateTouchKey(touchX[id], touchY[id]); touchX[id] = touchArray[0]; touchY[id] = touchArray[1]; // Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]); touchXPtr[id].SetValue(0, touchX[id]); touchYPtr[id].SetValue(0, touchY[id]); csoundObj.sendScore(String.format("i1.%d 0 -2 %d", id, id)); } } } break; case MotionEvent.ACTION_POINTER_UP: case MotionEvent.ACTION_UP: { int activePointerIndex = event.getActionIndex(); int pointerId = event.getPointerId(activePointerIndex); int id = getTouchId(pointerId); if (id != -1) { touchIds[id] = -1; csoundObj.sendScore(String.format("i-1.%d 0 0 %d", id, id)); } //write file /* System.out.println(String.format( "i1.%d 0 -2 %d", id, id)); writeToFile(String.format( "i1.%d 0 -2 %d", id, id)); */ } break; } return true; } }); OnTouchListener octBtnOk = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (octNbr < 12) octNbr += 12; break; } return false; } }; octPlusButton.setOnTouchListener(octBtnOk); OnTouchListener octBtnMinus = new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (octNbr > -36) octNbr -= 12; break; } return false; } }; octMinusButton.setOnTouchListener(octBtnMinus); String csd = getResourceFileAsString(R.raw.multitouch_xy_kx); File f = createTempFile(csd); csoundObj.addBinding(this); csoundObj.startCsound(f); // initknobs(); initSeekBar(diode3, diode4); }
From source file:com.dgnt.dominionCardPicker.view.DynamicListView.java
@Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mDownX = (int) event.getX(); mDownY = (int) event.getY(); mActivePointerId = event.getPointerId(0); break;/* w w w. ja v a 2 s.c om*/ case MotionEvent.ACTION_MOVE: if (mActivePointerId == INVALID_POINTER_ID) { break; } int pointerIndex = event.findPointerIndex(mActivePointerId); mLastEventY = (int) event.getY(pointerIndex); int deltaY = mLastEventY - mDownY; if (mCellIsMobile) { mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left, mHoverCellOriginalBounds.top + deltaY + mTotalOffset); mHoverCell.setBounds(mHoverCellCurrentBounds); invalidate(); handleCellSwitch(); mIsMobileScrolling = false; handleMobileCellScroll(); return false; } break; case MotionEvent.ACTION_UP: touchEventsEnded(); break; case MotionEvent.ACTION_CANCEL: touchEventsCancelled(); break; case MotionEvent.ACTION_POINTER_UP: /* If a multitouch event took place and the original touch dictating * the movement of the hover cell has ended, then the dragging event * ends and the hover cell is animated to its corresponding position * in the listview. */ pointerIndex = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; final int pointerId = event.getPointerId(pointerIndex); if (pointerId == mActivePointerId) { touchEventsEnded(); } break; default: break; } return super.onTouchEvent(event); }
From source file:com.nextgis.maplibui.fragment.ReorderedLayerView.java
@Override public boolean onTouchEvent(@NonNull MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mDownX = (int) event.getX(); mDownY = (int) event.getY(); mActivePointerId = event.getPointerId(0); break;//w ww . jav a 2 s . c om case MotionEvent.ACTION_MOVE: if (mActivePointerId == NOT_FOUND) { break; } int pointerIndex = event.findPointerIndex(mActivePointerId); mLastEventY = (int) event.getY(pointerIndex); int deltaY = mLastEventY - mDownY; if (mCellIsMobile) { int top = mHoverCellOriginalBounds.top + deltaY + mTotalOffset; mHoverCellCurrentBounds.offsetTo(mHoverCellOriginalBounds.left, top); mHoverCell.setBounds(mHoverCellCurrentBounds); invalidate(); handleCellSwitch(); mIsMobileScrolling = false; handleMobileCellScroll(); return false; } break; case MotionEvent.ACTION_UP: touchEventsEnded(); setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); ((LayersListAdapter) getAdapter()).notifyDataChanged(); break; case MotionEvent.ACTION_CANCEL: touchEventsCancelled(); setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); 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(); } setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); break; default: break; } return super.onTouchEvent(event); }
From source file:com.iangclifton.auid.horizontaliconview.HorizontalIconView.java
@Override public boolean onTouchEvent(MotionEvent ev) { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); }//w ww . ja v a 2s. c o m mVelocityTracker.addMovement(ev); final int action = MotionEventCompat.getActionMasked(ev); switch (action) { case MotionEvent.ACTION_DOWN: { if (!mScroller.isFinished()) { mScroller.abortAnimation(); } // Remember where the motion event started mPreviousX = (int) MotionEventCompat.getX(ev, 0); mActivePointerId = MotionEventCompat.getPointerId(ev, 0); break; } case MotionEvent.ACTION_MOVE: { final int activePointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId); if (activePointerIndex == INVALID_POINTER) { Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent"); break; } final int x = (int) MotionEventCompat.getX(ev, 0); int deltaX = (int) (mPreviousX - x); if (!mIsBeingDragged && Math.abs(deltaX) > mTouchSlop) { mIsBeingDragged = true; if (deltaX > 0) { deltaX -= mTouchSlop; } else { deltaX += mTouchSlop; } } if (mIsBeingDragged) { // Scroll to follow the motion event mPreviousX = x; final int oldX = getScrollX(); final int range = mScrollRange; if (overScrollBy(deltaX, 0, oldX, 0, range, 0, mOverscrollDistance, 0, true)) { // Break our velocity if we hit a scroll barrier. mVelocityTracker.clear(); } if (mEdgeEffectLeft != null) { final int pulledToX = oldX + deltaX; if (pulledToX < 0) { mEdgeEffectLeft.onPull((float) deltaX / getWidth()); if (!mEdgeEffectRight.isFinished()) { mEdgeEffectRight.onRelease(); } } else if (pulledToX > range) { mEdgeEffectRight.onPull((float) deltaX / getWidth()); if (!mEdgeEffectLeft.isFinished()) { mEdgeEffectLeft.onRelease(); } } if (!mEdgeEffectLeft.isFinished() || !mEdgeEffectRight.isFinished()) { postInvalidateOnAnimation(); } } } break; } case MotionEvent.ACTION_UP: { if (mIsBeingDragged) { mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); int initialVelocity = (int) mVelocityTracker.getXVelocity(mActivePointerId); if ((Math.abs(initialVelocity) > mMinimumVelocity)) { fling(-initialVelocity); } else { if (mScroller.springBack(getScrollX(), 0, 0, mScrollRange, 0, 0)) { postInvalidateOnAnimation(); } } mActivePointerId = INVALID_POINTER; mIsBeingDragged = false; mVelocityTracker.recycle(); mVelocityTracker = null; if (mEdgeEffectLeft != null) { mEdgeEffectLeft.onRelease(); mEdgeEffectRight.onRelease(); } } else { // Was not being dragged, was this a press on an icon? final int activePointerIndex = ev.findPointerIndex(mActivePointerId); if (activePointerIndex == INVALID_POINTER) { return false; } final int x = (int) ev.getX(activePointerIndex) + getScrollX(); final int y = (int) ev.getY(activePointerIndex); int i = 0; for (Rect rect : mIconPositions) { if (rect.contains(x, y)) { final int position = i + mSkippedIconCount; Toast.makeText(getContext(), "Pressed icon " + position + "; rect count: " + mIconPositions.size(), Toast.LENGTH_SHORT).show(); break; } i++; } } break; } case MotionEvent.ACTION_CANCEL: { if (mIsBeingDragged) { if (mScroller.springBack(getScrollX(), 0, 0, mScrollRange, 0, 0)) { postInvalidateOnAnimation(); } mActivePointerId = INVALID_POINTER; mIsBeingDragged = false; if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } if (mEdgeEffectLeft != null) { mEdgeEffectLeft.onRelease(); mEdgeEffectRight.onRelease(); } } break; } case MotionEvent.ACTION_POINTER_UP: { onSecondaryPointerUp(ev); break; } } return true; }
From source file:it.sephiroth.android.library.imagezoom.ScaleGestureDetector.java
public boolean onTouchEvent(final MotionEvent event) { final int action = event.getAction(); final boolean handled = true; if (!mGestureInProgress) { switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: { // We have a new multi-finger gesture // as orientation can change, query the metrics in touch // down final DisplayMetrics metrics = mContext.getResources().getDisplayMetrics(); mRightSlopEdge = metrics.widthPixels - mEdgeSlop; mBottomSlopEdge = metrics.heightPixels - mEdgeSlop; // Be paranoid in case we missed an event reset();// w w w.j a va 2 s. com mPrevEvent = MotionEvent.obtain(event); mTimeDelta = 0; setContext(event); // Check if we have a sloppy gesture. If so, delay // the beginning of the gesture until we're sure that's // what the user wanted. Sloppy gestures can happen if the // edge of the user's hand is touching the screen, for // example. final float edgeSlop = mEdgeSlop; final float rightSlop = mRightSlopEdge; final float bottomSlop = mBottomSlopEdge; final float x0 = event.getRawX(); final float y0 = event.getRawY(); final float x1 = getRawX(event, 1); final float y1 = getRawY(event, 1); final boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop || x0 > rightSlop || y0 > bottomSlop; final boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop || x1 > rightSlop || y1 > bottomSlop; if (p0sloppy && p1sloppy) { mFocusX = -1; mFocusY = -1; mSloppyGesture = true; } else if (p0sloppy) { mFocusX = MotionEventCompat.getX(event, 1); mFocusY = MotionEventCompat.getY(event, 1); mSloppyGesture = true; } else if (p1sloppy) { mFocusX = MotionEventCompat.getX(event, 0); mFocusY = MotionEventCompat.getY(event, 0); mSloppyGesture = true; } else { mGestureInProgress = mListener.onScaleBegin(this); } } break; case MotionEvent.ACTION_MOVE: if (mSloppyGesture) { // Initiate sloppy gestures if we've moved outside of // the slop area. final float edgeSlop = mEdgeSlop; final float rightSlop = mRightSlopEdge; final float bottomSlop = mBottomSlopEdge; final float x0 = event.getRawX(); final float y0 = event.getRawY(); final float x1 = getRawX(event, 1); final float y1 = getRawY(event, 1); final boolean p0sloppy = x0 < edgeSlop || y0 < edgeSlop || x0 > rightSlop || y0 > bottomSlop; final boolean p1sloppy = x1 < edgeSlop || y1 < edgeSlop || x1 > rightSlop || y1 > bottomSlop; if (p0sloppy && p1sloppy) { mFocusX = -1; mFocusY = -1; } else if (p0sloppy) { mFocusX = MotionEventCompat.getX(event, 1); mFocusY = MotionEventCompat.getY(event, 1); } else if (p1sloppy) { mFocusX = MotionEventCompat.getX(event, 0); mFocusY = MotionEventCompat.getY(event, 0); } else { mSloppyGesture = false; mGestureInProgress = mListener.onScaleBegin(this); } } break; case MotionEvent.ACTION_POINTER_UP: if (mSloppyGesture) { // Set focus point to the remaining finger final int id = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT == 0 ? 1 : 0; mFocusX = event.getX(id); mFocusY = event.getY(id); } break; } } else { // Transform gesture in progress - attempt to handle it switch (action & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_UP: // Gesture ended setContext(event); // Set focus point to the remaining finger final int id = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT == 0 ? 1 : 0; mFocusX = event.getX(id); mFocusY = event.getY(id); if (!mSloppyGesture) { mListener.onScaleEnd(this); } reset(); break; case MotionEvent.ACTION_CANCEL: if (!mSloppyGesture) { mListener.onScaleEnd(this); } reset(); break; case MotionEvent.ACTION_MOVE: setContext(event); // Only accept the event if our relative pressure is within // a certain limit - this can help filter shaky data as a // finger is lifted. if (mCurrPressure / mPrevPressure > PRESSURE_THRESHOLD) { final boolean updatePrevious = mListener.onScale(this); if (updatePrevious) { mPrevEvent.recycle(); mPrevEvent = MotionEvent.obtain(event); } } break; } } return handled; }