使用api 16和Webview(Android)的MotionEvent

时间:2013-12-20 15:07:04

标签: android webview motionevent

我想制作一个MotionEvent来控制webiew。如果api低于16,则一切都适用:

 long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 500;
    float x = 50f;
    float y = 50f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    int metaState = 0;
    MotionEvent me = MotionEvent.obtain(
        downTime, 
        eventTime, 
        action, 
        x, 
        y, 
        metaState
    );

   view.dispatchTouchEvent(me);

但是api更高,然后我得到一个错误:Source不是SOURCE_CLASS_PIONTER。然后我将代码更改为:

long downTime = SystemClock.uptimeMillis();
    long eventTime = SystemClock.uptimeMillis() + 500;
    float x = 50f;
    float y = 50f;
    // List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
    int metaState = 0;
    MotionEvent.PointerCoords p[] = null; 
    MotionEvent.PointerProperties p2[] = null;
  p[0] = new MotionEvent.PointerCoords();
p[0].x = x;
p[0].y = y;
p2[0] = new MotionEvent.PointerProperties();
p2[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
p2[0].id = 0;
MotionEvent me =  MotionEvent.obtain(downTime, eventTime, action, 1, p2, p, metaState, 0, 1f, 1f, 0,0, 0, 0);
    view.dispatchTouchEvent(me);

但它没有用。你能帮助我吗? 对不起,我的英语不好。

来自德国的问候。

1 个答案:

答案 0 :(得分:1)

  MotionEvent me = MotionEvent.obtain(
        downTime, 
        eventTime, 
        action, 
        x, 
        y, 
        metaState
    );
    me.setSource(4098);
   view.dispatchTouchEvent(me);

适合我。