detecting movements using onTouchEvent

1

I found this method on the internet to detect motion, however I would like it when I move to the right executes a command ( example change the color of the button ), but I am having trouble implementing this code if someone knows Give me strength, thank you in advance.

private String TAG = GestureActivity.class.getSimpleName();
float initialX, initialY;

@Override
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);
}
    
asked by anonymous 13.05.2015 / 18:05

0 answers