5

画面上の追加の指を検出する方法は? たとえば、1 本の指で画面に触れ、しばらくして最初の指を画面に置いたままにしてから、最初の指をそのままの状態で別の指で画面に触れますか? Touch Listenerで2番目の指のタッチを検出するには?

4

2 に答える 2

4

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のマルチタッチ ジェスチャーの処理をご覧ください。

于 2015-08-17T08:25:53.240 に答える
1

マルチタッチ イベントを処理する必要がある場合は、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 をご覧ください。

于 2015-08-17T07:29:38.093 に答える