Android 0nTouch event problem [Swipe DOWN and UP]

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

im having an application which needs onTouch swipe events like in Iphone. they are

  1. Swipe Up.
  2. Swipe Down.
  3. Swipe Left.
  4. Swipe Right.

i implemented onTouch event as following . and i got Swipe Left and Right actions correctly.but which is the correct way to implement the swipe Down and Up actions.

MYCODE:

    float downXValue,downYValue;      @Override     public boolean onTouchEvent(MotionEvent arg1) {           // Get the action that was done on this touch event         switch (arg1.getAction())         {             case MotionEvent.ACTION_DOWN:             {                 // store the X value when the user's finger was pressed down                 downXValue = arg1.getX();                 downYValue = arg1.getY();                 break;             }              case MotionEvent.ACTION_UP:             {                 // Get the X value when the user released his/her finger                 float currentX = arg1.getX();                    float currentY=arg1.getY();                  // going backwards: pushing stuff to the right                 if (downXValue < currentX)                 {                     Log.d(DEBUG_TAG, "Right");                 }                  // going forwards: pushing stuff to the left                 if (downXValue > currentX)                 {                      Log.d(DEBUG_TAG, "Left");                 }                 break;             }         }           //GestureListener is called here...           //return gestures.onTouchEvent(event);           return true;     } 

Thank you.

回答1:

What about...

if (downYValue < currentY) {     Log.d(DEBUG_TAG, "Down"); } 

Are you sure you wrote the code above?

Edit

OK, I believe you. What you have to do is basically:

double sizeInX = Math.abs(downXValue - currentX); double sizeInY = Math.abs(downYValue - currentY); if( sizeInX > sizeInY ){    // you better swipe horizontally } else {    // you better swipe vertically } 


回答2:

This is a listener I wrote some time ago, that handles both up an down swypes. Theres a float oldPosition variable that I used to track the touch position. There is some unneeded code in there but you'll get the idea:

    LinearLayout tableM = (LinearLayout) findViewById (R.id.tableM);     tableM.setOnTouchListener(new OnTouchListener () {         public boolean onTouch(View view, MotionEvent event) {             if (event.getAction() == android.view.MotionEvent.ACTION_DOWN) {                 oldPosition = event.getY();                 return true;             } else             if (event.getAction() == android.view.MotionEvent.ACTION_MOVE) {                 if (event.getY() < oldPosition) {                     tableIndexM++;                     if (tableIndexM > (minsecs.length - 3)) {                         tableIndexM = 0;                     }                     fillTable (rowM0, rowM1, rowM2, minsecs, tableIndexM);                 }                 else if (event.getY() > oldPosition) {                     tableIndexM--;                     if (tableIndexM < 0) {                         tableIndexM = minsecs.length - 3;                     }                     fillTable (rowM0, rowM1, rowM2, minsecs, tableIndexM);                 }                 oldPosition = event.getY();                 return true;             }             return false;         }     });         


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!