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:Main.java

public static void dispatchCancelEvent(View view) {
    view.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
            MotionEvent.ACTION_CANCEL, 0, 0, 0));
}

From source file:Main.java

public static void setKeyboardFocus(final EditText mEditText) {
    (new Handler()).postDelayed(new Runnable() {
        public void run() {
            mEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0));
            mEditText.dispatchTouchEvent(MotionEvent.obtain(SystemClock.uptimeMillis(),
                    SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 0, 0, 0));
        }//from   w w w. ja va2 s.co  m
    }, 100);
}

From source file:Main.java

private static void sendAction(View view, int action, long downTime, float x, float y) {
    long eventTime = SystemClock.uptimeMillis();
    MotionEvent event = MotionEvent.obtain(downTime, eventTime, action, x, y, 0);
    view.onTouchEvent(event);/* w  w w .ja  v a  2s.c om*/
}

From source file:Main.java

public static void dispatchTouch(final View view, final long duration) {
    final long downTime = SystemClock.uptimeMillis();
    final long eventTime = SystemClock.uptimeMillis();
    final float x = view.getWidth() / 3;//0.0f;
    final float y = view.getHeight() / 3;//0.0f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    final int metaState = 0;
    MotionEvent motionEvent = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, metaState);

    // Dispatch touch event to view
    view.dispatchTouchEvent(motionEvent);

    new Handler().postDelayed(new Runnable() {
        @Override//from ww  w . j a  v  a2  s  .  c om
        public void run() {
            MotionEvent motionEvent = MotionEvent.obtain(downTime + duration, eventTime + duration,
                    MotionEvent.ACTION_UP, x, y, metaState);
            view.dispatchTouchEvent(motionEvent);
        }
    }, duration);
}

From source file:Main.java

/**
 * Performs a single touch on the center of the supplied view.
 * This is safe to call from the instrumentation thread and will invoke the touch
 * asynchronously./*www  .j ava 2s  .  co m*/
 *
 * @param view The view the coordinates are relative to.
 */
public static void simulateTouchCenterOfView(final View view) throws Throwable {
    view.post(new Runnable() {
        @Override
        public void run() {
            long eventTime = SystemClock.uptimeMillis();
            float x = (float) (view.getRight() - view.getLeft()) / 2;
            float y = (float) (view.getBottom() - view.getTop()) / 2;
            view.onTouchEvent(MotionEvent.obtain(eventTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0));
            view.onTouchEvent(MotionEvent.obtain(eventTime, eventTime, MotionEvent.ACTION_UP, x, y, 0));
        }
    });
}

From source file:Main.java

public static List<MotionEvent> createMotionEvents(final AbsListView absListView, final float fromY,
        final float toY) {
    int x = (int) (absListView.getX() + absListView.getWidth() / 2);

    List<MotionEvent> results = new ArrayList<>();
    results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
            MotionEvent.ACTION_DOWN, x, fromY, 0));

    float diff = (toY - fromY) / 25;
    float y = fromY;
    for (int i = 0; i < 25; i++) {
        y += diff;/*from  ww  w.j a va2 s.  c  o m*/
        results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
                MotionEvent.ACTION_MOVE, x, y, 0));
    }
    results.add(MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
            MotionEvent.ACTION_UP, x, toY, 0));

    return results;
}

From source file:Main.java

public static MotionEvent motionEventAtPosition(View view, int action, int xPercent, int yPercent) {
    // NOTE: This method is not perfect. If you send touch events in a granular nature, you'll
    // see varying results of accuracy depending on the size of the jump.

    int paddingLeft = view.getPaddingLeft();
    int paddingRight = view.getPaddingRight();
    int paddingTop = view.getPaddingTop();
    int paddingBottom = view.getPaddingBottom();

    int width = view.getWidth();
    int height = view.getHeight();

    int[] topLeft = new int[2];
    view.getLocationInWindow(topLeft);//www. ja v  a 2s .  c  om
    int x1 = topLeft[0] + paddingLeft;
    int y1 = topLeft[1] + paddingTop;
    int x2 = x1 + width - paddingLeft - paddingRight;
    int y2 = y1 + height - paddingTop - paddingBottom;

    float x = x1 + ((x2 - x1) * xPercent / 100f);
    float y = y1 + ((y2 - y1) * yPercent / 100f);

    long time = SystemClock.uptimeMillis();
    return MotionEvent.obtain(time, time, action, x, y, 0);
}

From source file:Main.java

public static MotionEvent motionEventAtPosition(View view, int action, int position) {
    // NOTE: This method is not perfect. If you send touch events in a granular nature, you'll
    // see varying results of accuracy depending on the size of the jump.

    int paddingLeft = view.getPaddingLeft();
    int paddingRight = view.getPaddingRight();
    int paddingTop = view.getPaddingTop();
    int paddingBottom = view.getPaddingBottom();

    int width = view.getWidth();
    int height = view.getHeight();

    int topLeft[] = new int[2];
    view.getLocationInWindow(topLeft);/*from   ww w  .java 2  s  .c om*/
    int x1 = topLeft[0] + paddingLeft;
    int y1 = topLeft[1] + paddingTop;
    int x2 = x1 + width - paddingLeft - paddingRight;
    int y2 = y1 + height - paddingTop - paddingBottom;

    float x = x1 + ((x2 - x1) * position / 100f);
    float y = y1 + ((y2 - y1) / 2f);

    long time = SystemClock.uptimeMillis();
    return MotionEvent.obtain(time, time, action, x, y, 0);
}

From source file:net.hoodalabs.cordova.plugins.touchevent.HoodalabsTouchEvent.java

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext)
        throws JSONException {

    if ("fireAt".equals(action)) {

        final float x = args.getInt(0);
        final float y = args.getInt(1);

        // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
        final int metaState = 0;

        final long timestamp = SystemClock.uptimeMillis();

        MotionEvent touchStart = MotionEvent.obtain(timestamp, timestamp + 50, MotionEvent.ACTION_DOWN, x, y,
                metaState);//from  www . j  a  v  a 2 s .co m
        touchStart.setSource(InputDevice.SOURCE_TOUCHSCREEN);

        webView.dispatchTouchEvent(touchStart);

        MotionEvent touchEnd = MotionEvent.obtain(timestamp + 50, timestamp + 100, MotionEvent.ACTION_UP, x, y,
                metaState);
        touchEnd.setSource(InputDevice.SOURCE_TOUCHSCREEN);

        webView.dispatchTouchEvent(touchEnd);

        callbackContext.success(); // Thread-safe.
        return true;
    }

    return false;
}

From source file:com.hippo.ehviewer.gallery.gl.GestureRecognizer.java

public void cancelScale() {
    long now = SystemClock.uptimeMillis();
    MotionEvent cancelEvent = MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0, 0, 0);
    mScaleDetector.onTouchEvent(cancelEvent);
    cancelEvent.recycle();//  w  ww  .ja  v  a 2 s  .  c  o m
}