Example usage for android.view MotionEvent obtain

List of usage examples for android.view MotionEvent obtain

Introduction

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

Prototype

static public MotionEvent obtain(long downTime, long eventTime, int action, float x, float y, int metaState) 

Source Link

Document

Create a new MotionEvent, filling in a subset of the basic motion values.

Usage

From source file:com.telerik.examples.primitives.ExampleViewPagerBase.java

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

    mLastMotionX += offset;

    float oldScroll = getScroll(this);
    float scroll = oldScroll - offset;
    final int clientLength = getClientLength();

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

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

    if (scroll < leftBound) {
        scroll = leftBound;
    } else if (scroll > rightBound) {
        scroll = rightBound;
    }
    // Don't lose the rounded component
    mLastMotionX = mLastMotionX + (scroll - (int) scroll);
    if (this.orientation == LinearLayout.HORIZONTAL) {
        scrollTo((int) scroll, getInvertedScroll(this));
    } else {
        scrollTo(getInvertedScroll(this), (int) scroll);
    }
    pageScrolled((int) scroll);

    // 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.av.remusic.widget.RoundViewPager.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  .ja  v a2 s.co m*/
 * @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();
}

From source file:com.tjych.swip.vertical.DirectionalViewPager.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 .  j  a v a  2s.  c o m*/
 * @see #endFakeDrag()
 */
public void fakeDragBy(float offset) {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    if (mOrientation == HORIZONTAL) {
        mLastMotionX += offset;
    } else {
        mLastMotionY += offset;
    }

    float oldScrollCoord = getScrollCoord();
    float scrollCoord = oldScrollCoord - offset;
    final int dimension = getClientDimension();

    float startBound = dimension * mFirstOffset;
    float endBound = dimension * mLastOffset;

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

    if (scrollCoord < startBound) {
        scrollCoord = startBound;
    } else if (scrollCoord > endBound) {
        scrollCoord = endBound;
    }
    // Don't lose the rounded component
    if (mOrientation == HORIZONTAL) {
        mLastMotionX += scrollCoord - (int) scrollCoord;
        scrollTo((int) scrollCoord, getScrollY());
    } else {
        mLastMotionY += scrollCoord - (int) scrollCoord;
        scrollTo(getScrollX(), (int) scrollCoord);
    }

    pageScrolled((int) scrollCoord);

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

From source file:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

/**
 * Start a fake drag of the pager./*from  ww w. ja 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;
    }
    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 2 s . c  o  m
 * <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.viewpagerindicator.MyDirectionalViewPager.java

/**
 * Start a fake drag of the pager./* ww w .j a  v a2  s.com*/
 *
 * <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 (mOrientation == HORIZONTAL) {
        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:cn.androidy.materialdesignsample.ryanharterviewpager.ViewPager.java

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

    if (isOrientationHorizontal()) {
        mLastMotionX += offset;

        float oldScrollX = getScrollX();
        float scrollX = oldScrollX - offset;
        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();
    } else {
        mLastMotionY += offset;

        float oldScrollY = getScrollY();
        float scrollY = oldScrollY - offset;
        final int height = getClientHeight();

        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 * height;
        }

        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();
    }
}

From source file:android.improving.utils.views.cardsview.OrientedViewPager.java

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

    if (mOrientation == Orientation.VERTICAL) {
        mLastMotionY += offset;

        float oldScrollY = getScrollY();
        float scrollY = oldScrollY - offset;
        final int height = getClientSize();

        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 * height;
        }

        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();
    } else {
        mLastMotionX += offset;

        float oldScrollX = getScrollX();
        float scrollX = oldScrollX - offset;
        final int width = getClientSize();

        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();
    }
}

From source file:com.viewpagerindicator.MyDirectionalViewPager.java

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

    if (mOrientation == HORIZONTAL) {
        mLastMotionX += offset;
    } else {
        mLastMotionY += offset;
    }

    float oldScroll;
    float scroll;
    final int size;
    if (mOrientation == HORIZONTAL) {
        oldScroll = getScrollX();
        scroll = oldScroll - offset;
        size = getClientWidth();
    } else {
        oldScroll = getScrollY();
        scroll = oldScroll - offset;
        size = getClientHeight();
    }

    float preBound = size * mFirstOffset;
    float nextBound = size * mLastOffset;

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

    if (scroll < preBound) {
        scroll = preBound;
    } else if (scroll > nextBound) {
        scroll = nextBound;
    }
    // Don't lose the rounded component
    if (mOrientation == HORIZONTAL) {
        mLastMotionX += scroll - (int) scroll;
        scrollTo((int) scroll, getScrollY());
    } else {
        mLastMotionY += scroll - (int) scroll;
        scrollTo(getScrollX(), (int) scroll);
    }
    pageScrolled((int) scroll);

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

From source file:dev.dworks.libs.widget.ViewPager.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()//  ww  w.  j ava2 s .co m
 * @see #endFakeDrag()
 */
public void fakeDragByHorizontally(float xOffset) {
    if (!mFakeDragging) {
        throw new IllegalStateException("No fake drag in progress. Call beginFakeDrag first.");
    }

    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());
    pageScrolledX((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();
}