android代码实现模拟⽤户点击、滑动等操作/**
* 模拟⽤户点击
*
* @param view 要触发操作的view
* @param x 相对于要操作view的左上⾓x轴偏移量
* @param y 相对于要操作view的左上⾓y轴偏移量
*/
private static void analogUserClick(View view, float x, float y) {
if (view == null) {
return;
}
LogUtil.e(TAG_POINT, "正在模拟点击操作:p->" + x + "," + y);
long downTime = SystemClock.uptimeMillis();//模拟按下去的时间
long eventTime = downTime;//事件发⽣时间
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime,
ACTION_DOWN, x, y, 0);
eventTime = eventTime + 90;//离开屏幕时间
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, x, y, 0);
//回收事件
}
/**
* 模拟⽤户滑动操作
*
* @param view 要触发操作的view
* @param type 模拟操作类型:均匀滑动、快速滑动
* @param p1x 滑动的起始点x坐标
* @param p1y 滑动的起始点y坐标
* @param p2x 滑动的终点x坐标
* @param p2y 滑动的终点y坐标
*/
private static void analogUserScroll(View view, final int type, final float p1x, final float p1y, final float p2x, final float p2y) {
LogUtil.e(TAG_POINT, "正在模拟滑屏操作:p1->" + p1x + "," + p1y + ";p2->" + p2x + "," + p2y);
if (view == null) {
return;
}
long downTime = SystemClock.uptimeMillis();//模拟按下去的时间
long eventTime = downTime;
float pX = p1x;
float pY = p1y;
int speed = 0;//快速滑动
float touchTime = 116;//模拟滑动时发⽣的触摸事件次数
//平均每次事件要移动的距离
float perX = (p2x - p1x) / touchTime;float up
float perY = (p2y - p1y) / touchTime;
boolean isReversal = perX < 0 || perY < 0;//判断是否反向:⼿指从下往上滑动,或者⼿指从右往左滑动
boolean isHandY = Math.abs(perY) > Math.abs(perX);//判断是左右滑动还是上下滑动
if (type == USER_TOUCH_TYPE_1) {//加速滑动
touchTime = 10;//如果是快速滑动,则发⽣的触摸事件⽐均匀滑动更少
speed = isReversal ? -20 : 20;//反向移动则坐标每次递减
}
/
/模拟⽤户按下
MotionEvent downEvent = MotionEvent.obtain(downTime, eventTime,
ACTION_DOWN, pX, pY, 0);
//模拟移动过程中的事件
List<MotionEvent> moveEvents = new ArrayList<>();
boolean isSkip = false;
for (int i = 0; i < touchTime; i++) {
pX += (perX + speed);
pY += (perY + speed);
if ((isReversal && pX < p2x) || (!isReversal && pX > p2x)) {
pX = p2x;
isSkip = !isHandY;
}
if ((isReversal && pY < p2y) || (!isReversal && pY > p2y)) {
pY = p2y;
isSkip = isHandY;
}
eventTime += 20.0f;//事件发⽣的时间要不断递增
MotionEvent moveEvent = getMoveEvent(downTime, eventTime, pX, pY);
moveEvents.add(moveEvent);
if (type == USER_TOUCH_TYPE_1) {//加速滑动
speed += (isReversal ? -70 : 70);
}
if (isSkip) {
break;
}
}
//模拟⼿指离开屏幕
MotionEvent upEvent = MotionEvent.obtain(downTime, eventTime,
MotionEvent.ACTION_UP, pX, pY, 0);
/
/回收触摸事件
for (int i = 0; i < moveEvents.size(); i++) {
<(i).recycle();
}
}
private static MotionEvent getMoveEvent(long downTime, long evntTime, float x, float y) { return MotionEvent.obtain(downTime, evntTime,
MotionEvent.ACTION_MOVE, x, y, 0);
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论