我是靠谱客的博主 瘦瘦宝马,这篇文章主要介绍android代码实现短按 长按,现在分享给大家,希望可以做个参考。

复制代码
1
2
3
4
5
6
7
void sendTap(int displayId, float x, float y) { final long now = SystemClock.uptimeMillis(); injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_DOWN, now, now, x, y, 1.0f, displayId); injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_UP, now, now, x, y, 0.0f, displayId); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void sendLongTouch(int displayId, float x, float y) { int duration = 1000; final long down = SystemClock.uptimeMillis(); injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_DOWN, down, down, x, y, 1.0f, displayId); long now = SystemClock.uptimeMillis(); final long endTime = down + duration; while (now < endTime) { final long elapsedTime = now - down; final float alpha = (float) elapsedTime / duration; injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_MOVE, down, now, lerp(x, x, alpha), lerp(y, y, alpha), 1.0f, displayId); now = SystemClock.uptimeMillis(); } injectMotionEvent(InputDevice.SOURCE_TOUCHSCREEN, MotionEvent.ACTION_UP, down, now, x, y, 0.0f, displayId); }
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
private static final float lerp(float a, float b, float alpha) { return (b - a) * alpha + a; } private static void injectMotionEvent(int inputSource, int action, long downTime, long when, float x, float y, float pressure, int displayId) { final float DEFAULT_SIZE = 1.0f; final int DEFAULT_META_STATE = 0; final float DEFAULT_PRECISION_X = 1.0f; final float DEFAULT_PRECISION_Y = 1.0f; final int DEFAULT_EDGE_FLAGS = 0; MotionEvent event = MotionEvent.obtain(downTime, when, action, x, y, pressure, DEFAULT_SIZE, DEFAULT_META_STATE, DEFAULT_PRECISION_X, DEFAULT_PRECISION_Y, getInputDeviceId(inputSource), DEFAULT_EDGE_FLAGS); event.setSource(inputSource); event.setDisplayId(displayId); InputManager.getInstance().injectInputEvent(event, InputManager.INJECT_INPUT_EVENT_MODE_WAIT_FOR_FINISH); } private static int getInputDeviceId(int inputSource) { final int DEFAULT_DEVICE_ID = 0; int[] devIds = InputDevice.getDeviceIds(); for (int devId : devIds) { InputDevice inputDev = InputDevice.getDevice(devId); if (inputDev.supportsSource(inputSource)) { return devId; } } return DEFAULT_DEVICE_ID; }

最后

以上就是瘦瘦宝马最近收集整理的关于android代码实现短按 长按的全部内容,更多相关android代码实现短按内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部