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:cc.flydev.launcher.Folder.java

public void onDragOver(DragObject d) {
    final DragView dragView = d.dragView;
    final int scrollOffset = mScrollView.getScrollY();
    final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
    r[0] -= getPaddingLeft();/*from   ww  w  .j  a v  a 2s.  c  o m*/
    r[1] -= getPaddingTop();

    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent translatedEv = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y,
            0);

    if (!mAutoScrollHelper.isEnabled()) {
        mAutoScrollHelper.setEnabled(true);
    }

    final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
    translatedEv.recycle();

    if (handled) {
        mReorderAlarm.cancelAlarm();
    } else {
        mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
        if (isLayoutRtl()) {
            mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
        }
        if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
            mReorderAlarm.cancelAlarm();
            mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
            mReorderAlarm.setAlarm(REORDER_DELAY);
            mPreviousTargetCell[0] = mTargetCell[0];
            mPreviousTargetCell[1] = mTargetCell[1];
            mDragMode = DRAG_MODE_REORDER;
        } else {
            mDragMode = DRAG_MODE_NONE;
        }
    }
}

From source file:com.llf.android.launcher3.Folder.java

@Override
public void onDragOver(DragObject d) {
    final DragView dragView = d.dragView;
    final int scrollOffset = mScrollView.getScrollY();
    final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
    r[0] -= getPaddingLeft();/*from w w  w  . j ava2  s  . c o  m*/
    r[1] -= getPaddingTop();

    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent translatedEv = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y,
            0);

    if (!mAutoScrollHelper.isEnabled()) {
        mAutoScrollHelper.setEnabled(true);
    }

    final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
    translatedEv.recycle();

    if (handled) {
        mReorderAlarm.cancelAlarm();
    } else {
        mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
        if (isLayoutRtl()) {
            mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
        }
        if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
            mReorderAlarm.cancelAlarm();
            mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
            mReorderAlarm.setAlarm(REORDER_DELAY);
            mPreviousTargetCell[0] = mTargetCell[0];
            mPreviousTargetCell[1] = mTargetCell[1];
            mDragMode = DRAG_MODE_REORDER;
        } else {
            mDragMode = DRAG_MODE_NONE;
        }
    }
}

From source file:com.aohuan.dodo.dispatchevent_about.dummy_demo.view1.NestedScrollWebView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = false;

    MotionEvent trackedEvent = MotionEvent.obtain(event);

    final int action = MotionEventCompat.getActionMasked(event);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;/*  w  ww  .  j a va2s  .co  m*/
    }

    int y = (int) event.getY();

    event.offsetLocation(0, mNestedYOffset);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = y;
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        result = super.onTouchEvent(event);
        break;
    case MotionEvent.ACTION_MOVE:
        int deltaY = mLastMotionY - y;

        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            trackedEvent.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }

        int oldY = getScrollY();
        mLastMotionY = y - mScrollOffset[1];
        if (deltaY < 0) {
            int newScrollY = Math.max(0, oldY + deltaY);
            deltaY -= newScrollY - oldY;
            if (dispatchNestedScroll(0, newScrollY - deltaY, 0, deltaY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                trackedEvent.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }
        }

        trackedEvent.recycle();
        result = super.onTouchEvent(trackedEvent);
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        stopNestedScroll();
        result = super.onTouchEvent(event);
        break;
    }
    return result;
}

From source file:com.achep.acdisplay.ui.fragments.AcDisplayFragment.java

private void populateStdMotion(@NonNull MotionEvent srcEvent) {
    // Track current movement to be able to handle
    // flings correctly.
    MotionEvent dstEvent = MotionEvent.obtainNoHistory(srcEvent);
    mVelocityTracker.addMovement(MotionEvent.obtainNoHistory(srcEvent));
    dstEvent.recycle();

    // No need to handle swipe-to-dismiss if the
    // widget is not dismissible.
    if (!isDismissible(mSelectedWidget)) {
        return;//from   w  ww. java2s. com
    }

    final float y = srcEvent.getY() - mIconsContainer.getHeight();

    if (y <= 0) {
        if (mSceneContainer.getTranslationY() != 0) {
            resetSceneContainerParams();
        }
        return;
    }

    // Populate current animation
    float height = getSceneView().getHeight();
    float progress = MathUtils.range(y / height, 0f, 1f);
    populateStdAnimation(progress);
}

From source file:com.just.agentweb.NestedScrollAgentWebView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = false;

    MotionEvent trackedEvent = MotionEvent.obtain(event);

    final int action = MotionEventCompat.getActionMasked(event);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;/*from   ww w .  j  a v a2s. co m*/
    }

    int y = (int) event.getY();

    event.offsetLocation(0, mNestedYOffset);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = y;
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        result = super.onTouchEvent(event);
        break;
    case MotionEvent.ACTION_MOVE:
        int deltaY = mLastMotionY - y;

        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            trackedEvent.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }

        mLastMotionY = y - mScrollOffset[1];

        int oldY = getScrollY();
        int newScrollY = Math.max(0, oldY + deltaY);
        int dyConsumed = newScrollY - oldY;
        int dyUnconsumed = deltaY - dyConsumed;

        if (dispatchNestedScroll(0, dyConsumed, 0, dyUnconsumed, mScrollOffset)) {
            mLastMotionY -= mScrollOffset[1];
            trackedEvent.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }

        result = super.onTouchEvent(trackedEvent);
        trackedEvent.recycle();
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        stopNestedScroll();
        result = super.onTouchEvent(event);
        break;
    }
    return result;
}

From source file:akhil.alltrans.NestedScrollingMarkDownView.java

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean result = false;

    MotionEvent trackedEvent = MotionEvent.obtain(event);

    final int action = MotionEventCompat.getActionMasked(event);

    if (action == MotionEvent.ACTION_DOWN) {
        mNestedYOffset = 0;//from  www  . j  a  v a 2 s  .c  om
    }

    int y = (int) event.getY();

    event.offsetLocation(0, mNestedYOffset);

    switch (action) {
    case MotionEvent.ACTION_DOWN:
        mLastMotionY = y;
        startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
        result = super.onTouchEvent(event);
        break;
    case MotionEvent.ACTION_MOVE:
        int deltaY = mLastMotionY - y;

        if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
            deltaY -= mScrollConsumed[1];
            trackedEvent.offsetLocation(0, mScrollOffset[1]);
            mNestedYOffset += mScrollOffset[1];
        }

        int oldY = getScrollY();
        mLastMotionY = y - mScrollOffset[1];
        if (deltaY < 0) {
            int newScrollY = Math.max(0, oldY + deltaY);
            deltaY -= newScrollY - oldY;
            if (dispatchNestedScroll(0, newScrollY - deltaY, 0, deltaY, mScrollOffset)) {
                mLastMotionY -= mScrollOffset[1];
                trackedEvent.offsetLocation(0, mScrollOffset[1]);
                mNestedYOffset += mScrollOffset[1];
            }
        }

        result = super.onTouchEvent(trackedEvent);
        trackedEvent.recycle();
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        stopNestedScroll();
        result = super.onTouchEvent(event);
        break;
    }
    return result;
}

From source file:com.wb.launcher3.Folder.java

public void onDragOver(DragObject d) {
    final DragView dragView = d.dragView;
    final int scrollOffset = mScrollView.getScrollY();
    final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
    r[0] -= getPaddingLeft();//from   www . j  a  v  a 2  s  .  c  om
    r[1] -= getPaddingTop();

    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent translatedEv = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y,
            0);

    if (!mAutoScrollHelper.isEnabled()) {
        mAutoScrollHelper.setEnabled(true);
    }

    final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
    translatedEv.recycle();

    if (handled) {
        mReorderAlarm.cancelAlarm();
    } else {
        mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
        if (isLayoutRtl()) {
            mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
        }
        if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
            mReorderAlarm.cancelAlarm();
            mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
            mReorderAlarm.setAlarm(REORDER_DELAY);
            mPreviousTargetCell[0] = mTargetCell[0];
            mPreviousTargetCell[1] = mTargetCell[1];
            mDragMode = DRAG_MODE_REORDER;
        } else {
            mDragMode = DRAG_MODE_NONE;
        }
    }
    //*/
}

From source file:com.android.launcher3.Folder.java

public void onDragOver(DragObject d) {
    final DragView dragView = d.dragView;
    final int scrollOffset = mScrollView.getScrollY();
    final float[] r = getDragViewVisualCenter(d.x, d.y, d.xOffset, d.yOffset, dragView, null);
    r[0] -= getPaddingLeft();//from   w w w.j a v a2 s . c  o m
    r[1] -= getPaddingTop();

    final long downTime = SystemClock.uptimeMillis();
    final MotionEvent translatedEv = MotionEvent.obtain(downTime, downTime, MotionEvent.ACTION_MOVE, d.x, d.y,
            0);

    if (!mAutoScrollHelper.isEnabled()) {
        mAutoScrollHelper.setEnabled(true);
    }

    final boolean handled = mAutoScrollHelper.onTouch(this, translatedEv);
    translatedEv.recycle();

    if (handled) {
        mReorderAlarm.cancelAlarm();
    } else {
        mTargetCell = mContent.findNearestArea((int) r[0], (int) r[1] + scrollOffset, 1, 1, mTargetCell);
        if (isLayoutRtl()) {
            mTargetCell[0] = mContent.getCountX() - mTargetCell[0] - 1;
        }
        if (mTargetCell[0] != mPreviousTargetCell[0] || mTargetCell[1] != mPreviousTargetCell[1]) {
            mReorderAlarm.cancelAlarm();
            mReorderAlarm.setOnAlarmListener(mReorderAlarmListener);
            mReorderAlarm.setAlarm(REORDER_DELAY);
            mPreviousTargetCell[0] = mTargetCell[0];
            mPreviousTargetCell[1] = mTargetCell[1];
        }
    }
}

From source file:com.yao.zhihudaily.tool.LazyViewPager.java

/**
 * Fake drag by an offset in pixels. You must have called {@link #beginFakeDrag()} first.
 *
 * @param xOffset Offset in pixels to drag by.
 * @see #beginFakeDrag()/*from   w w  w  .  jav  a2  s  . c  om*/
 * @see #endFakeDrag()
 */
public void fakeDragBy(float xOffset) {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }
    mLastMotionX += xOffset;
    float scrollX = getScrollX() - xOffset;
    final int width = getWidth();
    final int widthWithMargin = width + mPageMargin;
    final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
    final float rightBound = Math.min(mCurItem + 1, mAdapter.getCount() - 1) * widthWithMargin;
    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());
    if (mOnPageChangeListener != null) {
        final int position = (int) scrollX / widthWithMargin;
        final int positionOffsetPixels = (int) scrollX % widthWithMargin;
        final float positionOffset = (float) positionOffsetPixels / widthWithMargin;
        mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
    }
    // 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();
}

From source file:com.jzh.stuapp.view.MyViewPager.java

/**
 * Fake drag by an offset in pixels. You must have called
 * {@link #beginFakeDrag()} first.//from   w ww. ja v  a 2 s  .c om
 * 
 * @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.");
    }

    mLastMotionX += xOffset;
    float scrollX = getScrollX() - xOffset;
    final int width = getWidth();
    final int widthWithMargin = width + mPageMargin;

    final float leftBound = Math.max(0, (mCurItem - 1) * widthWithMargin);
    final float rightBound = Math.min(mCurItem + 1, mAdapter.getCount() - 1) * widthWithMargin;
    if (scrollX < leftBound) {
        scrollX = leftBound;
    } else if (scrollX > rightBound) {
        scrollX = rightBound;
    }
    mLastMotionX += scrollX - (int) scrollX;
    scrollTo((int) scrollX, getScrollY());
    if (mOnPageChangeListener != null) {
        final int position = (int) scrollX / widthWithMargin;
        final int positionOffsetPixels = (int) scrollX % widthWithMargin;
        final float positionOffset = (float) positionOffsetPixels / widthWithMargin;
        mOnPageChangeListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
    }

    final long time = SystemClock.uptimeMillis();
    final MotionEvent ev = MotionEvent.obtain(mFakeDragBeginTime, time, MotionEvent.ACTION_MOVE, mLastMotionX,
            0, 0);
    mVelocityTracker.addMovement(ev);
    ev.recycle();
}