Example usage for android.view MotionEvent recycle

List of usage examples for android.view MotionEvent recycle

Introduction

In this page you can find the example usage for android.view MotionEvent recycle.

Prototype

@Override
public final void recycle() 

Source Link

Document

Recycle the MotionEvent, to be re-used by a later caller.

Usage

From source file:com.lanou.mirror.tool.VerticalViewPager.java

/**
 * Start a fake drag of the pager.//from  ww  w . j  a  v  a  2s.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;
    } /* end of if */
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    // XXX 
    mInitialMotionY = mLastMotionY = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    } /* end of if */
    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:administrator.example.com.myscrollview.VerticalViewPager.java

/**
 * Fake drag by an offset in pixels. You must have called {@link #beginFakeDrag()} first.
 *
 * @param yOffset Offset in pixels to drag by.
 * @see #beginFakeDrag()/*from  w w w  .  j a v  a 2  s. c om*/
 * @see #endFakeDrag()
 */
public void fakeDragBy(float yOffset) {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    } /* end of if */

    mLastMotionY += yOffset;
    float scrollY = getScrollY() - yOffset;
    final int height = getHeight();
    final int heightWithMargin = height + mPageMargin;

    final float topBound = Math.max(0, (mCurItem - 1) * heightWithMargin);
    final float bottomBound = Math.min(mCurItem + 1, mAdapter.getCount() - 1) * heightWithMargin;
    if (scrollY < topBound) {
        scrollY = topBound;
    } else if (scrollY > bottomBound) {
        scrollY = bottomBound;
    } /* end of if */
    // Don't lose the rounded component
    mLastMotionY += scrollY - (int) scrollY;
    scrollTo(getScrollX(), (int) scrollY);
    pageScrolled((int) scrollY);

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

From source file:interactive.view.pagereader.VerticalViewPager.java

/**
 * Start a fake drag of the pager./*from  w  ww  . j a  v  a2  s . c om*/
 * 
 * <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;
    } /* end of if */
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    // XXX 
    mInitialMotionY = mLastMotionY = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    } /* end of if */
    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.example.view.VerticalViewPager.java

/**
 * Start a fake drag of the pager.//  www  .ja  va  2  s  .  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;
    } /* end of if */
    mFakeDragging = true;
    setScrollState(SCROLL_STATE_DRAGGING);
    // XXX
    mInitialMotionY = mLastMotionY = 0;
    if (mVelocityTracker == null) {
        mVelocityTracker = VelocityTracker.obtain();
    } else {
        mVelocityTracker.clear();
    } /* end of if */
    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:org.bangbang.support.v4.widget.VerticalViewPager.java

/**
 * Start a fake drag of the pager.//from  www . j  a  v a 2s  . c  om
 *
 * <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);
    // bangbang.S
    //        mInitialMotionX = mLastMotionX = 0;
    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:com.anysoftkeyboard.keyboards.views.AnyKeyboardBaseView.java

@Override
public boolean onTouchEvent(MotionEvent nativeMotionEvent) {
    if (mKeyboard == null)//I mean, if there isn't any keyboard I'm handling, what's the point?
        return false;
    final int action = MotionEventCompat.getActionMasked(nativeMotionEvent);
    final int pointerCount = MotionEventCompat.getPointerCount(nativeMotionEvent);
    final int oldPointerCount = mOldPointerCount;
    mOldPointerCount = pointerCount;//from   ww  w  . jav  a  2  s. c o m
    if (pointerCount > 1)
        mLastTimeHadTwoFingers = SystemClock.elapsedRealtime();//marking the time. Read isAtTwoFingersState()

    if (mTouchesAreDisabledTillLastFingerIsUp) {
        if (mOldPointerCount == 1 && (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP)) {
            mTouchesAreDisabledTillLastFingerIsUp = false;
        }

        return true;
    }
    // TODO: cleanup this code into a multi-touch to single-touch event
    // converter class?
    // If the device does not have distinct multi-touch support panel,
    // ignore all multi-touch
    // events except a transition from/to single-touch.
    if (!mHasDistinctMultitouch && pointerCount > 1 && oldPointerCount > 1) {
        return true;
    }

    // Gesture detector must be enabled only when mini-keyboard is not
    // on the screen.
    if (!mMiniKeyboardVisible && mGestureDetector != null
            && (mGestureDetector.onTouchEvent(nativeMotionEvent))) {
        Log.d(TAG, "Gesture detected!");
        mHandler.cancelKeyTimers();
        dismissKeyPreview();
        return true;
    }

    final long eventTime = nativeMotionEvent.getEventTime();
    final int index = MotionEventCompat.getActionIndex(nativeMotionEvent);
    final int id = nativeMotionEvent.getPointerId(index);
    final int x = (int) nativeMotionEvent.getX(index);
    final int y = (int) nativeMotionEvent.getY(index);

    // Needs to be called after the gesture detector gets a turn, as it
    // may have
    // displayed the mini keyboard
    if (mMiniKeyboard != null && mMiniKeyboardVisible) {
        final int miniKeyboardPointerIndex = nativeMotionEvent.findPointerIndex(mMiniKeyboardTrackerId);
        if (miniKeyboardPointerIndex >= 0 && miniKeyboardPointerIndex < pointerCount) {
            final int miniKeyboardX = (int) nativeMotionEvent.getX(miniKeyboardPointerIndex);
            final int miniKeyboardY = (int) nativeMotionEvent.getY(miniKeyboardPointerIndex);
            MotionEvent translated = generateMiniKeyboardMotionEvent(action, miniKeyboardX, miniKeyboardY,
                    eventTime);
            mMiniKeyboard.onTouchEvent(translated);
            translated.recycle();
        }
        return true;
    }

    if (mHandler.isInKeyRepeat()) {
        // It will keep being in the key repeating mode while the key is
        // being pressed.
        if (action == MotionEvent.ACTION_MOVE) {
            return true;
        }
        final PointerTracker tracker = getPointerTracker(id);
        // Key repeating timer will be canceled if 2 or more keys are in
        // action, and current
        // event (UP or DOWN) is non-modifier key.
        if (pointerCount > 1 && !tracker.isModifier()) {
            mHandler.cancelKeyRepeatTimer();
        }
        // Up event will pass through.
    }

    // TODO: cleanup this code into a multi-touch to single-touch event
    // converter class?
    // Translate mutli-touch event to single-touch events on the device
    // that has no distinct
    // multi-touch panel.
    if (!mHasDistinctMultitouch) {
        // Use only main (id=0) pointer tracker.
        PointerTracker tracker = getPointerTracker(0);
        if (pointerCount == 1 && oldPointerCount == 2) {
            // Multi-touch to single touch transition.
            // Send a down event for the latest pointer.
            tracker.onDownEvent(x, y, eventTime);
        } else if (pointerCount == 2 && oldPointerCount == 1) {
            // Single-touch to multi-touch transition.
            // Send an up event for the last pointer.
            tracker.onUpEvent(tracker.getLastX(), tracker.getLastY(), eventTime);
        } else if (pointerCount == 1 && oldPointerCount == 1) {
            tracker.onTouchEvent(action, x, y, eventTime);
        } else {
            Log.w(TAG, "Unknown touch panel behavior: pointer count is " + pointerCount + " (old "
                    + oldPointerCount + ")");
        }
        return true;
    }

    if (action == MotionEvent.ACTION_MOVE) {
        for (int i = 0; i < pointerCount; i++) {
            PointerTracker tracker = getPointerTracker(nativeMotionEvent.getPointerId(i));
            tracker.onMoveEvent((int) nativeMotionEvent.getX(i), (int) nativeMotionEvent.getY(i), eventTime);
        }
    } else {
        PointerTracker tracker = getPointerTracker(id);
        sendOnXEvent(action, eventTime, x, y, tracker);
    }

    return true;
}

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./* ww w.j av a2 s .  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:com.brantapps.viewpagerindicator.vertical.VerticalViewPager.java

/**
 * Start a fake drag of the pager.//  w  w  w.  j  av a  2s . 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);
    // BrantApps Change: Changed setting of mLastMotionX to mLastMotionY
    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:org.bangbang.support.v4.widget.VerticalViewPager.java

/**
 * Fake drag by an offset in pixels. You must have called {@link #beginFakeDrag()} first.
 *
 * @param yOffset Offset in pixels to drag by.
 * @see #beginFakeDrag()//from   ww  w  .  j a v a2s  . c o  m
 * @see #endFakeDrag()
 */
public void fakeDragBy(float yOffset) {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    mLastMotionY += yOffset;

    float oldScrollY = getScrollY();
    float scrollY = oldScrollY - yOffset;
    final int height = getHeight();

    float bottomBound = height * mFirstOffset;
    float topBound = height * mLastOffset;

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

    if (scrollY < bottomBound) {
        scrollY = bottomBound;
    } else if (scrollY > topBound) {
        scrollY = topBound;
    }
    // Don't lose the rounded component
    // bangbang.S
    //        mLastMotionX += scrollX - (int) scrollX;
    mLastMotionY += scrollY - (int) scrollY;
    scrollTo(getScrollX(), (int) scrollY);
    pageScrolled((int) scrollY);

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

From source file:com.gome.ecmall.custom.VerticalViewPager.java

/**
 * Fake drag by an offset in pixels. You must have called {@link #beginFakeDrag()} first.
 * //  w ww.  j  av a  2 s . co m
 * @param xOffset
 *            Offset in pixels to drag by.
 * @see #beginFakeDrag()
 * @see #endFakeDrag()
 */
public void fakeDragBy(float yOffset) {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    mLastMotionY += yOffset;

    float oldScrollY = getScrollY();
    float scrollY = oldScrollY - yOffset;
    final int height = getHeight();

    float topBound = height * mFirstOffset;
    float bottomBound = height * mLastOffset;

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

    if (scrollY < topBound) {
        scrollY = topBound;
    } else if (scrollY > bottomBound) {
        scrollY = bottomBound;
    }
    // Don't lose the rounded component
    mLastMotionY += scrollY - (int) scrollY;
    scrollTo(getScrollX(), (int) scrollY);
    pageScrolled((int) scrollY);

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