List of usage examples for android.view MotionEvent recycle
@Override public final void recycle()
From source file:com.personal.taskmanager2.utilities.RecyclerViewTouchListener.java
@Override public void onTouchEvent(RecyclerView rv, MotionEvent e) { switch (e.getActionMasked()) { case MotionEvent.ACTION_CANCEL: Log.d(TAG, "Cancel Event"); break;//from w w w . j a v a2 s. c o m case MotionEvent.ACTION_DOWN: Log.d(TAG, "Press Event"); break; case MotionEvent.ACTION_UP: Log.d(TAG, "Up Event"); if (mVelocityTracker == null) { Log.d(TAG, "velocity tracker is null in action up"); break; } Log.d(TAG, "Up Intercept"); float deltaX = e.getRawX() - mDownX; float absDeltaX = Math.abs(deltaX); mVelocityTracker.addMovement(e); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (absDeltaX > mViewWidth / 2) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss) { dismiss(mChildView, mChildPosition, dismissRight); } else { mChildView.animate().alpha(1).translationX(0).setDuration(mAnimationTime).setListener(null); } mVelocityTracker.recycle(); mVelocityTracker = null; mDownX = 0; mDeltaX = 0; mChildView = null; mChildPosition = RecyclerView.NO_POSITION; mSwiping = false; break; case MotionEvent.ACTION_MOVE: Log.d(TAG, "Move Event"); mRecyclerView.requestDisallowInterceptTouchEvent(true); mRefreshLayout.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(e); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (e.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); mRefreshLayout.onTouchEvent(cancelEvent); cancelEvent.recycle(); mChildView.setTranslationX(mDeltaX); /*mChildView.setAlpha(Math.max(0.15f, Math.min(1f, 1f - 2f * Math.abs(mDeltaX) / mViewWidth)));*/ break; } }
From source file:com.yyl.inputmethod.accessibility.AccessibilityEntityProvider.java
/** * Simulates a key press by injecting touch events into the keyboard view. * This avoids the complexity of trackers and listeners within the keyboard. * * @param key The key to press.//from www . j a v a2 s. c o m */ void simulateKeyPress(final Key key) { final int x = key.mHitBox.centerX(); final int y = key.mHitBox.centerY(); final long downTime = SystemClock.uptimeMillis(); final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0); final MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0); mKeyboardView.onTouchEvent(downEvent); mKeyboardView.onTouchEvent(upEvent); downEvent.recycle(); upEvent.recycle(); }
From source file:com.apptentive.android.sdk.module.messagecenter.view.MessageCenterListView.java
@Override public boolean dispatchTouchEvent(MotionEvent ev) { final float x = ev.getX(); final float y = ev.getY(); final int action = ev.getAction(); if (action == MotionEvent.ACTION_DOWN && touchTarget == null && stickyWrapper != null && isStickyViewTouched(stickyWrapper.view, x, y)) { touchTarget = stickyWrapper.view; touchPt.x = x;/*from ww w.j a v a2 s . c om*/ touchPt.y = y; downEvent = MotionEvent.obtain(ev); } if (touchTarget != null) { if (isStickyViewTouched(touchTarget, x, y)) { // forward event to header view touchTarget.dispatchTouchEvent(ev); } if (action == MotionEvent.ACTION_UP) { super.dispatchTouchEvent(ev); clearTouchTarget(); } else if (action == MotionEvent.ACTION_CANCEL) { clearTouchTarget(); } else if (action == MotionEvent.ACTION_MOVE) { if (Math.abs(y - touchPt.y) > touchSlop) { MotionEvent event = MotionEvent.obtain(ev); event.setAction(MotionEvent.ACTION_CANCEL); touchTarget.dispatchTouchEvent(event); event.recycle(); super.dispatchTouchEvent(downEvent); super.dispatchTouchEvent(ev); clearTouchTarget(); } } return true; } return super.dispatchTouchEvent(ev); }
From source file:com.onyx.latinime.accessibility.AccessibilityEntityProvider.java
/** * Simulates a key press by injecting touch events into the keyboard view. * This avoids the complexity of trackers and listeners within the keyboard. * * @param key The key to press.// w ww . j av a 2 s . com */ void simulateKeyPress(final Key key) { final int x = key.getHitBox().centerX(); final int y = key.getHitBox().centerY(); final long downTime = SystemClock.uptimeMillis(); final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0); final MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0); mKeyboardView.onTouchEvent(downEvent); mKeyboardView.onTouchEvent(upEvent); downEvent.recycle(); upEvent.recycle(); }
From source file:com.codingfeel.sm.views.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java
private void caseMotionActionMove(MotionEvent motionEvent) { mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true;/* w w w. ja v a 2s .c o m*/ int mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mRecyclerView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); if (mSwiping) { setTranslationX(mDownView, deltaX - mSwipingSlop); setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); } } }
From source file:com.malinskiy.superrecyclerview.swipe.SwipeDismissRecyclerViewTouchListener.java
private void caseMotionActionMove(MotionEvent motionEvent) { mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true;//ww w.j a v a2s. c om mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mRecyclerView.requestDisallowInterceptTouchEvent(true); // Cancel ListView's touch (un-highlighting the item) MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (MotionEventCompat .getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); mRecyclerView.onTouchEvent(cancelEvent); cancelEvent.recycle(); if (mSwiping) { setTranslationX(mDownView, deltaX - mSwipingSlop); setAlpha(mDownView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); } } }
From source file:com.nononsenseapps.feeder.ui.SwipeDismissTouchListener.java
@Override public boolean onTouch(View view, MotionEvent motionEvent) { // offset because the view is translated during swipe motionEvent.offsetLocation(mTranslationX, 0); if (mViewWidth < 2) { mViewWidth = mView.getWidth();/*w w w.j a v a 2 s .c o m*/ } switch (motionEvent.getActionMasked()) { case MotionEvent.ACTION_DOWN: { // TODO: ensure this is a finger, and set a flag mDownX = motionEvent.getRawX(); mDownY = motionEvent.getRawY(); if (mCallbacks.canDismiss(mToken)) { mVelocityTracker = VelocityTracker.obtain(); mVelocityTracker.addMovement(motionEvent); } return false; } case MotionEvent.ACTION_UP: { if (mVelocityTracker == null) { break; } float deltaX = motionEvent.getRawX() - mDownX; mVelocityTracker.addMovement(motionEvent); mVelocityTracker.computeCurrentVelocity(1000); float velocityX = mVelocityTracker.getXVelocity(); float absVelocityX = Math.abs(velocityX); float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX && absVelocityY < absVelocityX && mSwiping) { // dismiss only if flinging in the same direction as dragging dismiss = (velocityX < 0) == (deltaX < 0); dismissRight = mVelocityTracker.getXVelocity() > 0; } if (dismiss) { // dismiss mSwipingView.animate().translationX(dismissRight ? mViewWidth : -mViewWidth) //.alpha(0) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { performDismiss(); } }); } else if (mSwiping) { // cancel mSwipingView.animate().translationX(0) //.alpha(1) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mCallbacks.onSwipeCancelled(); } }); } mVelocityTracker.recycle(); mVelocityTracker = null; mTranslationX = 0; mDownX = 0; mDownY = 0; mSwiping = false; notNotifiedSwipeStart = true; break; } case MotionEvent.ACTION_CANCEL: { if (mVelocityTracker == null) { break; } mSwipingView.animate().translationX(0) //.alpha(1) .setDuration(mAnimationTime).setListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { mCallbacks.onSwipeCancelled(); } }); mVelocityTracker.recycle(); mVelocityTracker = null; mTranslationX = 0; mDownX = 0; mDownY = 0; mSwiping = false; notNotifiedSwipeStart = true; break; } case MotionEvent.ACTION_MOVE: { if (mVelocityTracker == null) { break; } mVelocityTracker.addMovement(motionEvent); float deltaX = motionEvent.getRawX() - mDownX; float deltaY = motionEvent.getRawY() - mDownY; if (Math.abs(deltaX) > mSlop && Math.abs(deltaY) < Math.abs(deltaX) / 2) { mSwiping = true; mSwipingSlop = (deltaX > 0 ? mSlop : -mSlop); mView.getParent().requestDisallowInterceptTouchEvent(true); // Cancel listview's touch MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction(MotionEvent.ACTION_CANCEL | (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); mView.onTouchEvent(cancelEvent); cancelEvent.recycle(); } if (mSwiping) { if (notNotifiedSwipeStart) { notNotifiedSwipeStart = false; mCallbacks.onSwipeStarted(deltaX > 0); } mTranslationX = deltaX; mSwipingView.setTranslationX(deltaX - mSwipingSlop); //mView.setAlpha(mInterpolator.getInterpolation(1f - 1f * Math.abs(deltaX) / mViewWidth)); // mView.setAlpha(Math.max(0f, Math.min(1f, // 1f - 2f * Math.abs(deltaX) / mViewWidth))); return true; } break; } } return false; }
From source file:com.android.inputmethod.accessibility.AccessibilityEntityProvider.java
/** * Simulates a key press by injecting touch events into the keyboard view. * This avoids the complexity of trackers and listeners within the keyboard. * * @param key The key to press./*from www. j a va 2 s.co m*/ */ void simulateKeyPress(Key key) { final int x = key.mHitBox.centerX(); final int y = key.mHitBox.centerY(); final long downTime = SystemClock.uptimeMillis(); final MotionEvent downEvent = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_DOWN, x, y, 0); final MotionEvent upEvent = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, x, y, 0); mKeyboardView.onTouchEvent(downEvent); mKeyboardView.onTouchEvent(upEvent); downEvent.recycle(); upEvent.recycle(); }
From source file:com.yek.keyboard.keyboards.views.AnyKeyboardView.java
@Override public boolean onTouchEvent(@NonNull MotionEvent me) { if (getKeyboard() == null)//I mean, if there isn't any keyboard I'm handling, what's the point? return false; if (areTouchesDisabled(me)) { return super.onTouchEvent(me); }//from w w w. ja v a 2 s. com final int action = MotionEventCompat.getActionMasked(me); // Gesture detector must be enabled only when mini-keyboard is not // on the screen. if (!mMiniKeyboardPopup.isShowing() && mGestureDetector != null && mGestureDetector.onTouchEvent(me)) { Logger.d(TAG, "Gesture detected!"); mKeyPressTimingHandler.cancelAllMessages(); dismissAllKeyPreviews(); return true; } if (action == MotionEvent.ACTION_DOWN) { mFirstTouchPoint.x = (int) me.getX(); mFirstTouchPoint.y = (int) me.getY(); mIsFirstDownEventInsideSpaceBar = mSpaceBarKey != null && mSpaceBarKey.isInside(mFirstTouchPoint.x, mFirstTouchPoint.y); } // If the motion event is above the keyboard and it's a MOVE event // coming even before the first MOVE event into the extension area if (!mIsFirstDownEventInsideSpaceBar && me.getY() < mExtensionKeyboardYActivationPoint && !mMiniKeyboardPopup.isShowing() && !mExtensionVisible && action == MotionEvent.ACTION_MOVE) { if (mExtensionKeyboardAreaEntranceTime <= 0) mExtensionKeyboardAreaEntranceTime = SystemClock.uptimeMillis(); if (SystemClock.uptimeMillis() - mExtensionKeyboardAreaEntranceTime > DELAY_BEFORE_POPPING_UP_EXTENSION_KBD) { KeyboardExtension extKbd = ((ExternalAnyKeyboard) getKeyboard()).getExtensionLayout(); if (extKbd == null || extKbd.getKeyboardResId() == AddOn.INVALID_RES_ID) { Logger.i(TAG, "No extension keyboard"); return super.onTouchEvent(me); } else { // telling the main keyboard that the last touch was // canceled MotionEvent cancel = MotionEvent.obtain(me.getDownTime(), me.getEventTime(), MotionEvent.ACTION_CANCEL, me.getX(), me.getY(), 0); super.onTouchEvent(cancel); cancel.recycle(); mExtensionVisible = true; dismissAllKeyPreviews(); if (mExtensionKey == null) { mExtensionKey = new AnyKey(new Keyboard.Row(getKeyboard()), getThemedKeyboardDimens()); mExtensionKey.edgeFlags = 0; mExtensionKey.height = 1; mExtensionKey.width = 1; mExtensionKey.popupResId = extKbd.getKeyboardResId(); mExtensionKey.externalResourcePopupLayout = mExtensionKey.popupResId != 0; mExtensionKey.x = getWidth() / 2; mExtensionKey.y = mExtensionKeyboardPopupOffset; } // so the popup will be right above your finger. mExtensionKey.x = (int) me.getX(); onLongPress(extKbd, mExtensionKey, AnyApplication.getConfig().isStickyExtensionKeyboard(), getPointerTracker(me)); // it is an extension.. getMiniKeyboard().setPreviewEnabled(true); return true; } } else { return super.onTouchEvent(me); } } else if (mExtensionVisible && me.getY() > mExtensionKeyboardYDismissPoint) { // closing the popup dismissPopupKeyboard(); return true; } else { return super.onTouchEvent(me); } }
From source file:com.anysoftkeyboard.keyboards.views.AnyKeyboardView.java
@Override public boolean onTouchEvent(@NonNull MotionEvent me) { if (getKeyboard() == null)//I mean, if there isn't any keyboard I'm handling, what's the point? return false; if (areTouchesDisabled(me)) { return super.onTouchEvent(me); }/*from ww w . ja v a 2 s . c o m*/ final int action = MotionEventCompat.getActionMasked(me); // Gesture detector must be enabled only when mini-keyboard is not // on the screen. if (!mMiniKeyboardPopup.isShowing() && mGestureDetector != null && mGestureDetector.onTouchEvent(me)) { Logger.d(TAG, "Gesture detected!"); mKeyPressTimingHandler.cancelAllMessages(); dismissAllKeyPreviews(); return true; } if (action == MotionEvent.ACTION_DOWN) { mFirstTouchPoint.x = (int) me.getX(); mFirstTouchPoint.y = (int) me.getY(); mIsFirstDownEventInsideSpaceBar = mSpaceBarKey != null && mSpaceBarKey.isInside(mFirstTouchPoint.x, mFirstTouchPoint.y); } // If the motion event is above the keyboard and it's a MOVE event // coming even before the first MOVE event into the extension area if (!mIsFirstDownEventInsideSpaceBar && me.getY() < mExtensionKeyboardYActivationPoint && !mMiniKeyboardPopup.isShowing() && !mExtensionVisible && action == MotionEvent.ACTION_MOVE) { if (mExtensionKeyboardAreaEntranceTime <= 0) mExtensionKeyboardAreaEntranceTime = SystemClock.uptimeMillis(); if (SystemClock.uptimeMillis() - mExtensionKeyboardAreaEntranceTime > DELAY_BEFORE_POPPING_UP_EXTENSION_KBD) { KeyboardExtension extKbd = ((ExternalAnyKeyboard) getKeyboard()).getExtensionLayout(); if (extKbd == null || extKbd.getKeyboardResId() == AddOn.INVALID_RES_ID) { Logger.i(TAG, "No extension keyboard"); return super.onTouchEvent(me); } else { // telling the main keyboard that the last touch was // canceled MotionEvent cancel = MotionEvent.obtain(me.getDownTime(), me.getEventTime(), MotionEvent.ACTION_CANCEL, me.getX(), me.getY(), 0); super.onTouchEvent(cancel); cancel.recycle(); mExtensionVisible = true; dismissAllKeyPreviews(); if (mExtensionKey == null) { mExtensionKey = new AnyKey(new Row(getKeyboard()), getThemedKeyboardDimens()); mExtensionKey.edgeFlags = 0; mExtensionKey.height = 1; mExtensionKey.width = 1; mExtensionKey.popupResId = extKbd.getKeyboardResId(); mExtensionKey.externalResourcePopupLayout = mExtensionKey.popupResId != 0; mExtensionKey.x = getWidth() / 2; mExtensionKey.y = mExtensionKeyboardPopupOffset; } // so the popup will be right above your finger. mExtensionKey.x = (int) me.getX(); onLongPress(extKbd, mExtensionKey, AnyApplication.getConfig().isStickyExtensionKeyboard(), getPointerTracker(me)); // it is an extension.. getMiniKeyboard().setPreviewEnabled(true); return true; } } else { return super.onTouchEvent(me); } } else if (mExtensionVisible && me.getY() > mExtensionKeyboardYDismissPoint) { // closing the popup dismissPopupKeyboard(); return true; } else { return super.onTouchEvent(me); } }