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.cnpeng.cnpeng_mydemosfrom2016_12.a_12_GetLocalFiles_VP_FM.CustomNoPreLoadViewPager.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. c o  m*/
 * @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:ca.co.rufus.androidboilerplate.ui.DebugDrawerLayout.java

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);
        }/*from  w ww  .  ja  v  a 2  s  .  c  o  m*/
        cancelEvent.recycle();
        mChildrenCanceledTouch = true;
    }
}

From source file:bw.com.yunifangstore.view.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 ww  .j av a 2 s  .c  o  m*/
 * @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.chenglong.muscle.ui.LazyViewPager.java

/**
 * Fake drag by an offset in pixels. You must have called
 * {@link #beginFakeDrag()} first./*from  ww  w.  j av a2s .com*/
 *
 * @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;
    }
    // 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:beichen.douban.ui.view.LazyViewPager.java

/**
 * Fake drag by an offset in pixels. You must have called
 * {@link #beginFakeDrag()} first.//from w  w  w.  j ava 2s .c  o  m
 * 
 * @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;
    }
    // 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:org.immopoly.android.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()/*from  w  w w  .j a  v  a 2 s  .c o m*/
 * @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 = forcedChildWidth > 0 ? forcedChildWidth : 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());
    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:org.catrobat.catroid.uitest.util.UiTestUtils.java

public static void longClickAndDrag(final Solo solo, final float xFrom, final float yFrom, final float xTo,
        final float yTo, final int steps) {
    final Activity activity = solo.getCurrentActivity();
    Handler handler = new Handler(activity.getMainLooper());

    handler.post(new Runnable() {

        public void run() {
            MotionEvent downEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_DOWN, xFrom, yFrom, 0);
            activity.dispatchTouchEvent(downEvent);
            downEvent.recycle();//  ww w. j a va 2 s.co  m
        }
    });

    solo.sleep(ViewConfiguration.getLongPressTimeout() + 200);

    handler.post(new Runnable() {
        public void run() {
            double offsetX = xTo - xFrom;
            offsetX /= steps;
            double offsetY = yTo - yFrom;
            offsetY /= steps;
            for (int i = 0; i <= steps; i++) {
                float x = xFrom + (float) (offsetX * i);
                float y = yFrom + (float) (offsetY * i);
                MotionEvent moveEvent = MotionEvent.obtain(SystemClock.uptimeMillis(),
                        SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE, x, y, 0);
                activity.dispatchTouchEvent(moveEvent);

                solo.sleep(20);
                moveEvent.recycle();
            }
        }
    });

    solo.sleep(steps * 20 + 200);

    handler.post(new Runnable() {

        public void run() {
            MotionEvent upEvent = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                    MotionEvent.ACTION_UP, xTo, yTo, 0);
            activity.dispatchTouchEvent(upEvent);
            upEvent.recycle();
            Log.d(TAG, "longClickAndDrag finished: " + (int) yTo);
        }
    });

    solo.sleep(1000);
}

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

/**
 * Start a fake drag of the pager./*from  w w  w. j  ava2 s.  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

/**
 * Start a fake drag of the pager.//from  w  w  w .j  a  va  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:android.webkit.cts.WebViewTest.java

public void testRequestImageRef() throws Exception, Throwable {
    if (!NullWebViewUtils.isWebViewAvailable()) {
        return;//from ww w .  j a  va  2 s  .  c  o  m
    }
    final class ImageLoaded {
        public boolean mImageLoaded;

        @JavascriptInterface
        public void loaded() {
            mImageLoaded = true;
        }
    }
    final ImageLoaded imageLoaded = new ImageLoaded();
    runTestOnUiThread(new Runnable() {
        public void run() {
            mOnUiThread.getSettings().setJavaScriptEnabled(true);
        }
    });
    mOnUiThread.addJavascriptInterface(imageLoaded, "imageLoaded");
    AssetManager assets = getActivity().getAssets();
    Bitmap bitmap = BitmapFactory.decodeStream(assets.open(TestHtmlConstants.LARGE_IMG_URL));
    int imgWidth = bitmap.getWidth();
    int imgHeight = bitmap.getHeight();

    startWebServer(false);
    final String imgUrl = mWebServer.getAssetUrl(TestHtmlConstants.LARGE_IMG_URL);
    mOnUiThread.loadDataAndWaitForCompletion(
            "<html><head><title>Title</title><style type=\"text/css\">"
                    + "#imgElement { -webkit-transform: translate3d(0,0,1); }"
                    + "#imgElement.finish { -webkit-transform: translate3d(0,0,0);"
                    + " -webkit-transition-duration: 1ms; }</style>"
                    + "<script type=\"text/javascript\">function imgLoad() {"
                    + "imgElement = document.getElementById('imgElement');"
                    + "imgElement.addEventListener('webkitTransitionEnd',"
                    + "function(e) { imageLoaded.loaded(); });" + "imgElement.className = 'finish';}</script>"
                    + "</head><body><img id=\"imgElement\" src=\"" + imgUrl + "\" width=\"" + imgWidth
                    + "\" height=\"" + imgHeight + "\" onLoad=\"imgLoad()\"/></body></html>",
            "text/html", null);
    new PollingCheck() {
        @Override
        protected boolean check() {
            return imageLoaded.mImageLoaded;
        }
    }.run();
    getInstrumentation().waitForIdleSync();

    final HrefCheckHandler handler = new HrefCheckHandler(mWebView.getHandler().getLooper());
    final Message msg = new Message();
    msg.setTarget(handler);

    // touch the image
    handler.reset();
    int[] location = mOnUiThread.getLocationOnScreen();

    long time = SystemClock.uptimeMillis();
    getInstrumentation().sendPointerSync(MotionEvent.obtain(time, time, MotionEvent.ACTION_DOWN,
            location[0] + imgWidth / 2, location[1] + imgHeight / 2, 0));
    getInstrumentation().waitForIdleSync();
    mOnUiThread.requestImageRef(msg);
    new PollingCheck() {
        @Override
        protected boolean check() {
            boolean done = false;
            if (handler.hasCalledHandleMessage()) {
                if (handler.mResultUrl != null) {
                    done = true;
                } else {
                    handler.reset();
                    Message newMsg = new Message();
                    newMsg.setTarget(handler);
                    mOnUiThread.requestImageRef(newMsg);
                }
            }
            return done;
        }
    }.run();
    assertEquals(imgUrl, handler.mResultUrl);
}