画面上の追加の指を検出する方法は? たとえば、1 本の指で画面に触れ、しばらくして最初の指を画面に置いたままにしてから、最初の指をそのままの状態で別の指で画面に触れますか? Touch Listenerで2番目の指のタッチを検出するには?
2 に答える
MotionEvent.ACTION_POINTER_DOWN and MotionEvent.ACTION_POINTER_UP
人差し指から送信します。最初の指のためMotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP
に使用されます。
MotionEventのgetPointerCount ()メソッドを使用すると、デバイス上のポインターの数を確認できます。すべてのイベントとポインターの位置は、メソッドで受け取る MotionEvent のインスタンスに含まれます。onTouch()
複数のポインターからのタッチ イベントを追跡するには、MotionEvent.getActionIndex()
およびMotionEvent.getActionMasked()
メソッドを使用して、ポインターのインデックスと、このポインターに対して発生したタッチ イベントを識別する必要があります。
int action = MotionEventCompat.getActionMasked(event);
// Get the index of the pointer associated with the action.
int index = MotionEventCompat.getActionIndex(event);
int xPos = -1;
int yPos = -1;
Log.d(DEBUG_TAG,"The action is " + actionToString(action));
if (event.getPointerCount() > 1) {
Log.d(DEBUG_TAG,"Multitouch event");
// The coordinates of the current screen contact, relative to
// the responding View or Activity.
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
} else {
// Single touch event
Log.d(DEBUG_TAG,"Single touch event");
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
}
...
// Given an action int, returns a string description
public static String actionToString(int action) {
switch (action) {
case MotionEvent.ACTION_DOWN: return "Down";
case MotionEvent.ACTION_MOVE: return "Move";
case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down";
case MotionEvent.ACTION_UP: return "Up";
case MotionEvent.ACTION_POINTER_UP: return "Pointer Up";
case MotionEvent.ACTION_OUTSIDE: return "Outside";
case MotionEvent.ACTION_CANCEL: return "Cancel";
}
return "";
}
詳細については、Googleのマルチタッチ ジェスチャーの処理をご覧ください。
マルチタッチ イベントを処理する必要がある場合は、PointerCount を確認する必要があります。
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
Log.d(DEBUG_TAG,"Multitouch event");
// The coordinates of the current screen contact, relative to
// the responding View or Activity.
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
} else {
// Single touch event
Log.d(DEBUG_TAG,"Single touch event");
xPos = (int)MotionEventCompat.getX(event, index);
yPos = (int)MotionEventCompat.getY(event, index);
}
...
}
詳細については、 https : //developer.android.com/training/gestures/multi.html をご覧ください。