멀티 터치 처리하기

2010. 3. 18. 19:20안드로이드 개발

다행히 좋은 레퍼런스 사이트를 찾아서 금방 해결했다.
http://blogs.zdnet.com/Burnette/?p=1847

public boolean onTouchEvent(MotionEvent event)
{
  switch(event.getAction())
  {
     case MotionEvent.ACTION_POINTER_2_DOWN :
     {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        oldDistance = FloatMath.sqrt(x * x + y * y);
        newDistance = 0.0f;
      
        if (oldDistance > 10f)
        {
           mode = "zoom";
        }
        else
        {
           mode = "normal";
           oldDistance = 0.0f;
           newDistance = 0.0f;
        }
     }
     break;
  
     case MotionEvent.ACTION_MOVE:
     {
        if (mode.equals("zoom"))
        {
           float x = event.getX(0) - event.getX(1);
           float y = event.getY(0) - event.getY(1);
           newDistance = FloatMath.sqrt(x * x + y * y);
          
           if (newDistance > 10f)
           {
              float scale = newDistance / oldDistance;
           }
           else
           {
              mode = "normal";
       oldDistance = 0.0f;
       newDistance = 0.0f;
    }
        }
     }
     break;
}

사용자가 두 손가락으로 멀티 터치를 하면 MotionEvent.ACTION_POINTER_2_DOWN 이벤트가 넘어오고 여기서 첫번째 거리값을 구해서 가지고 있다가 두 손가락을 벌렸다 좁혔다하면서 zoom in/out을 하면 두번째 거리값까지 구해서 scale을 계산해내면 된다.