Android MotionEvent.obtain方法使用

2025-11-26 06:49:25

1、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;

Android MotionEvent.obtain方法使用

2、// 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);

Android MotionEvent.obtain方法使用

3、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;

Android MotionEvent.obtain方法使用

4、        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;

Android MotionEvent.obtain方法使用

5、        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);

Android MotionEvent.obtain方法使用

6、            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();

Android MotionEvent.obtain方法使用

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢