android MotionEvent.obtain模拟事件,自动触发

view.setOnTouchListener(new OnTouchListener()
{
    public boolean onTouch(View v, MotionEvent event)
    {
        Toast toast = Toast.makeText(
            getApplicationContext(), 
            "View touched", 
            Toast.LENGTH_LONG
        );
        toast.show();

        return true;
    }
});


// Obtain MotionEvent object
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
    downTime, 
    eventTime, 
    MotionEvent.ACTION_UP, 
    x, 
    y, 
    metaState
);

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



 
private void twoPointTouch(float xOneStart, float yOneStart, float xOneEnd, float yOneEnd,
            float xTwoStart, float yTwoStart, float xTwoEnd, float yTwoEnd, int stepCount) {
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis();
         
        MotionEvent.PointerCoords pointerCoordsOneStart = new MotionEvent.PointerCoords();
        pointerCoordsOneStart.x = xOneStart;
        pointerCoordsOneStart.y = yOneStart;
         
        MotionEvent.PointerCoords pointerCoordsTwoStart = new MotionEvent.PointerCoords();
        pointerCoordsTwoStart.x = xTwoStart;
        pointerCoordsTwoStart.y = yTwoStart;
         
        MotionEvent.PointerCoords pointerCoordsOneEnd = new MotionEvent.PointerCoords();
        pointerCoordsOneEnd.x = xOneEnd;
        pointerCoordsOneEnd.y = yOneEnd;
         
        MotionEvent.PointerCoords pointerCoordsTwoEnd = new MotionEvent.PointerCoords();
        pointerCoordsTwoEnd.x = xTwoEnd;
        pointerCoordsTwoEnd.y = yTwoEnd;
         
        float xOneStep = (xOneEnd - xOneStart) / stepCount;
        float yOneStep = (yOneEnd - yOneStart) / stepCount;
         
        float xTwoStep = (xTwoEnd - xTwoStart) / stepCount;
        float yTwoStep = (yTwoEnd - xTwoStart) / stepCount;
     
        MotionEvent.PointerCoords pointerCoordsOneStep = new MotionEvent.PointerCoords();
        pointerCoordsOneStep.x = xOneStart;
        pointerCoordsOneStep.y = yOneStart;
         
        MotionEvent.PointerCoords pointerCoordsTwoStep = new MotionEvent.PointerCoords();
        pointerCoordsTwoStep.x = xTwoStart;
        pointerCoordsTwoStep.y = yTwoStart;
         
        MotionEvent event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN,
                2, new int[] {0, 1}, new MotionEvent.PointerCoords[] {pointerCoordsOneStart, pointerCoordsTwoStart}, 0, 0.0f,
                0.0f, 0, 0, 0, 0);
        Log.e(LOG_TAG, "this event has " + event.getPointerCount() + "action is " + event.getAction());
        inst.sendPointerSync(event);
        inst.waitForIdleSync();
         
        for (int i = 0; i < stepCount; ++i) {
            pointerCoordsOneStep.x += xOneStep;
            pointerCoordsOneStep.y += yOneStep;
             
            pointerCoordsTwoStep.x += xTwoStep;
            pointerCoordsTwoStep.y += yTwoStep;
             
            eventTime = SystemClock.uptimeMillis();
            event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_MOVE,
                    2, new int[] {0, 1}, new MotionEvent.PointerCoords[] {pointerCoordsOneStep, pointerCoordsTwoStep}, 0, 0.0f,
                    0.0f, 0, 0, 0, 0);
            Log.e(LOG_TAG, "pointerCoordsOneStep x is " + pointerCoordsOneStep.x);
            Log.e(LOG_TAG, "pointerCoordsOneStep y is " + pointerCoordsOneStep.y);
             
            Log.i(LOG_TAG, "pointerCoordsTwoStep x is " + pointerCoordsTwoStep.x);
            Log.i(LOG_TAG, "pointerCoordsTwoStep y is " + pointerCoordsTwoStep.y);
            inst.sendPointerSync(event);
            inst.waitForIdleSync();
        }
         
        eventTime = SystemClock.uptimeMillis();
        event = MotionEvent.obtain(downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP,
                2, new int[] {0, 1}, new MotionEvent.PointerCoords[] {pointerCoordsOneEnd, pointerCoordsTwoEnd}, 0, 0.0f,
                0.0f, 0, 0, 0, 0);
         
         
        inst.sendPointerSync(event);
        inst.waitForIdleSync();
         
    

        
    



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值