java – How to smoothly move a Bitmap to a specific side when pressed?

Question:

How to make it so that when you press and hold on the left side of the Bitmap screen (in my case, the rocket) smoothly moves to the left, and when you press the right side to the right?

public boolean onTouchEvent(MotionEvent event)
{
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:



        case MotionEvent.ACTION_MOVE:
            if(event.getY()>(Game.heightDisplay/2))  {
                if(coordY > 930) {
                    coordY = 930;
                }
                else {
                    coordY= coordY + 5;
                }
            }
            else if(event.getY()<(Game.heightDisplay/2)) {

                if(coordY < 0) {
                    coordY = 0;
                }
                else {
                    coordY= coordY - 5;
                }
            }
            break;


        case MotionEvent.ACTION_UP:

            break;
    }


    return super.onTouchEvent(event);
}

Answer:

  1. Hang the listener on the screen.
  2. In it, get the coordinates of the click.
  3. Compare them to your screen sizes.
  4. Now you have information about whether the left or right side of the screen was clicked.
  5. Depending on this, you change the coordinate of the container containing the picture.
Scroll to Top