好的,多亏了这里的 Gabe 和许多关于此的博客,我找到了解决问题的方法!
首先,我在“Activity”类中初始化了我的变量
int GLOBAL_TOUCH_POSITION_X = 0;
int GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
接下来,在 onCreate() 内部:
//Two-Finger Drag Gesture detection
RelativeLayout TextLoggerLayout = (RelativeLayout)findViewById(R.id.ActivityrView);
TextLoggerLayout.setOnTouchListener(
new RelativeLayout.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent m) {
handleTouch(m);
return true;
}
});
现在定义函数handleTouch(m)如下,它输出“双指触摸”的当前位置以及触摸的初始位置:
void handleTouch(MotionEvent m){
//Number of touches
int pointerCount = m.getPointerCount();
if(pointerCount == 2){
int action = m.getActionMasked();
int actionIndex = m.getActionIndex();
String actionString;
TextView tv = (TextView) findViewById(R.id.testDiffText);
switch (action)
{
case MotionEvent.ACTION_DOWN:
GLOBAL_TOUCH_POSITION_X = (int) m.getX(1);
actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
tv.setText(actionString);
break;
case MotionEvent.ACTION_UP:
GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
actionString = "UP"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
tv.setText(actionString);
break;
case MotionEvent.ACTION_MOVE:
GLOBAL_TOUCH_CURRENT_POSITION_X = (int) m.getX(1);
int diff = GLOBAL_TOUCH_POSITION_X-GLOBAL_TOUCH_CURRENT_POSITION_X;
actionString = "Diff "+diff+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
tv.setText(actionString);
break;
case MotionEvent.ACTION_POINTER_DOWN:
GLOBAL_TOUCH_POSITION_X = (int) m.getX(1);
actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X;
tv.setText(actionString);
break;
default:
actionString = "";
}
pointerCount = 0;
}
else {
GLOBAL_TOUCH_POSITION_X = 0;
GLOBAL_TOUCH_CURRENT_POSITION_X = 0;
}
}
你有它!“双指拖动”手势终于实现了。看起来,我也可以在上面写一篇小博文!!:)