2018年12月8日 星期六

19-Level up !!! input draganddrop

拖放 (Drag and Drop) 是種很常見的操作,可是咱們曾介紹的 MonkeyDevice.draginput swipe 都難以漂亮地模擬此動作,並非不能,只是實現的效果非常不理想且醜陋。不過從 Android 8.0.0 開始,有一新的 input 成員能夠較為優雅地實現此模擬了,它便是 input draganddrop

語法 input draganddrop <x1> <y1> <x2> <y2> [ duration(ms) ]
說明 duration 預設為 300 ms。
看官們應該能發現它的使用方式和 18-input三兄弟swipe 一模一樣。

咱們立刻來看看使用 input draganddrop 與 拐彎抹角地使用 input swipe 來將一 icon 長壓後,由 (920, 1125) 移到 (280, 160) 後放開的差別:
adb shell input draganddrop 920 1125 280 160
adb shell input swipe 920 1125 280 160 20000
因為咱們必須先將 icon 長壓後,才能令此 icon 進到可被移動的模式,又因 input swipe 無法指定壓下螢幕後多久才可送出 MotionEvent.ACTION_MOVE,所以只好設定較長的 duration 參數使其達成類似效果,但可想而知,這是多麼不適合的作法。

input draganddrop 會在送出 MotionEvent.ACTION_DOWN 後睡眠 ViewConfiguration.getLongPressTimeout() 所指定的時間,接著才送出一連串 MotionEvent.ACTION_MOVE,所以可以達成一般拖放的需求。當然若是看官需要指定壓下螢幕後多久才能送出 MotionEvent.ACTION_MOVE 的話,input draganddrop 是做不到的。要完成此需求,需要本喵之後會介紹的方法來土法煉鋼,敬請期待喽~喵~~

沒有留言:

張貼留言