List of usage examples for android.view MotionEvent getActionMasked
public final int getActionMasked()
From source file:de.vanita5.twittnuker.activity.support.ComposeActivity.java
@Override public boolean onTouchEvent(final MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: { getWindow().getDecorView().getHitRect(mWindowDecorHitRect); if (!mWindowDecorHitRect.contains(Math.round(event.getX()), Math.round(event.getY()))) { onBackPressed();// w w w .ja v a2 s . c o m return true; } break; } } return super.onTouchEvent(event); }
From source file:research.sg.edu.edapp.kb.KbSoftKeyboard.java
/** * Called by the framework when your view for creating input needs to * be generated. This will be called the first time your input method * is displayed, and every time it needs to be re-created such as due to * a configuration change.// w w w . j a va2s.c o m */ @Override public View onCreateInputView() { mInputView = (KbLatinKeyboardView) getLayoutInflater().inflate(R.layout.input, null); mInputView.setKeyboard(mQwertyKeyboard); mInputView.setOnKeyboardActionListener(this); /****************************************************edit 2**************************************************************/ featuredb = new DataDBHelper(this); KbSoftKeyboard gd = new KbSoftKeyboard(); sf = new Save_feature(); swipeWord = new GetSwipeWord(this); //gDetect.setOnDoubleTapListener(gd); mInputView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { int index = event.getActionIndex(); int action = event.getActionMasked(); int pointerId = event.getPointerId(index); //check for actions of motion event if (event.getAction() == MotionEvent.ACTION_DOWN) { //retrieve key at current char ch = getkeylabel(event); if (ch > 0) swipe += ch; // test+=swipe.charAt(0); //set up start timer for measuring duration // start=System.currentTimeMillis(); //setup velocity tracker if (mvel == null) { // Retrieve a new VelocityTracker object to watch the velocity of a motion. mvel = VelocityTracker.obtain(); } else { // Reset the velocity tracker back to its initial state. mvel.clear(); } } if (event.getAction() == MotionEvent.ACTION_MOVE) { mvel.addMovement(event); mvel.computeCurrentVelocity(1000); // Log velocity of pixels per second // Best practice to use VelocityTrackerCompat where possible. x_vel += abs(VelocityTrackerCompat.getXVelocity(mvel, pointerId)); y_vel += abs(VelocityTrackerCompat.getYVelocity(mvel, pointerId)); n_event += 1; // Log.d("", "X velocity: " + x_vel); // Log.d("", "Y velocity: " + y_vel); } if (event.getAction() == MotionEvent.ACTION_UP) { //record time when finger lifted up // end=System.currentTimeMillis(); //calculate duration // duration=(end-start)/100; //calculate velocity pixels per sec // Log.d("", "X velocity: " + x_vel); // Log.d("", "Y velocity: " + y_vel); velocity = Math.sqrt(x_vel * x_vel + y_vel * y_vel); //obtain pressure pressure += event.getPressure(); np_event += 1; swipe2 = get_final_string(swipe); swipe = ""; if (swipe2 == null) swipe2 = ""; //print generated string System.out.println(swipe + "\n 2nd " + swipe2); } if (((int) old_x) == 0 & ((int) old_y) == 0) { old_x = event.getX(); old_y = event.getY(); swipe += getkeylabel(event); } else if (((int) olddir_x) == 0 & ((int) olddir_y) == 0) { olddir_x = event.getX() - old_x; olddir_y = event.getY() - old_y; old_x = event.getX(); old_y = event.getY(); } else { check_change(event); } // Return false to avoid consuming the touch event return false; } }); //***********************************************edit 2 end***************************************************************** //Saved my life if (mInputView.isPreviewEnabled()) mInputView.setPreviewEnabled(false); return mInputView; }
From source file:com.achep.acdisplay.ui.fragments.AcDisplayFragment.java
@Override public void onForwardedEvent(MotionEvent event, int activePointerId) { int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_DOWN: // Track the velocity of movement, so we // can do swipe-to-dismiss. mVelocityTracker = VelocityTracker.obtain(); mTouchSticky = false;//w w w . j a va 2 s. c o m case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_UP: populateStdMotion(event); if (action != MotionEvent.ACTION_UP) { return; // Don't fall down. } boolean dismissing = swipeToDismiss(); if (!dismissing) { if (mTouchSticky) { // Disable the default timeout mechanism and let // the selected widget to stay for a while. onWidgetStick(mSelectedWidget); } else if (mPressedIconView == null || !isPinnable()) { showHomeWidget(); } else { onWidgetPin(mSelectedWidget); } } case MotionEvent.ACTION_CANCEL: mTouchHandler.removeCallbacksAndMessages(null); mVelocityTracker.recycle(); mVelocityTracker = null; mTouchSticky = false; if (action == MotionEvent.ACTION_CANCEL) { showHomeWidget(); } break; } }
From source file:com.cyanogenmod.filemanager.ui.widgets.ViewDragHelper.java
/** * Check if this event as provided to the parent view's onInterceptTouchEvent should * cause the parent to intercept the touch event stream. * * @param ev MotionEvent provided to onInterceptTouchEvent * @return true if the parent view should return true from onInterceptTouchEvent *///from w w w . j av a 2s. c om public boolean shouldInterceptTouchEvent(MotionEvent ev) { final int action = ev.getActionMasked(); final int actionIndex = ev.getActionIndex(); if (action == MotionEvent.ACTION_DOWN) { // Reset things for a new event stream, just in case we didn't get // the whole previous stream. cancel(); } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); final int pointerId = ev.getPointerId(0); saveInitialMotion(x, y, pointerId); final View toCapture = findTopChildUnder((int) x, (int) y); // Catch a settling view if possible. if (toCapture == mCapturedView && mDragState == STATE_SETTLING) { tryCaptureViewForDrag(toCapture, pointerId); } final int edgesTouched = mInitialEdgesTouched[pointerId]; if ((edgesTouched & mTrackingEdges) != 0) { mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); } break; } case MotionEvent.ACTION_POINTER_DOWN: { final int pointerId = ev.getPointerId(actionIndex); final float x = ev.getX(actionIndex); final float y = ev.getY(actionIndex); saveInitialMotion(x, y, pointerId); // A ViewDragHelper can only manipulate one view at a time. if (mDragState == STATE_IDLE) { final int edgesTouched = mInitialEdgesTouched[pointerId]; if ((edgesTouched & mTrackingEdges) != 0) { mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); } } else if (mDragState == STATE_SETTLING) { // Catch a settling view if possible. final View toCapture = findTopChildUnder((int) x, (int) y); if (toCapture == mCapturedView) { tryCaptureViewForDrag(toCapture, pointerId); } } break; } case MotionEvent.ACTION_MOVE: { // First to cross a touch slop over a draggable view wins. Also report edge drags. final int pointerCount = ev.getPointerCount(); for (int i = 0; i < pointerCount; i++) { final int pointerId = ev.getPointerId(i); final float x = ev.getX(i); final float y = ev.getY(i); final float dx = x - mInitialMotionX[pointerId]; final float dy = y - mInitialMotionY[pointerId]; reportNewEdgeDrags(dx, dy, pointerId); if (mDragState == STATE_DRAGGING) { // Callback might have started an edge drag break; } final View toCapture = findTopChildUnder((int) x, (int) y); if (toCapture != null && checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) { break; } } saveLastMotion(ev); break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerId = ev.getPointerId(actionIndex); clearMotionHistory(pointerId); break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { cancel(); break; } } return mDragState == STATE_DRAGGING; }
From source file:com.mishiranu.dashchan.ui.navigator.DrawerForm.java
@Override public boolean onTouch(View v, MotionEvent event) { switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: { multipleFingersCountingTime = false; multipleFingersStartTime = 0L;//ww w . j a va 2 s . c o m multipleFingersTime = 0L; break; } case MotionEvent.ACTION_POINTER_DOWN: { if (!multipleFingersCountingTime) { multipleFingersCountingTime = true; multipleFingersStartTime = System.currentTimeMillis(); } break; } case MotionEvent.ACTION_POINTER_UP: { if (event.getPointerCount() <= 2) { if (multipleFingersCountingTime) { multipleFingersCountingTime = false; multipleFingersTime += System.currentTimeMillis() - multipleFingersStartTime; } } break; } } return false; }
From source file:com.mylikes.likes.etchasketch.Slate.java
@SuppressLint("NewApi") @Override/*ww w. j ava 2 s .c o m*/ public boolean onTouchEvent(MotionEvent event) { final int action = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) ? event.getActionMasked() : event.getAction(); int N = event.getHistorySize(); int P = event.getPointerCount(); long time = event.getEventTime(); mEmpty = false; // starting a new touch? commit the previous state of the canvas if (action == MotionEvent.ACTION_DOWN) { commitStroke(); } if (mZoomMode) { return false; } int pointerIndex = MotionEventCompat.getActionIndex(event); int pointerId = event.getPointerId(pointerIndex); if (moveMode) { if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN) { if (firstFinger != null) { MotionEvent.PointerCoords coords1 = new MotionEvent.PointerCoords(); event.getPointerCoords(0, coords1); Log.d(TAG, "coords1: " + coords1.x + " " + coords1.y); MotionEvent.PointerCoords coords2 = new MotionEvent.PointerCoords(); event.getPointerCoords(1, coords2); firstFinger.set(coords1.x, coords1.y); secondFinger = new PointF(coords2.x, coords2.y); } else { touchStartTime = System.currentTimeMillis(); moveDrawingStartX = event.getX(); moveDrawingStartY = event.getY(); int i = 0; moveDrawingIndex = -1; resizeDrawingIndex = -1; resizeDrawingCorner = null; if (selectedDrawing != null) { moveDrawingIndex = 0; } if (i >= overlays.size()) { return true; } Log.d(TAG, "Start dragging overlay"); firstFinger = new PointF(event.getX(), event.getY()); } return true; } else if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { if (secondFinger != null) { secondFinger = null; MotionEvent.PointerCoords coords1 = new MotionEvent.PointerCoords(); event.getPointerCoords(0, coords1); moveDrawingStartX = coords1.x; moveDrawingStartY = coords1.y; return true; } if (firstFinger != null) { firstFinger = null; } if (moveDrawingIndex == -1 && resizeDrawingIndex == -1 && System.currentTimeMillis() - touchStartTime < 400 && Math.abs(event.getX() - moveDrawingStartX) + Math.abs(event.getY() - moveDrawingStartY) < 8) { if (resizeDrawingCorner != null && selectedDrawing != null) { if (resizeDrawingCorner == "tl" && selectedDrawing instanceof TextDrawing) { //promptForText((TextDrawing)selectedDrawing); } else if (resizeDrawingCorner == "tr") { //maybeRemoveDrawing(selectedDrawing); } } else { //promptForText((int) event.getX(), (int) event.getY()); } } moveDrawingIndex = -1; Log.d(TAG, "Stop dragging overlay"); // TODO: add to undo stack return true; } else if (firstFinger != null && secondFinger != null && action == MotionEvent.ACTION_MOVE) { MotionEvent.PointerCoords coords1 = new MotionEvent.PointerCoords(); event.getPointerCoords(0, coords1); Log.d(TAG, "coords1: " + coords1.x + " " + coords1.y); MotionEvent.PointerCoords coords2 = new MotionEvent.PointerCoords(); event.getPointerCoords(1, coords2); Log.d(TAG, "coords2: " + coords2.x + " " + coords2.y); float xDist = firstFinger.x - secondFinger.x, yDist = firstFinger.y - secondFinger.y; float origAngle = (float) Math.atan2(yDist, xDist); if (origAngle < 0) origAngle += Math.PI * 2; float lastDistance = (float) Math.sqrt(xDist * xDist + yDist * yDist); xDist = coords2.x - coords1.x; yDist = coords2.y - coords1.y; float newDistance = (float) Math.sqrt(xDist * xDist + yDist * yDist); float newAngle = (float) Math.atan2(yDist, xDist); if (newAngle < 0) newAngle += Math.PI * 2; if (newAngle - origAngle > Math.PI / 2) { origAngle += Math.PI; } else if (origAngle - newAngle > Math.PI / 2) { newAngle += Math.PI; } firstFinger.set(coords1.x, coords1.y); secondFinger = new PointF(coords2.x, coords2.y); if (selectedDrawing != null) { selectedDrawing.resizeBy(newDistance / lastDistance); selectedDrawing.rotateBy(newAngle - origAngle); invalidate(); } } else if (moveDrawingIndex >= 0 && moveDrawingIndex < overlays.size() && action == MotionEvent.ACTION_MOVE) { float x = event.getX(); float y = event.getY(); overlays.get(moveDrawingIndex).moveBy((int) ((x - moveDrawingStartX) * getDrawingDensity()), (int) ((y - moveDrawingStartY) * getDrawingDensity())); // TODO: only invalidate relevant Rect invalidate(); moveDrawingStartX = x; moveDrawingStartY = y; return true; } else if (resizeDrawingIndex >= 0 && resizeDrawingIndex < overlays.size() && action == MotionEvent.ACTION_MOVE) { float x = event.getX(); float y = event.getY(); overlays.get(resizeDrawingIndex).resizeCorner(resizeDrawingCorner, (int) (x - moveDrawingStartX), (int) (y - moveDrawingStartY)); // TODO: only invalidate relevant Rect invalidate(); moveDrawingStartX = x; moveDrawingStartY = y; return true; } return false; } if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN || action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { int j = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) ? event.getActionIndex() : 0; mTmpSpot.update(event.getX(j), event.getY(j), event.getSize(j), event.getPressure(j) + event.getSize(j), time, getToolTypeCompat(event, j)); mStrokes[event.getPointerId(j)].add(mTmpSpot); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_POINTER_UP) { mStrokes[event.getPointerId(j)].finish(time); } } else if (action == MotionEvent.ACTION_MOVE) { if (dbgX >= 0) { dbgRect.set(dbgX - 1, dbgY - 1, dbgX + 1, dbgY + 1); } for (int i = 0; i < N; i++) { for (int j = 0; j < P; j++) { mTmpSpot.update(event.getHistoricalX(j, i), event.getHistoricalY(j, i), event.getHistoricalSize(j, i), event.getHistoricalPressure(j, i) + event.getHistoricalSize(j, i), event.getHistoricalEventTime(i), getToolTypeCompat(event, j)); if ((mDebugFlags & FLAG_DEBUG_STROKES) != 0) { if (dbgX >= 0) { //mTiledCanvas.drawLine(dbgX, dbgY, mTmpSpot.x, mTmpSpot.y, mDebugPaints[3]); } dbgX = mTmpSpot.x; dbgY = mTmpSpot.y; dbgRect.union(dbgX - 1, dbgY - 1, dbgX + 1, dbgY + 1); } mStrokes[event.getPointerId(j)].add(mTmpSpot); } } for (int j = 0; j < P; j++) { mTmpSpot.update(event.getX(j), event.getY(j), event.getSize(j), event.getPressure(j) + event.getSize(j), time, getToolTypeCompat(event, j)); if ((mDebugFlags & FLAG_DEBUG_STROKES) != 0) { if (dbgX >= 0) { //mTiledCanvas.drawLine(dbgX, dbgY, mTmpSpot.x, mTmpSpot.y, mDebugPaints[3]); } dbgX = mTmpSpot.x; dbgY = mTmpSpot.y; dbgRect.union(dbgX - 1, dbgY - 1, dbgX + 1, dbgY + 1); } mStrokes[event.getPointerId(j)].add(mTmpSpot); } if ((mDebugFlags & FLAG_DEBUG_STROKES) != 0) { Rect dirty = new Rect(); dbgRect.roundOut(dirty); invalidate(dirty); } } if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) { for (int j = 0; j < P; j++) { mStrokes[event.getPointerId(j)].finish(time); } dbgX = dbgY = -1; } return true; }
From source file:com.cyanogenmod.filemanager.ui.widgets.ViewDragHelper.java
/** * Process a touch event received by the parent view. This method will dispatch callback events * as needed before returning. The parent view's onTouchEvent implementation should call this. * * @param ev The touch event received by the parent view *///from w w w . j a v a 2s . c o m public void processTouchEvent(MotionEvent ev) { final int action = ev.getActionMasked(); final int actionIndex = ev.getActionIndex(); if (action == MotionEvent.ACTION_DOWN) { // Reset things for a new event stream, just in case we didn't get // the whole previous stream. cancel(); } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); final int pointerId = ev.getPointerId(0); final View toCapture = findTopChildUnder((int) x, (int) y); saveInitialMotion(x, y, pointerId); // Since the parent is already directly processing this touch event, // there is no reason to delay for a slop before dragging. // Start immediately if possible. tryCaptureViewForDrag(toCapture, pointerId); final int edgesTouched = mInitialEdgesTouched[pointerId]; if ((edgesTouched & mTrackingEdges) != 0) { mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); } break; } case MotionEvent.ACTION_POINTER_DOWN: { final int pointerId = ev.getPointerId(actionIndex); final float x = ev.getX(actionIndex); final float y = ev.getY(actionIndex); saveInitialMotion(x, y, pointerId); // A ViewDragHelper can only manipulate one view at a time. if (mDragState == STATE_IDLE) { // If we're idle we can do anything! Treat it like a normal down event. final View toCapture = findTopChildUnder((int) x, (int) y); tryCaptureViewForDrag(toCapture, pointerId); final int edgesTouched = mInitialEdgesTouched[pointerId]; if ((edgesTouched & mTrackingEdges) != 0) { mCallback.onEdgeTouched(edgesTouched & mTrackingEdges, pointerId); } } else if (isCapturedViewUnder((int) x, (int) y)) { // We're still tracking a captured view. If the same view is under this // point, we'll swap to controlling it with this pointer instead. // (This will still work if we're "catching" a settling view.) tryCaptureViewForDrag(mCapturedView, pointerId); } break; } case MotionEvent.ACTION_MOVE: { if (mDragState == STATE_DRAGGING) { final int index = ev.findPointerIndex(mActivePointerId); final float x = ev.getX(index); final float y = ev.getY(index); final int idx = (int) (x - mLastMotionX[mActivePointerId]); final int idy = (int) (y - mLastMotionY[mActivePointerId]); dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy); saveLastMotion(ev); } else { // Check to see if any pointer is now over a draggable view. final int pointerCount = ev.getPointerCount(); for (int i = 0; i < pointerCount; i++) { final int pointerId = ev.getPointerId(i); final float x = ev.getX(i); final float y = ev.getY(i); final float dx = x - mInitialMotionX[pointerId]; final float dy = y - mInitialMotionY[pointerId]; reportNewEdgeDrags(dx, dy, pointerId); if (mDragState == STATE_DRAGGING) { // Callback might have started an edge drag. break; } final View toCapture = findTopChildUnder((int) x, (int) y); if (checkTouchSlop(toCapture, dx, dy) && tryCaptureViewForDrag(toCapture, pointerId)) { break; } } saveLastMotion(ev); } break; } case MotionEvent.ACTION_POINTER_UP: { final int pointerId = ev.getPointerId(actionIndex); if (mDragState == STATE_DRAGGING && pointerId == mActivePointerId) { // Try to find another pointer that's still holding on to the captured view. int newActivePointer = INVALID_POINTER; final int pointerCount = ev.getPointerCount(); for (int i = 0; i < pointerCount; i++) { final int id = ev.getPointerId(i); if (id == mActivePointerId) { // This one's going away, skip. continue; } final float x = ev.getX(i); final float y = ev.getY(i); if (findTopChildUnder((int) x, (int) y) == mCapturedView && tryCaptureViewForDrag(mCapturedView, id)) { newActivePointer = mActivePointerId; break; } } if (newActivePointer == INVALID_POINTER) { // We didn't find another pointer still touching the view, release it. releaseViewForPointerUp(); } } clearMotionHistory(pointerId); break; } case MotionEvent.ACTION_UP: { if (mDragState == STATE_DRAGGING) { releaseViewForPointerUp(); } cancel(); break; } case MotionEvent.ACTION_CANCEL: { if (mDragState == STATE_DRAGGING) { dispatchViewReleased(0, 0); } cancel(); break; } } }
From source file:com.cyanogenmod.filemanager.ui.widgets.DrawerLayout.java
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getActionMasked(); // "|" used deliberately here; both methods should be invoked. final boolean interceptForDrag = mLeftDragger.shouldInterceptTouchEvent(ev) | mRightDragger.shouldInterceptTouchEvent(ev); boolean interceptForTap = false; switch (action) { case MotionEvent.ACTION_DOWN: { final float x = ev.getX(); final float y = ev.getY(); mInitialMotionX = x;//from w w w . ja v a2 s .co m mInitialMotionY = y; if (mScrimOpacity > 0 && isContentView(mLeftDragger.findTopChildUnder((int) x, (int) y))) { interceptForTap = true; } mDisallowInterceptRequested = false; mChildrenCanceledTouch = false; break; } case MotionEvent.ACTION_MOVE: { // If we cross the touch slop, don't perform the delayed peek for an edge touch. if (mLeftDragger.checkTouchSlop(ViewDragHelper.DIRECTION_ALL)) { mLeftCallback.removeCallbacks(); mRightCallback.removeCallbacks(); } break; } case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: { closeDrawers(true); mDisallowInterceptRequested = false; mChildrenCanceledTouch = false; } } return interceptForDrag || interceptForTap || hasPeekingDrawer() || mChildrenCanceledTouch; }
From source file:com.grottworkshop.gwsswipelayout.SwipeLayout.java
@Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabledInAdapterView() || !isEnabled()) return true; if (!isSwipeEnabled()) return super.onTouchEvent(event); int action = event.getActionMasked(); ViewParent parent = getParent();//from www. j a v a2s . c o m gestureDetector.onTouchEvent(event); Status status = getOpenStatus(); ViewGroup touching = null; if (status == Status.Close) { touching = getSurfaceView(); } else if (status == Status.Open) { touching = getBottomView(); } switch (action) { case MotionEvent.ACTION_DOWN: mDragHelper.processTouchEvent(event); parent.requestDisallowInterceptTouchEvent(true); sX = event.getRawX(); sY = event.getRawY(); if (touching != null) touching.setPressed(true); return true; case MotionEvent.ACTION_MOVE: { if (sX == -1 || sY == -1) { // Trick: // When in nested mode, we need to send a constructed ACTION_DOWN MotionEvent to mDragHelper, to help // it initialize itself. event.setAction(MotionEvent.ACTION_DOWN); mDragHelper.processTouchEvent(event); parent.requestDisallowInterceptTouchEvent(true); sX = event.getRawX(); sY = event.getRawY(); return true; } float distanceX = event.getRawX() - sX; float distanceY = event.getRawY() - sY; float angle = Math.abs(distanceY / distanceX); angle = (float) Math.toDegrees(Math.atan(angle)); boolean doNothing = false; if (mDragEdge == DragEdge.Right) { boolean suitable = (status == Status.Open && distanceX > 0) || (status == Status.Close && distanceX < 0); suitable = suitable || (status == Status.Middle); if (angle > 30 || !suitable) { doNothing = true; } } if (mDragEdge == DragEdge.Left) { boolean suitable = (status == Status.Open && distanceX < 0) || (status == Status.Close && distanceX > 0); suitable = suitable || status == Status.Middle; if (angle > 30 || !suitable) { doNothing = true; } } if (mDragEdge == DragEdge.Top) { boolean suitable = (status == Status.Open && distanceY < 0) || (status == Status.Close && distanceY > 0); suitable = suitable || status == Status.Middle; if (angle < 60 || !suitable) { doNothing = true; } } if (mDragEdge == DragEdge.Bottom) { boolean suitable = (status == Status.Open && distanceY > 0) || (status == Status.Close && distanceY < 0); suitable = suitable || status == Status.Middle; if (angle < 60 || !suitable) { doNothing = true; } } if (doNothing) { parent.requestDisallowInterceptTouchEvent(false); return false; } else { if (touching != null) { touching.setPressed(false); } parent.requestDisallowInterceptTouchEvent(true); mDragHelper.processTouchEvent(event); } break; } case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: { sX = -1; sY = -1; if (touching != null) { touching.setPressed(false); } } default: parent.requestDisallowInterceptTouchEvent(true); mDragHelper.processTouchEvent(event); } return true; }
From source file:com.bitflake.counter.HorizontalPicker.java
@Override public boolean onTouchEvent(MotionEvent event) { if (!isEnabled()) { return false; }// ww w . j a v a2s . c om if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(event); int action = event.getActionMasked(); switch (action) { case MotionEvent.ACTION_MOVE: float currentMoveX = event.getX(); int deltaMoveX = (int) (lastDownEventX - currentMoveX); if (scrollingX || (Math.abs(deltaMoveX) > touchSlop) && values != null && values.length > 0) { if (!scrollingX) { deltaMoveX = 0; pressedItem = -1; scrollingX = true; getParent().requestDisallowInterceptTouchEvent(true); stopMarqueeIfNeeded(); } final int range = getScrollRange(); if (overScrollBy(deltaMoveX, 0, getScrollX(), 0, range, 0, overscrollDistance, 0, true)) { mVelocityTracker.clear(); } final float pulledToX = getScrollX() + deltaMoveX; if (pulledToX < 0) { leftEdgeEffect.onPull((float) deltaMoveX / getWidth()); if (!rightEdgeEffect.isFinished()) { rightEdgeEffect.onRelease(); } } else if (pulledToX > range) { rightEdgeEffect.onPull((float) deltaMoveX / getWidth()); if (!leftEdgeEffect.isFinished()) { leftEdgeEffect.onRelease(); } } lastDownEventX = currentMoveX; invalidate(); } break; case MotionEvent.ACTION_DOWN: if (!adjustScrollerX.isFinished()) { adjustScrollerX.forceFinished(true); } else if (!flingScrollerX.isFinished()) { flingScrollerX.forceFinished(true); } else { scrollingX = false; } lastDownEventX = event.getX(); if (!scrollingX) { pressedItem = getPositionFromTouch(event.getX()); } invalidate(); break; case MotionEvent.ACTION_UP: VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000, maximumFlingVelocity); int initialVelocityX = (int) velocityTracker.getXVelocity(); if (scrollingX && Math.abs(initialVelocityX) > mMinimumFlingVelocity) { flingX(initialVelocityX); } else if (values != null) { float positionX = event.getX(); if (!scrollingX) { int itemPos = getPositionOnScreen(positionX); int relativePos = itemPos - sideItems; if (relativePos == 0) { selectItem(); } else { smoothScrollBy(relativePos); } } else if (scrollingX) { finishScrolling(); } } mVelocityTracker.recycle(); mVelocityTracker = null; if (leftEdgeEffect != null) { leftEdgeEffect.onRelease(); rightEdgeEffect.onRelease(); } case MotionEvent.ACTION_CANCEL: pressedItem = -1; invalidate(); if (leftEdgeEffect != null) { leftEdgeEffect.onRelease(); rightEdgeEffect.onRelease(); } break; } return true; }