-2

I'm currently processing movement on Android and I used MotionEvent.ACTION_MOVE.

I calculate currentx =(int) event.getX()/size and currenty=event.gety()/size, which gives me the integer base on the size of grid which I work on. Howevor, I want a way to find where the current y is changed while moving and decide to do something after that.

For example, if the size is equal to 10 then in x=150 and y=150 my currentx would be 15 and and currenty would be 15 but when currenty changed to something like 16 that means 150<event.get(y)<160. I want to know I'm in down direction etcetera.

Jawa
  • 2,336
  • 6
  • 34
  • 39

1 Answers1

0
public boolean onTouchEvent(MotionEvent event) {
//mGestureDetector.onTouchEvent(event);

int action = event.getActionMasked();

switch (action) {

    case MotionEvent.ACTION_DOWN:
        initialX = event.getX();
        initialY = event.getY();

        Log.d(TAG, "Action was DOWN");
        break;

    case MotionEvent.ACTION_MOVE:
        Log.d(TAG, "Action was MOVE");
        break;

    case MotionEvent.ACTION_UP:
        float finalX = event.getX();
        float finalY = event.getY();

        Log.d(TAG, "Action was UP");

        if (initialX < finalX) {
            Log.d(TAG, "Left to Right swipe performed");
        }

        if (initialX > finalX) {
            Log.d(TAG, "Right to Left swipe performed");
        }

        if (initialY < finalY) {
            Log.d(TAG, "Up to Down swipe performed");
        }

        if (initialY > finalY) {
            Log.d(TAG, "Down to Up swipe performed");
        }

        break;

    case MotionEvent.ACTION_CANCEL:
        Log.d(TAG,"Action was CANCEL");
        break;

    case MotionEvent.ACTION_OUTSIDE:
        Log.d(TAG, "Movement occurred outside bounds of current screen element");
        break;
}

return super.onTouchEvent(event);
  }

here is my code

private int getDirection(float startx, float starty,float endx ,float endy )
{

int direction=-1;

    startx = (((int) startx / _size) *_size) ;
    starty = (((int) starty / _size) * _size) ;
    endx = (((int) endx / _size) * _size) ;
    endy = (((int) endy / _size) * _size) ;


  //  Log.i("direction","startx:"+startx+"endx:"+endx+"starty: "+starty+"endy: "+endy+" ");


    if (starty==endy)
    {
        direction= 0;

        return direction;

    }
    if(startx==endx)
    {
        direction =1;
       return direction;
    }

    if(Math.abs(startx-endx)==Math.abs(starty-endy))
    {

        direction =2;
       return direction;



    }



    else 
    {
        direction =-1;
    return direction;
    }





}

so i can find i'm in horizantal or vertical or non of these 2 cordinates but i need a way to compare the previous and current orientation while in action.move some thing like onDirectionChanggeListener i can use action_up but i need it during move action.

Stefan
  • 5,203
  • 8
  • 27
  • 51
P-RAD
  • 1,293
  • 2
  • 15
  • 36