List of usage examples for android.view MotionEvent ACTION_DOWN
int ACTION_DOWN
To view the source code for android.view MotionEvent ACTION_DOWN.
Click Source Link
From source file:au.com.glassechidna.velocityviewpager.VelocityViewPager.java
/** * Start a fake drag of the pager.//from www.ja v a 2 s. c o m * * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager * with the touch scrolling of another view, while still letting the ViewPager * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call * {@link #endFakeDrag()} to complete the fake drag and fling as necessary. * * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag * is already in progress, this method will return false. * * @return true if the fake drag began successfully, false if it could not be started. * * @see #fakeDragBy(float) * @see #endFakeDrag() */ public boolean beginFakeDrag() { if (mIsBeingDragged) { return false; } mFakeDragging = true; setScrollState(SCROLL_STATE_DRAGGING); mInitialMotionX = mLastMotionX = 0; if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else { mVelocityTracker.clear(); } final long time = SystemClock.uptimeMillis(); final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0); mVelocityTracker.addMovement(ev); ev.recycle(); mFakeDragBeginTime = time; return true; }
From source file:com.processing.core.PApplet.java
/** * Figure out how to process a mouse event. When loop() has been * called, the events will be queued up until drawing is complete. * If noLoop() has been called, then events will happen immediately. *///from ww w. j a v a 2 s. com protected void nativeMotionEvent(android.view.MotionEvent motionEvent) { // enqueueMotionEvent(event); // // // this will be the last event in the list // AndroidTouchEvent pme = motionEventQueue[motionEventCount + historyCount]; // pme.setAction(event.getAction()); // pme.setNumPointers(event.getPointerCount()); // pme.setPointers(event); // // // historical events happen before the 'current' values // if (pme.action == MotionEvent.ACTION_MOVE && historyCount > 0) { // for (int i = 0; i < historyCount; i++) { // AndroidTouchEvent hist = motionEventQueue[motionEventCount++]; // hist.setAction(event.getAction()); // hist.setNumPointers(event.getPointerCount()); // hist.setPointers(event, i); // } // } // ACTION_HOVER_ENTER and ACTION_HOVER_EXIT are passed into // onGenericMotionEvent(android.view.MotionEvent) // if we want to implement mouseEntered/Exited // http://developer.android.com/reference/android/view/MotionEvent.html // http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html // http://www.techrepublic.com/blog/app-builder/use-androids-gesture-detector-to-translate-a-swipe-into-an-event/1577 int metaState = motionEvent.getMetaState(); int modifiers = 0; if ((metaState & android.view.KeyEvent.META_SHIFT_ON) != 0) { modifiers |= Event.SHIFT; } if ((metaState & META_CTRL_ON) != 0) { modifiers |= Event.CTRL; } if ((metaState & META_META_ON) != 0) { modifiers |= Event.META; } if ((metaState & android.view.KeyEvent.META_ALT_ON) != 0) { modifiers |= Event.ALT; } int clickCount = 1; // not really set... (i.e. not catching double taps) int index; // MotionEvent.html -> getButtonState() does BUTTON_PRIMARY, SECONDARY, TERTIARY // use this for left/right/etc switch (motionEvent.getAction()) { case MotionEvent.ACTION_DOWN: motionPointerId = motionEvent.getPointerId(0); postEvent(new MouseEvent(motionEvent, motionEvent.getEventTime(), MouseEvent.PRESS, modifiers, (int) motionEvent.getX(), (int) motionEvent.getY(), LEFT, clickCount)); break; case MotionEvent.ACTION_MOVE: // int historySize = motionEvent.getHistorySize(); index = motionEvent.findPointerIndex(motionPointerId); if (index != -1) { postEvent(new MouseEvent(motionEvent, motionEvent.getEventTime(), MouseEvent.DRAG, modifiers, (int) motionEvent.getX(index), (int) motionEvent.getY(index), LEFT, clickCount)); } break; case MotionEvent.ACTION_UP: index = motionEvent.findPointerIndex(motionPointerId); if (index != -1) { postEvent(new MouseEvent(motionEvent, motionEvent.getEventTime(), MouseEvent.RELEASE, modifiers, (int) motionEvent.getX(index), (int) motionEvent.getY(index), LEFT, clickCount)); } break; } //postEvent(pme); }
From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java
/** * Called when a key is long pressed. By default this will open any popup * keyboard associated with this key through the attributes popupLayout and * popupCharacters.//from w w w .jav a 2s .c o m * * @param popupKey the key that was long pressed * @return true if the long press is handled, false otherwise. Subclasses * should call the method on the base class if the subclass doesn't * wish to handle the call. */ protected boolean onLongPress(Context packageContext, Key popupKey, boolean isSticky, boolean requireSlideInto) { if (popupKey.popupResId == 0) return false; if (mMiniKeyboard == null) { createMiniKeyboard(); } AnyPopupKeyboard popupKeyboard = setupMiniKeyboardContainer(packageContext, popupKey, isSticky); mMiniKeyboardVisible = true; if (mWindowOffset == null) { mWindowOffset = new int[2]; getLocationInWindow(mWindowOffset); } int popupX = popupKey.x + mWindowOffset[0]; popupX -= mMiniKeyboard.getPaddingLeft(); int popupY = popupKey.y + mWindowOffset[1]; popupY += getPaddingTop(); popupY -= mMiniKeyboard.getMeasuredHeight(); popupY -= mMiniKeyboard.getPaddingBottom(); final int x = popupX; final int y = mShowPreview && mOldPreviewKeyIndex != NOT_A_KEY && isOneRowKeys(mMiniKeyboard.getKeyboard().getKeys()) ? mPopupPreviewDisplayedY : popupY; int adjustedX = x; boolean shouldMirrorKeys = false; //now we need to see the the popup is positioned correctly: //1) if the right edge is off the screen, then we'll try to put the right edge over the popup key if (adjustedX > (getMeasuredWidth() - mMiniKeyboard.getMeasuredWidth())) { adjustedX = popupKey.x + mWindowOffset[0] - mMiniKeyboard.getMeasuredWidth(); //adding the width of the key - now the right most popup key is above the finger adjustedX += popupKey.width; adjustedX += mMiniKeyboard.getPaddingRight(); shouldMirrorKeys = true; } //2) if it is still negative, then let's put it at the beginning (shouldn't happen) if (adjustedX < 0) { adjustedX = 0; shouldMirrorKeys = false; } if (shouldMirrorKeys) popupKeyboard.mirrorKeys(); mMiniKeyboardOriginX = adjustedX + mMiniKeyboard.getPaddingLeft() - mWindowOffset[0]; mMiniKeyboardOriginY = y + mMiniKeyboard.getPaddingTop() - mWindowOffset[1]; //I'm not sure I need to do this, but in any case - this is to sync the popup window //to align to the mini-keyboard position mMiniKeyboard.setPopupOffset(adjustedX, y); // NOTE:I'm checking the main keyboard shift state directly! // Not anything else. mMiniKeyboard.setShifted(mKeyboard != null && mKeyboard.isShifted()); // Mini keyboard needs no pop-up key preview displayed. mMiniKeyboard.setPreviewEnabled(false); // animation switching required? mMiniKeyboardPopup.setContentView(mMiniKeyboard); mMiniKeyboardPopup.setWidth(mMiniKeyboard.getMeasuredWidth()); mMiniKeyboardPopup.setHeight(mMiniKeyboard.getMeasuredHeight()); mMiniKeyboardPopup.showAtLocation(this, Gravity.NO_GRAVITY, adjustedX, y); if (requireSlideInto) { // Inject down event on the key to mini keyboard. long eventTime = SystemClock.uptimeMillis(); mMiniKeyboardPopupTime = eventTime; MotionEvent downEvent = generateMiniKeyboardMotionEvent(MotionEvent.ACTION_DOWN, popupKey.x + popupKey.width / 2, popupKey.y + popupKey.height / 2, eventTime); mMiniKeyboard.onTouchEvent(downEvent); downEvent.recycle(); } invalidateAllKeys(); return true; }
From source file:android.support.v71.widget.RecyclerView.java
@Override public boolean onInterceptTouchEvent(MotionEvent e) { if (mLayoutFrozen) { // When layout is frozen, RV does not intercept the motion event. // A child view e.g. a button may still get the click. return false; }//from ww w. j a v a 2 s . c o m if (dispatchOnItemTouchIntercept(e)) { cancelTouch(); return true; } if (mLayout == null) { return false; } final boolean canScrollHorizontally = mLayout.canScrollHorizontally(); final boolean canScrollVertically = mLayout.canScrollVertically(); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(e); final int action = MotionEventCompat.getActionMasked(e); final int actionIndex = MotionEventCompat.getActionIndex(e); switch (action) { case MotionEvent.ACTION_DOWN: if (mIgnoreMotionEventTillDown) { mIgnoreMotionEventTillDown = false; } mScrollPointerId = MotionEventCompat.getPointerId(e, 0); mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f); if (mScrollState == SCROLL_STATE_SETTLING) { getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } // Clear the nested offsets mNestedOffsets[0] = mNestedOffsets[1] = 0; int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; if (canScrollHorizontally) { nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL; } if (canScrollVertically) { nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; } startNestedScroll(nestedScrollAxis); break; case MotionEventCompat.ACTION_POINTER_DOWN: mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex); mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f); mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f); break; case MotionEvent.ACTION_MOVE: { final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId); if (index < 0) { Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId + " not found. Did any MotionEvents get skipped?"); return false; } final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f); final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f); if (mScrollState != SCROLL_STATE_DRAGGING) { final int dx = x - mInitialTouchX; final int dy = y - mInitialTouchY; boolean startScroll = false; if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) { mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1); startScroll = true; } if (canScrollVertically && Math.abs(dy) > mTouchSlop) { mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1); startScroll = true; } if (startScroll) { setScrollState(SCROLL_STATE_DRAGGING); } } } break; case MotionEventCompat.ACTION_POINTER_UP: { onPointerUp(e); } break; case MotionEvent.ACTION_UP: { mVelocityTracker.clear(); stopNestedScroll(); } break; case MotionEvent.ACTION_CANCEL: { cancelTouch(); } } return mScrollState == SCROLL_STATE_DRAGGING; }
From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java
/** * Start a fake drag of the pager./* w w w .j a v a2s .co m*/ * * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager * with the touch scrolling of another view, while still letting the ViewPager * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call * {@link #endFakeDrag()} to complete the fake drag and fling as necessary. * * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag * is already in progress, this method will return false. * * @return true if the fake drag began successfully, false if it could not be started. * * @see #fakeDragBy(float) * @see #endFakeDrag() */ public boolean beginFakeDrag() { if (mIsBeingDragged) { return false; } mFakeDragging = true; setScrollState(SCROLL_STATE_DRAGGING); if (isOrientationHorizontal()) { mInitialMotionX = mLastMotionX = 0; } else { mInitialMotionY = mLastMotionY = 0; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else { mVelocityTracker.clear(); } final long time = SystemClock.uptimeMillis(); final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0); mVelocityTracker.addMovement(ev); ev.recycle(); mFakeDragBeginTime = time; return true; }
From source file:android.improving.utils.views.cardsview.OrientedViewPager.java
/** * Start a fake drag of the pager./*from ww w . jav a 2s . c om*/ * <p/> * <p>A fake drag can be useful if you want to synchronize the motion of the ViewPager * with the touch scrolling of another view, while still letting the ViewPager * control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) * Call {@link #fakeDragBy(float)} to simulate the actual drag motion. Call * {@link #endFakeDrag()} to complete the fake drag and fling as necessary. * <p/> * <p>During a fake drag the ViewPager will ignore all touch events. If a real drag * is already in progress, this method will return false. * * @return true if the fake drag began successfully, false if it could not be started. * @see #fakeDragBy(float) * @see #endFakeDrag() */ public boolean beginFakeDrag() { if (mIsBeingDragged) { return false; } mFakeDragging = true; setScrollState(SCROLL_STATE_DRAGGING); if (mOrientation == Orientation.VERTICAL) { mInitialMotionY = mLastMotionY = 0; } else { mInitialMotionX = mLastMotionX = 0; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else { mVelocityTracker.clear(); } final long time = SystemClock.uptimeMillis(); final MotionEvent ev = MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN, 0, 0, 0); mVelocityTracker.addMovement(ev); ev.recycle(); mFakeDragBeginTime = time; return true; }
From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java
private void sendOnXEvent(final int action, final long eventTime, final int x, final int y, PointerTracker tracker) {/*from w w w. ja v a 2s .c o m*/ switch (action) { case MotionEvent.ACTION_DOWN: case 0x00000005:// MotionEvent.ACTION_POINTER_DOWN: onDownEvent(tracker, x, y, eventTime); break; case MotionEvent.ACTION_UP: case 0x00000006:// MotionEvent.ACTION_POINTER_UP: onUpEvent(tracker, x, y, eventTime); break; case MotionEvent.ACTION_CANCEL: onCancelEvent(tracker, x, y, eventTime); break; } }
From source file:android.support.v71.widget.RecyclerView.java
@Override public boolean onTouchEvent(MotionEvent e) { if (mLayoutFrozen || mIgnoreMotionEventTillDown) { return false; }//from www . j av a 2 s .co m if (dispatchOnItemTouch(e)) { cancelTouch(); return true; } if (mLayout == null) { return false; } final boolean canScrollHorizontally = mLayout.canScrollHorizontally(); final boolean canScrollVertically = mLayout.canScrollVertically(); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } boolean eventAddedToVelocityTracker = false; final MotionEvent vtev = MotionEvent.obtain(e); final int action = MotionEventCompat.getActionMasked(e); final int actionIndex = MotionEventCompat.getActionIndex(e); if (action == MotionEvent.ACTION_DOWN) { mNestedOffsets[0] = mNestedOffsets[1] = 0; } vtev.offsetLocation(mNestedOffsets[0], mNestedOffsets[1]); switch (action) { case MotionEvent.ACTION_DOWN: { mScrollPointerId = MotionEventCompat.getPointerId(e, 0); mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f); int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; if (canScrollHorizontally) { nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL; } if (canScrollVertically) { nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; } startNestedScroll(nestedScrollAxis); } break; case MotionEventCompat.ACTION_POINTER_DOWN: { mScrollPointerId = MotionEventCompat.getPointerId(e, actionIndex); mInitialTouchX = mLastTouchX = (int) (MotionEventCompat.getX(e, actionIndex) + 0.5f); mInitialTouchY = mLastTouchY = (int) (MotionEventCompat.getY(e, actionIndex) + 0.5f); } break; case MotionEvent.ACTION_MOVE: { final int index = MotionEventCompat.findPointerIndex(e, mScrollPointerId); if (index < 0) { Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId + " not found. Did any MotionEvents get skipped?"); return false; } final int x = (int) (MotionEventCompat.getX(e, index) + 0.5f); final int y = (int) (MotionEventCompat.getY(e, index) + 0.5f); int dx = mLastTouchX - x; int dy = mLastTouchY - y; if (dispatchNestedPreScroll(dx, dy, mScrollConsumed, mScrollOffset)) { dx -= mScrollConsumed[0]; dy -= mScrollConsumed[1]; vtev.offsetLocation(mScrollOffset[0], mScrollOffset[1]); // Updated the nested offsets mNestedOffsets[0] += mScrollOffset[0]; mNestedOffsets[1] += mScrollOffset[1]; } if (mScrollState != SCROLL_STATE_DRAGGING) { boolean startScroll = false; if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) { if (dx > 0) { dx -= mTouchSlop; } else { dx += mTouchSlop; } startScroll = true; } if (canScrollVertically && Math.abs(dy) > mTouchSlop) { if (dy > 0) { dy -= mTouchSlop; } else { dy += mTouchSlop; } startScroll = true; } if (startScroll) { setScrollState(SCROLL_STATE_DRAGGING); } } if (mScrollState == SCROLL_STATE_DRAGGING) { mLastTouchX = x - mScrollOffset[0]; mLastTouchY = y - mScrollOffset[1]; if (scrollByInternal(canScrollHorizontally ? dx : 0, canScrollVertically ? dy : 0, vtev)) { getParent().requestDisallowInterceptTouchEvent(true); } } } break; case MotionEventCompat.ACTION_POINTER_UP: { onPointerUp(e); } break; case MotionEvent.ACTION_UP: { mVelocityTracker.addMovement(vtev); eventAddedToVelocityTracker = true; mVelocityTracker.computeCurrentVelocity(1000, mMaxFlingVelocity); final float xvel = canScrollHorizontally ? -VelocityTrackerCompat.getXVelocity(mVelocityTracker, mScrollPointerId) : 0; final float yvel = canScrollVertically ? -VelocityTrackerCompat.getYVelocity(mVelocityTracker, mScrollPointerId) : 0; if (!((xvel != 0 || yvel != 0) && fling((int) xvel, (int) yvel))) { setScrollState(SCROLL_STATE_IDLE); } resetTouch(); } break; case MotionEvent.ACTION_CANCEL: { cancelTouch(); } break; } if (!eventAddedToVelocityTracker) { mVelocityTracker.addMovement(vtev); } vtev.recycle(); return true; }
From source file:cn.ismartv.tvrecyclerview.widget.RecyclerView.java
@Override public boolean onInterceptTouchEvent(MotionEvent e) { if (mLayoutFrozen) { // When layout is frozen, RV does not intercept the motion event. // A child view e.g. a button may still get the click. return false; }//w ww. j a va 2 s.co m if (dispatchOnItemTouchIntercept(e)) { cancelTouch(); return true; } if (mLayout == null) { return false; } final boolean canScrollHorizontally = mLayout.canScrollHorizontally(); final boolean canScrollVertically = mLayout.canScrollVertically(); if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(e); final int action = MotionEventCompat.getActionMasked(e); final int actionIndex = MotionEventCompat.getActionIndex(e); switch (action) { case MotionEvent.ACTION_DOWN: if (mIgnoreMotionEventTillDown) { mIgnoreMotionEventTillDown = false; } mScrollPointerId = e.getPointerId(0); mInitialTouchX = mLastTouchX = (int) (e.getX() + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY() + 0.5f); if (mScrollState == SCROLL_STATE_SETTLING) { getParent().requestDisallowInterceptTouchEvent(true); setScrollState(SCROLL_STATE_DRAGGING); } // Clear the nested offsets mNestedOffsets[0] = mNestedOffsets[1] = 0; int nestedScrollAxis = ViewCompat.SCROLL_AXIS_NONE; if (canScrollHorizontally) { nestedScrollAxis |= ViewCompat.SCROLL_AXIS_HORIZONTAL; } if (canScrollVertically) { nestedScrollAxis |= ViewCompat.SCROLL_AXIS_VERTICAL; } startNestedScroll(nestedScrollAxis); break; case MotionEventCompat.ACTION_POINTER_DOWN: mScrollPointerId = e.getPointerId(actionIndex); mInitialTouchX = mLastTouchX = (int) (e.getX(actionIndex) + 0.5f); mInitialTouchY = mLastTouchY = (int) (e.getY(actionIndex) + 0.5f); break; case MotionEvent.ACTION_MOVE: { final int index = e.findPointerIndex(mScrollPointerId); if (index < 0) { Log.e(TAG, "Error processing scroll; pointer index for id " + mScrollPointerId + " not found. Did any MotionEvents get skipped?"); return false; } final int x = (int) (e.getX(index) + 0.5f); final int y = (int) (e.getY(index) + 0.5f); if (mScrollState != SCROLL_STATE_DRAGGING) { final int dx = x - mInitialTouchX; final int dy = y - mInitialTouchY; boolean startScroll = false; if (canScrollHorizontally && Math.abs(dx) > mTouchSlop) { mLastTouchX = mInitialTouchX + mTouchSlop * (dx < 0 ? -1 : 1); startScroll = true; } if (canScrollVertically && Math.abs(dy) > mTouchSlop) { mLastTouchY = mInitialTouchY + mTouchSlop * (dy < 0 ? -1 : 1); startScroll = true; } if (startScroll) { setScrollState(SCROLL_STATE_DRAGGING); } } } break; case MotionEventCompat.ACTION_POINTER_UP: { onPointerUp(e); } break; case MotionEvent.ACTION_UP: { mVelocityTracker.clear(); stopNestedScroll(); } break; case MotionEvent.ACTION_CANCEL: { cancelTouch(); } } return mScrollState == SCROLL_STATE_DRAGGING; }
From source file:android.app.Activity.java
/** * Called to process touch screen events. You can override this to * intercept all touch screen events before they are dispatched to the * window. Be sure to call this implementation for touch screen events * that should be handled normally.//from w w w. j a va 2 s .c o m * * @param ev The touch screen event. * * @return boolean Return true if this event was consumed. */ public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getAction() == MotionEvent.ACTION_DOWN) { onUserInteraction(); } if (getWindow().superDispatchTouchEvent(ev)) { return true; } return onTouchEvent(ev); }