2014-10-06 2 views
-3

Я хочу реализовать два пальца салфетки жест в моем приложении Android. Я использовал следующий код:Жест «Два пальца»?

void handleTouch(MotionEvent m){ 
//Number of touches 
int pointerCount = m.getPointerCount(); 
if(pointerCount == 2){ 
    int action = m.getActionMasked(); 
    int actionIndex = m.getActionIndex(); 
    String actionString; 
    TextView tv = (TextView) findViewById(R.id.testDiffText); 
    switch (action) 
    { 
     case MotionEvent.ACTION_DOWN:     
      GLOBAL_TOUCH_POSITION_X = (int) m.getX(1); 
      actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X; 
      tv.setText(actionString); 
      break; 
     case MotionEvent.ACTION_UP:     
      GLOBAL_TOUCH_CURRENT_POSITION_X = 0; 
      actionString = "UP"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X; 
      tv.setText(actionString); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      GLOBAL_TOUCH_CURRENT_POSITION_X = (int) m.getX(1); 
      int diff = GLOBAL_TOUCH_POSITION_X-GLOBAL_TOUCH_CURRENT_POSITION_X; 
      actionString = "Diff "+diff+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X; 
      tv.setText(actionString);     
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      GLOBAL_TOUCH_POSITION_X = (int) m.getX(1); 
      actionString = "DOWN"+" current "+GLOBAL_TOUCH_CURRENT_POSITION_X+" prev "+GLOBAL_TOUCH_POSITION_X; 
      tv.setText(actionString); 
      break; 
     default: 
      actionString = ""; 
    } 

    pointerCount = 0; 
} 
else { 
    GLOBAL_TOUCH_POSITION_X = 0; 
    GLOBAL_TOUCH_CURRENT_POSITION_X = 0; 
} 

}

я получаю то же действие, выполняемое в вверх или вниз проведите пальцем. он выполняет только задачу Action_up. кто-нибудь может помочь мне реализовать как до дна салфетки, так и снизу до салфетки.

ответ

4
private static final int NONE = 0; 
private static final int SWIPE = 1; 
private int mode = NONE; 
private float startY; 
private float stopY; 
// We will only detect a swipe if the difference is at least 100 pixels 
// Change this value to your needs 
private static final int TRESHOLD = 100; 

public boolean onTouch(View v, MotionEvent event) 
{ 
    switch (event.getAction() & MotionEvent.ACTION_MASK) 
    { 
     case MotionEvent.ACTION_POINTER_DOWN: 
      // This happens when you touch the screen with two fingers 
      mode = SWIPE; 
      // You can also use event.getY(1) or the average of the two 
      startY = event.getY(0); 
      break; 

     case MotionEvent.ACTION_POINTER_UP: 
      // This happens when you release the second finger 
      mode = NONE; 
      if(Math.abs(startY - stopY) > TRESHOLD) 
      { 
       if(startY > stopY) 
       { 
        // Swipe up 
       } 
       else 
       { 
        //Swipe down 
       } 
      } 
      this.mode = NONE; 
      break; 

     case MotionEvent.ACTION_MOVE: 
      if(mode == SWIPE) 
      { 
       stopY = event.getY(0); 
      } 
      break; 
    } 

    return true; 
} 

Надеюсь, это помогло.

+0

Работает ли он на двух пальцах? – Kaifi

+0

MotionEvent.ACTION_DOWN означает один палец. MotionEvent.ACTION_POINTER_DOWN - два пальца. –

+0

Большое спасибо @Inneke De Clippel. :) – Kaifi

Смежные вопросы