0

I have an 8 directional joystick (consisting of a knob and pad), after setting an onTouchListener to the pad, in MotionEvent.ACTION_MOVE, I'm calculating position_x, position_y, distance, and angle, to get a direction[1-8]. Given a direction[1-8], I'd like to continuously execute a specific movement(); but MotionEvent.ACTION_MOVE only executes while finger is moving..

How can I execute a movement of the lat lng continuously?

--Lat/Lng Movement (d=direction)--

private void movement(int d, double lat, double lng) {
        if (d==1) { //up
            lat = lat + 0.0000002;}
        else if (d==2){ //upright
            lat = lat + 0.0000001;
            lng = lng + 0.0000001;
        }
        else if (d==3) { //right
            lng = lng + 0.0000002;
        }
        else if (d==4) { //downright
            lng = lng + 0.0000001;
            lat = lat - 0.0000001;
        }
        else if (d==5) { //down
            lat = lat - 0.0000002;
        }
        else if (d==6) { //downleft
            lat = lat - 0.0000001;
            lng = lng - 0.0000001;
        } 
        else if (d==7) { //left
            lng = lng - 0.0000002;
        } 
        else if (d==8) { //upleft
            lat = lat + 0.0000001;
            lng = lng - 0.0000001;
        } 
}

--action_move, getangle, direction--

case MotionEvent.ACTION_MOVE: {

                    position_x = (int) (pad.getX() + pad.getWidth() / 2 - knob.getWidth() / 2 * -1 - knob.getX() - pad.getPivotX());
                    position_y = (int) (pad.getY() + pad.getHeight() / 2 - knob.getHeight() / 2 * -1 - knob.getY() - pad.getPivotY());
                    distance = (float) Math.sqrt(Math.pow(position_x, 2) + Math.pow(position_y, 2));
                    angle = (float) getangle(position_x, position_y);
                    knob.setX(event.getX() + pad.getX() - knob.getWidth() / 2);
                    knob.setY(event.getY() + pad.getY() - knob.getHeight() / 2);
                    direction();
                    movement(direction(), lat, lng);


 private double getangle(float x, float y) {
    if (x >= 0 && y >= 0) return Math.toDegrees(Math.atan(y / x));
    else if (x < 0 && y >= 0) return Math.toDegrees(Math.atan(y / x)) + 180;
    else if (x < 0 && y < 0) return Math.toDegrees(Math.atan(y / x)) + 180;
    else if (x >= 0 && y < 0) return Math.toDegrees(Math.atan(y / x)) + 360;
    return 0;
}

private int direction() {
    if (distance > 50) {
        if (angle >= 67.5 && angle < 112.5) return 1;
        else if (angle >= 112.5 && angle < 157.5) return 2;
        else if (angle >= 157.5 && angle < 202.5) return 3;
        else if (angle >= 202.5 && angle < 247.5) return 4;
        else if (angle >= 247.5 && angle < 292.5) return 5;
        else if (angle >= 292.5 && angle < 337.5) return 6;
        else if (angle >= 337.5 || angle < 22.5) return 7;
        else if (angle >= 22.5 && angle < 67.5) return 8;
    } else if (distance <= 50) { //knob at rest in middle
            return 0;
    }
    return 0;
}
FlingLandHo
  • 13
  • 1
  • 6

1 Answers1

0

You can check the following question's top answer and apply the same logic of using threads and have a while loop in your ui thread.

Continuous "Action_DOWN" in Android

Berke Atac
  • 156
  • 5