2013-10-20 3 views
0
@Override 
    public boolean onTouch(View v, MotionEvent event) { 
     // TODO Auto-generated method stub 

     float r = 70; 
     float centerLx = (float) (screenWidth*.3425); 
     float centerLy = (float) (screenHeight*.4958); 
     float centerRx = (float) (screenWidth*.6538); 
     float centerRy = (float) (screenHeight*.4917); 
     float dx = 0; 
     float dy = 0; 
     float theta; 
     float c; 

     int action = event.getAction(); 
     int actionCode = action & MotionEvent.ACTION_MASK; 
     int pid = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT; 
     int fingerid = event.getPointerId(pid); 
     int x = (int) event.getX(pid); 
     int y = (int) event.getY(pid); 




      c = FloatMath.sqrt(dx*dx + dy*dy); 
      theta = (float) Math.atan(Math.abs(dy/dx)); 


      switch (actionCode) { 
      case MotionEvent.ACTION_DOWN: 
      case MotionEvent.ACTION_POINTER_DOWN: 

       //if touching down on left stick, set leftstick ID to this fingerid.                
       if(x < screenWidth/2 && c<r*.8) { 
        lsId = fingerid; 
            dx = x-centerLx; 
         dy = y-centerLy; 
        touchingLs = true; 
       } 
       else if(x > screenWidth/2 && c<r*.8) { 
        rsId = fingerid; 
            dx = x-centerRx; 
            dy = y-centerRy; 
        touchingRs = true; 
       } 



        break; 
      case MotionEvent.ACTION_MOVE: 


      if (touchingLs && fingerid == lsId) { 
       dx = x - centerLx; 
       dy = y - centerLy; 
      }else if (touchingRs && fingerid == rsId) { 
       dx = x - centerRx; 
       dy = y - centerRy; 
      } 


       c = FloatMath.sqrt(dx*dx + dy*dy); 
       theta = (float) Math.atan(Math.abs(dy/dx)); 


       //if touching outside left radius and moving left stick 
       if(c >= r && touchingLs && fingerid == lsId) { 
        if(dx>0 && dy<0) { //top right quadrant 
         lsX = r * FloatMath.cos(theta); 
         lsY = -(r * FloatMath.sin(theta)); 
         Log.i("message", "top right"); 
        } 

        if(dx<0 && dy<0) { //top left quadrant 
         lsX = -(r * FloatMath.cos(theta)); 
         lsY = -(r * FloatMath.sin(theta)); 
         Log.i("message", "top left"); 
        } 

        if(dx<0 && dy>0) { //bottom left quadrant 
         lsX = -(r * FloatMath.cos(theta)); 
         lsY = r * FloatMath.sin(theta); 
         Log.i("message", "bottom left"); 
        } 


        else if(dx > 0 && dy > 0){ //bottom right quadrant 
         lsX = r * FloatMath.cos(theta); 
         lsY = r * FloatMath.sin(theta); 
         Log.i("message", "bottom right"); 
        } 


       } 
       if(c >= r && touchingRs && fingerid == rsId) { 
        if(dx>0 && dy<0) { //top right quadrant 
         rsX = r * FloatMath.cos(theta); 
         rsY = -(r * FloatMath.sin(theta)); 
         Log.i("message", "top right"); 
        } 
        if(dx<0 && dy<0) { //top left quadrant 
         rsX = -(r * FloatMath.cos(theta)); 
         rsY = -(r * FloatMath.sin(theta)); 
         Log.i("message", "top left"); 
        } 
        if(dx<0 && dy>0) { //bottom left quadrant 
         rsX = -(r * FloatMath.cos(theta)); 
         rsY = r * FloatMath.sin(theta); 
         Log.i("message", "bottom left"); 
        } 
        else if(dx > 0 && dy > 0) { 
         rsX = r * FloatMath.cos(theta); 
         rsY = r * FloatMath.sin(theta); 
         Log.i("message", "bottom right"); 
        } 
       } 
       else { 
        if(c < r && touchingLs && fingerid == lsId) { 
         lsX = dx; 
         lsY = dy; 
        } 
        if(c < r && touchingRs && fingerid == rsId){ 
         rsX = dx; 
         rsY = dy; 
        } 

       } 

       break; 
      case MotionEvent.ACTION_UP: 
      case MotionEvent.ACTION_POINTER_UP: 

      if (fingerid == lsId) { 
       lsId = -1; 
       lsX = 0; 
       lsY = 0; 
       touchingLs = false; 
      } else if (fingerid == rsId) { 
       rsId = -1; 
       rsX = 0; 
       rsY = 0; 
       touchingRs = false; 
      } 


       break; 
      } 



     return true; 
    } 

에서 왼쪽 조이스틱 오른쪽 조이스틱있다 할 수 없습니다. 지금은 한 번에 하나만 움직입니다. 누군가가 올바른 길로 나를 설 수 있다면, 나는이 문제에 대해 악몽을 꾸며왔다.안드로이드는 독립적으로 이동하는 두 개의 가상 조이스틱을 얻을 수 있지만, 동시에

답변

1

내가 보는 가장 큰 문제는 당신이 ACTION_MOVE를받을 때 당신은 단지 하나의 포인터를 처리하고 있다는 것입니다. 안드로이드는 종종 하나의 MotionEvent에 여러 이벤트를 배치 할 것이므로 두 포인터를 모두 업데이트하지 않으면 한 번에 두 개의 포인터를 움직일 수 없습니다.

는 현재 fingerId를 사용하는 방법에 대해 잊어 버려요. ACTION_MOVE을 얻은 경우 두 포인터에 대해 x/y을 가져 와서 사용하십시오. 뭔가 같은 :

case ACTION_MOVE: 
    int x; 
    int y; 

    int leftIndex = event.findPointerIndex(lsId); 
    x = (int)event.getX(leftIndex); 
    y = (int)event.getY(leftIndex); 
    /// ... do stuff with left coordinates 
    ... 
    int rightIndex = event.findPointerIndex(rsId); 
    x = (int)event.getX(rightIndex); 
    y = (int)event.getY(rightIndex); 
    /// ... do stuff with right coordinates 
    ... 
    break;