在Android设备中,我们可以使用手指来进行屏幕触控操作,也有触控笔等设备,也有自动点击器这样的设备,这里我们来看看他们在MotionEvent中的type是什么,如果type一致,不同的设备touch事件可以共存。
在代码中添加log,打印MotionEvent即可观察
java
public class MainActivity extends AppCompatActivity {
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
Log.i("test114", "motionevent=" + motionEvent);
return false;
}
- 手指touch,toolType[0]=TOOL_TYPE_FINGER
java
08-10 19:40:44.048 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=1571.3867, y[0]=400.75, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=121686176, downTime=121686176, deviceId=8, source=0x1002, displayId=0, eventId=99301767 }
08-10 19:40:44.053 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1571.3867, y[0]=400.75, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=121686181, downTime=121686176, deviceId=8, source=0x1002, displayId=0, eventId=309619483 }
08-10 19:40:44.054 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=1571.3867, y[0]=400.75, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=121686194, downTime=121686176, deviceId=8, source=0x1002, displayId=0, eventId=566998607 }
- 普通的导电触屏笔,toolType[0]=TOOL_TYPE_FINGER
java
08-10 19:42:54.423 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=1623.3662, y[0]=411.74316, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=121816559, downTime=121816559, deviceId=8, source=0x1002, displayId=0, eventId=323824768 }
08-10 19:42:54.428 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1623.3662, y[0]=411.74316, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=121816565, downTime=121816559, deviceId=8, source=0x1002, displayId=0, eventId=365482332 }
- 特殊的触控笔,toolType[0]=TOOL_TYPE_STYLUS
java
08-10 19:44:07.289 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=1468.749, y[0]=391.1748, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=121889428, downTime=121889428, deviceId=7, source=0x5002, displayId=0, eventId=902540448 }
08-10 19:44:07.293 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1468.749, y[0]=391.1748, toolType[0]=TOOL_TYPE_STYLUS, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=121889430, downTime=121889428, deviceId=7, source=0x5002, displayId=0, eventId=417635430 }
- 点击器设备,这种设备可以搜到很多
java
08-10 19:50:03.541 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=122245665, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=529445397 }
08-10 19:50:03.559 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=4, eventTime=122245696, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=285560474 }
08-10 19:50:03.567 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245705, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=695468747 }
08-10 19:50:03.575 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245713, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=1021987380 }
08-10 19:50:03.584 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245721, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=24098659 }
08-10 19:50:03.592 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245730, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=947573210 }
08-10 19:50:03.601 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245738, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=1023697170 }
08-10 19:50:03.609 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245747, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=914557326 }
08-10 19:50:03.617 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245755, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=1024595037 }
08-10 19:50:03.626 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245763, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=610083777 }
08-10 19:50:03.634 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_MOVE, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=1, eventTime=122245771, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=417593152 }
08-10 19:50:03.660 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_UP, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=122245801, downTime=122245665, deviceId=8, source=0x1002, displayId=0, eventId=922423896 }
08-10 19:50:03.866 14115 14115 I test114 : motionevent=MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=1996.2207, y[0]=1148.2822, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=122246004, downTime=122246004, deviceId=8, source=0x1002, displayId=0, eventId=347762606 }
可以看到,除了down,up事件,还有很多的move事件
有时候可以注意到,通电的充电线也可以在屏幕上出现touch效果,这个touch和手指touch不会发生冲突,其type应该也是TOOL_TYPE_FINGER