我使用SPenEventLibrary来识别触摸是通过手指还是Spen完成的。在我升级到Android l之前,它一直在工作。
设备:Galaxy Note 3。
spenlib是最新的:libspen23.jar, pen-v3.1.8.jar
Android版本:5.0
代码很简单:
mSPenEventLibrary.setSPenTouchListener(this, new SPenTouchListener(){
@Override
public boolean onTouchFinger(View view, MotionEvent event) {
if (ApplicationPreferences.get(mContext).getSPENWristGaurdOnOff() == false) {
return handleEvent(event);
} else {
return true;
}
}
@Override
public boolean onTouchPen(View view, MotionEvent event) {
return handleEvent(event);
}
@Override
public void onTouchButtonDown(View arg0, MotionEvent arg1) {
}
@Override
public void onTouchButtonUp(View arg0, MotionEvent arg1) {
}
@Override
public boolean onTouchPenEraser(View arg0, MotionEvent arg1) {
}
});
当我用Spen触摸屏幕时,调用onTouchFinger()而不是onTouchPen()。在旧的Android版本中onTouchPen()被调用
这是来自三星开发者论坛的回答:
公共布尔onTouch(视图视图,MotionEvent事件){
boolean done = false;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (event.getMetaState() == 0) {
//Finger
// Do whatever you need to do here
done = true;
}
else if(event.getMetaState() == 512) {
//Pen
// Do whatever you need to do here
done = true;
}
}
else {
if (event.getToolType(0) == MotionEvent.TOOL_TYPE_FINGER) {
//Finger
// Do whatever you need to do here
done = true;
}
else if(event.getToolType(0) == MotionEvent.TOOL_TYPE_STYLUS) {
//Pen
// Do whatever you need to do here and then
done = true;
}
}
if (done)
return true;
return super.onTouch(view, event);
}