我嘗試修復在我的應用程序中添加多點觸控功能時出現在代碼中的問題。 這個問題似乎來自ACTION_POINTER_DOWN:多點觸控 - 指針超出範圍
private float oldDist = 0;
backCard.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent me) {
switch(me.getAction()){
case MotionEvent.ACTION_DOWN:
firstX = (int) me.getX();
case MotionEvent.ACTION_POINTER_DOWN:
if(me.getPointerCount() >= 2){
oldDist = getSpacing(me);
System.out.println(oldDist);
}
break;
case MotionEvent.ACTION_MOVE:
float newDist = getSpacing(me);
if(newDist - oldDist > 200 && oldDist != 0){
System.out.println("Enabled");
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
break;
}
return true;
}
private float getSpacing(MotionEvent me){
float difx = me.getX(0) - me.getX(1);
float dify = me.getY(0) - me.getY(1);
float spacing = (float) Math.sqrt(difx*difx + dify*dify);
return spacing;
}
});
當我使用它沒有在ACTION_POINTER_DOWN的getPointerCount()條件,我有一個超出範圍的錯誤。但是,如果我使用條件,日誌不顯示任何我打印在代碼中。 (當然,我使用2個手指!:)),因此即使多個手指同時觸摸屏幕,情況也不會如此。
我該如何解決這個問題?謝謝。
我的設備是GS3。
我不認爲ACTION_DOWN對所有手指交付一次。你幾乎不可能在相同的時刻,在幾毫秒內完全掌握你的手指。我建議你關注ACTION_MOVE。 – 323go