2013-05-22 2 views
3

Android에서 원터치 만 허용하는 방법은 무엇입니까? 멀티 터치를 지원하고 싶지 않고 내 손가락 중 하나가 이미 화면에 닿은 경우 다른 앱을 삭제하도록합니다. iOS에서 독점 터치를 사용하는 것과 같습니다.Android : 원터치 만 허용하는 방법?

또한 허용되는 터치 수를 설정하는 방법이 있습니까?

감사합니다.

편집 : 최소 목표 API = 당신은 첫 번째 터치의 포인터 ID를 추적하고 터치에 corressponding에만 터치 이벤트를 사용해야합니다 8. ​​

답변

2

. 여기 The official Android blog "Making Sense of Multitouch "

private static final int INVALID_POINTER_ID = -1; 

// The ‘active pointer’ is the one currently moving our object. 
private int mActivePointerId = INVALID_POINTER_ID; 

// Existing code ... 

@Override 
public boolean onTouchEvent(MotionEvent ev) { 
    final int action = ev.getAction(); 
    switch (action & MotionEvent.ACTION_MASK) { 
    case MotionEvent.ACTION_DOWN: { 
     final float x = ev.getX(); 
     final float y = ev.getY(); 

     mLastTouchX = x; 
     mLastTouchY = y; 

     // Save the ID of this pointer 
     mActivePointerId = ev.getPointerId(0); 
     break; 
    } 

    case MotionEvent.ACTION_MOVE: { 
     // Find the index of the active pointer and fetch its position 
     final int pointerIndex = ev.findPointerIndex(mActivePointerId); 
     final float x = ev.getX(pointerIndex); 
     final float y = ev.getY(pointerIndex); 

     final float dx = x - mLastTouchX; 
     final float dy = y - mLastTouchY; 

     mPosX += dx; 
     mPosY += dy; 

     mLastTouchX = x; 
     mLastTouchY = y; 

     invalidate(); 
     break; 
    } 

    case MotionEvent.ACTION_UP: { 
     mActivePointerId = INVALID_POINTER_ID; 
     break; 
    } 

    case MotionEvent.ACTION_CANCEL: { 
     mActivePointerId = INVALID_POINTER_ID; 
     break; 
    } 

    case MotionEvent.ACTION_POINTER_UP: { 
     // Extract the index of the pointer that left the touch sensor 
     final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) 
       >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
     final int pointerId = ev.getPointerId(pointerIndex); 
     if (pointerId == mActivePointerId) { 
      // This was our active pointer going up. Choose a new 
      // active pointer and adjust accordingly. 
      final int newPointerIndex = pointerIndex == 0 ? 1 : 0; 
      mLastTouchX = ev.getX(newPointerIndex); 
      mLastTouchY = ev.getY(newPointerIndex); 
      mActivePointerId = ev.getPointerId(newPointerIndex); 
     } 
     break; 
    } 
    } 

    return true; 
} 
+0

내가, 내 활동에 추가이 시도를 사용하여 멀티 터치를 해제 할 수 있지만 그것은에서 터치를 인식하지 않습니다 활동 내부 조각. 어떻게해야합니까? 감사! – dzep

+0

나는 ontouchlistener로 설정 한 객체의 id를 사용하여이 작업을 시도했지만 문제가 발생했습니다. 이것은 완벽했습니다. – b15

4

의 좋은 예 예이 android:splitMotionEvents="false" 또는 android:windowEnableSplitTouch="false"

+0

이것이 작동하는지 확실하지 않습니다. 하지만 API 레벨 11 이상이 필요합니다. – asloob

+0

당신이 원하는 버전이 맞습니다. –

+0

최소 타겟팅 버전은 API 10입니다. – dzep