Java Code Examples for android.view.MotionEvent#recycle()

The following examples show how to use android.view.MotionEvent#recycle() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InjectionManager.java    From android-inputinjector with GNU General Public License v2.0 7 votes vote down vote up
public void injectTouchEventRelease(int x, int y)
{
	MotionEvent me = MotionEvent.obtain(
			SystemClock.uptimeMillis(),
			SystemClock.uptimeMillis()+10,
			MotionEvent.ACTION_UP,
			x,
			y,
			0
			);
	
	if(Integer.valueOf(android.os.Build.VERSION.SDK_INT) < android.os.Build.VERSION_CODES.JELLY_BEAN)
		;
	else
		me.setSource(InputDevice.SOURCE_TOUCHSCREEN);
	
	injectEvent(me, INJECT_INPUT_EVENT_MODE_WAIT_FOR_RESULT);
	me.recycle();
}
 
Example 2
Source File: LinkagePager.java    From music-player with MIT License 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <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;
}
 
Example 3
Source File: SliderPager.java    From Android-Image-Slider with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <p>A fake drag can be useful if you want to synchronize the motion of the SliderPager
 * with the touch scrolling of another view, while still letting the SliderPager
 * 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 SliderPager 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;
}
 
Example 4
Source File: VerticalViewPager.java    From DoubleViewPager with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 *
 * <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;
}
 
Example 5
Source File: VerticalViewPager.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public boolean beginFakeDrag()
{
    if (C)
    {
        return false;
    }
    O = true;
    a(1);
    G = 0.0F;
    H = 0.0F;
    long l1;
    MotionEvent motionevent;
    if (K == null)
    {
        K = VelocityTracker.obtain();
    } else
    {
        K.clear();
    }
    l1 = SystemClock.uptimeMillis();
    motionevent = MotionEvent.obtain(l1, l1, 0, 0.0F, 0.0F, 0);
    K.addMovement(motionevent);
    motionevent.recycle();
    P = l1;
    return true;
}
 
Example 6
Source File: CoordinatorLayout.java    From ticdesign with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    MotionEvent cancelEvent = null;

    final int action = MotionEventCompat.getActionMasked(ev);

    // Make sure we reset in case we had missed a previous important event.
    if (action == MotionEvent.ACTION_DOWN) {
        resetTouchBehaviors();
    }

    final boolean intercepted = performIntercept(ev, TYPE_ON_INTERCEPT);

    if (cancelEvent != null) {
        cancelEvent.recycle();
    }

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        resetTouchBehaviors();
    }

    return intercepted;
}
 
Example 7
Source File: VerticalViewPager.java    From ticdesign with Apache License 2.0 6 votes vote down vote up
/**
 * Start a fake drag of the pager.
 * <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);
    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;
}
 
Example 8
Source File: DragDropTouchDragListener.java    From RecyclerViewExtensions with MIT License 5 votes vote down vote up
private void dispatchTouchEvent(View view, int action, float x, float y) {
    if (!(view instanceof RecyclerView)) {
        throw new IllegalStateException("DragDropTouchDragListener must be set on a RecyclerView");
    }
    MotionEvent motionEvent = MotionEvent.obtain(startTime, SystemClock.uptimeMillis(), action, x, y, 0);
    if (this.intercept) {
        onItemTouchListener.onTouchEvent((RecyclerView) view, motionEvent);
    } else {
        this.intercept = onItemTouchListener.onInterceptTouchEvent((RecyclerView) view, motionEvent);
    }
    motionEvent.recycle();
}
 
Example 9
Source File: ZoomableDraweeView.java    From fresco with MIT License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
  int a = event.getActionMasked();
  FLog.v(getLogTag(), "onTouchEvent: %d, view %x, received", a, this.hashCode());
  if (!mIsDialtoneEnabled && mTapGestureDetector.onTouchEvent(event)) {
    FLog.v(
        getLogTag(),
        "onTouchEvent: %d, view %x, handled by tap gesture detector",
        a,
        this.hashCode());
    return true;
  }

  if (!mIsDialtoneEnabled && mZoomableController.onTouchEvent(event)) {
    FLog.v(
        getLogTag(),
        "onTouchEvent: %d, view %x, handled by zoomable controller",
        a,
        this.hashCode());
    if (!mAllowTouchInterceptionWhileZoomed && !mZoomableController.isIdentity()) {
      getParent().requestDisallowInterceptTouchEvent(true);
    }
    return true;
  }
  if (super.onTouchEvent(event)) {
    FLog.v(getLogTag(), "onTouchEvent: %d, view %x, handled by the super", a, this.hashCode());
    return true;
  }
  // None of our components reported that they handled the touch event. Upon returning false
  // from this method, our parent won't send us any more events for this gesture. Unfortunately,
  // some components may have started a delayed action, such as a long-press timer, and since we
  // won't receive an ACTION_UP that would cancel that timer, a false event may be triggered.
  // To prevent that we explicitly send one last cancel event when returning false.
  MotionEvent cancelEvent = MotionEvent.obtain(event);
  cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
  mTapGestureDetector.onTouchEvent(cancelEvent);
  mZoomableController.onTouchEvent(cancelEvent);
  cancelEvent.recycle();
  return false;
}
 
Example 10
Source File: BaseCell.java    From Telegram with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    if (checkingForLongPress && getParent() != null && currentPressCount == pressCount) {
        checkingForLongPress = false;
        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
        onLongPress();
        MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
        onTouchEvent(event);
        event.recycle();
    }
}
 
Example 11
Source File: ContentViewGestureHandler.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Resets gesture handlers state; called on didStartLoading().
 * Note that this does NOT clear the pending motion events queue;
 * it gets cleared in hasTouchEventHandlers() called from WebKit
 * FrameLoader::transitionToCommitted iff the page ever had touch handlers.
 */
void resetGestureHandlers() {
    MotionEvent me = obtainActionCancelMotionEvent();
    me.setSource(InputDevice.SOURCE_CLASS_POINTER);
    mGestureDetector.onTouchEvent(me);
    mZoomManager.processTouchEvent(me);
    me.recycle();
    mLongPressDetector.cancelLongPress();
}
 
Example 12
Source File: SharedLinkCell.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    if (checkingForLongPress && getParent() != null && currentPressCount == pressCount) {
        checkingForLongPress = false;
        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
        if (pressedLink >= 0) {
            delegate.onLinkLongPress(links.get(pressedLink));
        }
        MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
        onTouchEvent(event);
        event.recycle();
    }
}
 
Example 13
Source File: EventFilter.java    From 365browser with Apache License 2.0 5 votes vote down vote up
/**
 * @see android.view.ViewGroup#onInterceptTouchEvent(android.view.MotionEvent)
 * @param event             The {@link MotionEvent} that started the gesture to be evaluated.
 * @param isKeyboardShowing Whether the keyboard is currently showing.
 * @return                  Whether the filter is going to intercept events.
 */
public final boolean onInterceptTouchEvent(MotionEvent event, boolean isKeyboardShowing) {
    MotionEvent sentEvent = event;
    if (mAutoOffset && (mCurrentTouchOffsetX != 0 || mCurrentTouchOffsetY != 0)) {
        sentEvent = MotionEvent.obtain(event);
        sentEvent.offsetLocation(mCurrentTouchOffsetX, mCurrentTouchOffsetY);
    }
    boolean consumed = onInterceptTouchEventInternal(sentEvent, isKeyboardShowing);
    if (sentEvent != event) sentEvent.recycle();
    return consumed;
}
 
Example 14
Source File: Means.java    From Memory-capsule with Apache License 2.0 5 votes vote down vote up
public static void setViewonlick(View view,float X,float Y){
    long dowmTime= SystemClock.uptimeMillis();
    MotionEvent dowmEvent=MotionEvent.obtain(dowmTime,dowmTime,MotionEvent.ACTION_DOWN,X,Y,0);
    dowmTime+=1000;
    MotionEvent upEvent=MotionEvent.obtain(dowmTime,dowmTime,MotionEvent.ACTION_UP,X,Y,0);
    view.onTouchEvent(dowmEvent);
    view.onTouchEvent(upEvent);
    dowmEvent.recycle();
    upEvent.recycle();
}
 
Example 15
Source File: GestureFrameLayout.java    From GestureViews with Apache License 2.0 5 votes vote down vote up
@Override
public boolean dispatchTouchEvent(@NonNull MotionEvent event) {
    currentMotionEvent = event;
    // We should remap given event back to original coordinates
    // so that children can correctly respond to it
    MotionEvent invertedEvent = applyMatrix(event, matrixInverse);
    try {
        return super.dispatchTouchEvent(invertedEvent);
    } finally {
        invertedEvent.recycle();
    }
}
 
Example 16
Source File: TranslucentDrawerLayout.java    From 920-text-editor-v2 with Apache License 2.0 5 votes vote down vote up
void cancelChildViewTouch() {
    // Cancel child touches
    if (!mChildrenCanceledTouch) {
        final long now = SystemClock.uptimeMillis();
        final MotionEvent cancelEvent = MotionEvent.obtain(now, now,
                MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
        final int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            getChildAt(i).dispatchTouchEvent(cancelEvent);
        }
        cancelEvent.recycle();
        mChildrenCanceledTouch = true;
    }
}
 
Example 17
Source File: ContentView.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public boolean onTouchEvent(MotionEvent event) {
    MotionEvent offset = createOffsetMotionEvent(event);
    boolean consumed = mContentViewCore.onTouchEvent(offset);
    offset.recycle();
    return consumed;
}
 
Example 18
Source File: ContentView.java    From android-chromium with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Mouse move events are sent on hover enter, hover move and hover exit.
 * They are sent on hover exit because sometimes it acts as both a hover
 * move and hover exit.
 */
@Override
public boolean onHoverEvent(MotionEvent event) {
    MotionEvent offset = createOffsetMotionEvent(event);
    boolean consumed = mContentViewCore.onHoverEvent(offset);
    offset.recycle();
    super.onHoverEvent(event);
    return consumed;
}
 
Example 19
Source File: SharedLinkCell.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
public void run() {
    if (checkingForLongPress && getParent() != null && currentPressCount == pressCount) {
        checkingForLongPress = false;
        performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
        if (pressedLink >= 0) {
            delegate.onLinkPress(links.get(pressedLink), true);
        }
        MotionEvent event = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
        onTouchEvent(event);
        event.recycle();
    }
}
 
Example 20
Source File: CustomViewPager.java    From youqu_master with Apache License 2.0 4 votes vote down vote up
/**
 * Fake drag by an offset in pixels. You must have called {@link #beginFakeDrag()} first.
 *
 * @param xOffset Offset in pixels to drag by.
 * @see #beginFakeDrag()
 * @see #endFakeDrag()
 */
public void fakeDragBy(float xOffset) {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    if (mAdapter == null) {
        return;
    }

    mLastMotionX += xOffset;

    float oldScrollX = getScrollX();
    float scrollX = oldScrollX - xOffset;
    final int width = getClientWidth();

    float leftBound = width * mFirstOffset;
    float rightBound = width * mLastOffset;

    final ItemInfo firstItem = mItems.get(0);
    final ItemInfo lastItem = mItems.get(mItems.size() - 1);
    if (firstItem.position != 0) {
        leftBound = firstItem.offset * width;
    }
    if (lastItem.position != mAdapter.getCount() - 1) {
        rightBound = lastItem.offset * width;
    }

    if (scrollX < leftBound) {
        scrollX = leftBound;
    } else if (scrollX > rightBound) {
        scrollX = rightBound;
    }
    // Don't lose the rounded component
    mLastMotionX += scrollX - (int) scrollX;
    scrollTo((int) scrollX, getScrollY());
    pageScrolled((int) scrollX);

    // Synthesize an event for the VelocityTracker.
    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(mFakeDragBeginTime, time, MotionEvent.ACTION_MOVE,
            mLastMotionX, 0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
}