日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

scrcpy学习--多点触控事件的模拟

發布時間:2023/12/29 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 scrcpy学习--多点触控事件的模拟 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

input tap x y 命令可以進行模擬點擊,但是只是一個點的事件,怎么進行多點模擬操作呢?

其實在MotionEvent里面把多點信息寫進去就可以了。

?

我們可以參考scrcpy里面的寫法,

https://github.com/Genymobile/scrcpy

?

??? private boolean injectTouch(int action, long pointerId, Position position, float pressure, int buttons) {

??????? long now = SystemClock.uptimeMillis();

?

??????? Point point = device.getPhysicalPoint(position);

??????? if (point == null) {

??????????? // ignore event

??????????? return false;

??????? }

?

??????? int pointerIndex = pointersState.getPointerIndex(pointerId);

??????? if (pointerIndex == -1) {

??????????? Ln.w("Too many pointers for touch event");

??????????? return false;

??????? }

// pointersState里對pointer進行添加等管理

??????? Pointer pointer = pointersState.get(pointerIndex);

??????? pointer.setPoint(point);

??????? pointer.setPressure(pressure);

??????? pointer.setUp(action == MotionEvent.ACTION_UP);

//這里設置pointerProperties, pointerCoords

??????? int pointerCount = pointersState.update(pointerProperties, pointerCoords);

?

??????? if (pointerCount == 1) {

??????????? if (action == MotionEvent.ACTION_DOWN) {

??????????????? lastTouchDown = now;

??????????? }

??????? } else {

//設置多點事件的action,如105,105,205

??????????? // secondary pointers must use ACTION_POINTER_* ORed with the pointerIndex

??????????? if (action == MotionEvent.ACTION_UP) {

??????????????? action = MotionEvent.ACTION_POINTER_UP | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);

??????????? } else if (action == MotionEvent.ACTION_DOWN) {

??????????????? action = MotionEvent.ACTION_POINTER_DOWN | (pointerIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT);

??????????? }

??????? }

?

??????? MotionEvent event = MotionEvent

??????????????? .obtain(lastTouchDown, now, action, pointerCount, pointerProperties, pointerCoords, 0, buttons, 1f, 1f, DEVICE_ID_VIRTUAL, 0,

??????????????????????? InputDevice.SOURCE_TOUCHSCREEN, 0);

??????? return device.injectEvent(event);

}

?

?

調用injectInputEvent方法

?

??? public boolean injectEvent(InputEvent event) {

??????? return injectEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_ASYNC);

??? }

?

??? public boolean injectEvent(InputEvent inputEvent, int mode) {

??????? if (!supportsInputEvents()) {

??????????? throw new AssertionError("Could not inject input event if !supportsInputEvents()");

??????? }

?

??????? if (displayId != 0 && !InputManager.setDisplayId(inputEvent, displayId)) {

??????????? return false;

??????? }

?

??????? return serviceManager.getInputManager().injectInputEvent(inputEvent, mode);

??? }

?

?

設置好幾個點的軌跡,組裝好MotionEvent, 就可以使用injectInputEvent來注入多點觸控事件了。MotionEvent的getActionIndex方法可以獲取到當前的操作的事件是哪個點發起的。

?

MotionEvent的log

?

03-14 14:03:41.892 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=357.66882, y[0]=888.5372, toolType[0]=TOOL_TYPE_FINGE

R, id[1]=1, x[1]=866.198, y[1]=1360.2915, toolType[1]=TOOL_TYPE_FINGER, buttonSt

ate=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=0, eve

ntTime=689571391, downTime=689567853, deviceId=8, source=0x1002 }

03-14 14:03:41.909 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=348.67715, y[0]=894.53406, toolType[0]=TOOL_TYPE_FING

ER, id[1]=1, x[1]=866.198, y[1]=1361.7908, toolType[1]=TOOL_TYPE_FINGER, buttonS

tate=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=1, ev

entTime=689571407, downTime=689567853, deviceId=8, source=0x1002 }

03-14 14:03:41.944 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=348.67715, y[0]=894.53406, toolType[0]=TOOL_TYPE_FING

ER, id[1]=1, x[1]=865.1989, y[1]=1362.1178, toolType[1]=TOOL_TYPE_FINGER, button

State=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=1, e

ventTime=689571445, downTime=689567853, deviceId=8, source=0x1002 }

03-14 14:03:41.961 28564 28564 D test614 : MotionEvent { action=ACTION_MOVE, act

ionButton=0, id[0]=0, x[0]=348.67715, y[0]=894.53406, toolType[0]=TOOL_TYPE_FING

ER, id[1]=1, x[1]=865.1989, y[1]=1362.79, toolType[1]=TOOL_TYPE_FINGER, buttonSt

ate=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=2, historySize=1, eve

ntTime=689571453, downTime=689567853, deviceId=8, source=0x1002 }

?

連接藍牙鼠標的點擊log

03-14 22:23:51.896 28564 28564 D test614 : MotionEvent { action=ACTION_DOWN, act

ionButton=0, id[0]=0, x[0]=270.19272, y[0]=632.4363, toolType[0]=TOOL_TYPE_MOUSE

, buttonState=BUTTON_PRIMARY, metaState=0, flags=0x0, edgeFlags=0x0, pointerCoun

t=1, historySize=0, eventTime=719579827, downTime=719579827, deviceId=16, source

=0x2002 }

03-14 22:23:52.052 28564 28564 D test614 : MotionEvent { action=ACTION_UP, actio

nButton=0, id[0]=0, x[0]=270.19272, y[0]=632.4363, toolType[0]=TOOL_TYPE_MOUSE,

buttonState=0, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySiz

e=0, eventTime=719579984, downTime=719579827, deviceId=16, source=0x2002 }

?

總結

以上是生活随笔為你收集整理的scrcpy学习--多点触控事件的模拟的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。