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 moved to the left, and when you press on the right 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); } 

    1 answer 1

    1. Hang the listener tach on the screen.
    2. In it you will receive the coordinates of clicking.
    3. Compare them with the size of the screen.
    4. Now you have information about whether the left or right side of the screen has been clicked.
    5. Depending on this, change the coordinate of the container containing the image.
    • Thanks for the answer, it worked out, but how can you make the movement smooth? - En1q0d
    • @ En1q0d, smooth = slowness. Just per unit of time move the image to a smaller number of pixels. - JuriySPb
    • thanks) And here's another question, how can you make the rocket move left or right while I press a button. Just now I just press, and it changes coordinates by 10, and that’s all, you have to press again to move it? - En1q0d
    • @ En1q0d, like, if you check the type of the event, Action_DOWN , MOVE and UP should be called. If so, then you need to move while MOVE . If MOVE doesn’t, move every n time after the DOWN event and stop at UP - YuriySPb
    • here it is a little bit not clear what exactly to enter in DOWN. So far added the code of the method where all this business occurs. - En1q0d