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, int pointerCount,
        PointerProperties[] pointerProperties, PointerCoords[] pointerCoords, int metaState, int buttonState,
        float xPrecision, float yPrecision, int deviceId, int edgeFlags, int source, int flags) 

Source Link

Document

Create a new MotionEvent, filling in all of the basic values that define the motion.

Usage

From source file:Main.java

public static MotionEvent hoverMotionEventAtPosition(View view, int action, int xPercent, int yPercent) {
    MotionEvent ev = motionEventAtPosition(view, action, xPercent, yPercent);

    MotionEvent.PointerProperties[] pointerProperties = new MotionEvent.PointerProperties[1];
    pointerProperties[0] = new MotionEvent.PointerProperties();

    MotionEvent.PointerCoords[] pointerCoords = new MotionEvent.PointerCoords[1];
    pointerCoords[0] = new MotionEvent.PointerCoords();
    pointerCoords[0].x = ev.getX();//from ww w  . ja  va2  s .  c  om
    pointerCoords[0].y = ev.getY();

    return MotionEvent.obtain(ev.getDownTime(), ev.getEventTime(), ev.getAction(), 1, pointerProperties,
            pointerCoords, ev.getMetaState(), 0, ev.getXPrecision(), ev.getYPrecision(), ev.getDeviceId(),
            ev.getEdgeFlags(), InputDevice.SOURCE_CLASS_POINTER, ev.getFlags());
}

From source file:Main.java

public static MotionEvent CreateMotionEventFromJSonObject(JSONObject jObj) throws JSONException {
    long uptime = SystemClock.uptimeMillis();
    //get pointer infos
    JSONArray array = jObj.optJSONArray("pointers");
    int arrlen = array != null ? array.length() : 1;
    PointerProperties[] props = new PointerProperties[arrlen];
    PointerCoords[] coords = new PointerCoords[arrlen];

    if (array != null) {
        for (int i = 0; i < array.length(); i++) {
            PointerProperties prop = new PointerProperties();
            PointerCoords coord = new PointerCoords();
            PointerValuesFromJSonObj(prop, coord, array.getJSONObject(i));
            props[i] = prop;/*from w  w w .j  a  va 2 s  .  com*/
            coords[i] = coord;
        }
    } else {
        JSONObject pointer = jObj.optJSONObject("pointers");
        if (pointer != null) {
            PointerProperties prop = new PointerProperties();
            PointerCoords coord = new PointerCoords();
            PointerValuesFromJSonObj(prop, coord, pointer);
            props[0] = prop;
            coords[0] = coord;
        }
    }
    long downTimeOffset = jObj.getLong("eventTime") - jObj.getLong("downTime");
    /* TODO calculating the time offset is not a goog solution. the downtime should be the same of all events in a row.
     * for an optimal use the events should be traced. then at the first occurency both down and event time
     * are set from uptime. and then in the follow up events the uptime is used for event time and the stored downtime 
     * for the downtime. but for this some identification of events is needed. 
     * first events can be identified with (eventTime == downTime)
     * */

    MotionEvent event = MotionEvent.obtain(uptime - downTimeOffset, uptime, jObj.getInt("action"),
            jObj.getInt("pointerCount"), props, coords, jObj.getInt("metaState"), jObj.getInt("buttonState"),
            (float) jObj.getDouble("xPrecision"), (float) jObj.getDouble("yPrecision"), jObj.getInt("deviceId"),
            jObj.getInt("edgeFlags"), jObj.getInt("source"), jObj.getInt("flags"));
    return event;
}