javaandroidimageontouchlistenerontouch

Limit the movement of a view to prevent moving off the screen


I current'y have an image that can be moved either Left or Right, depending on whether the User touches the Left Or Right part of their device's screen. However, I don't want the User to move the image off of the screen! So I was wondering, can I put a limit or restriction on how far the User can move the image Left or Right? Here is the code for the image that moves (when the Left or Right part of the device's screen is touched)

   //OnTouch Function
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int screenWidth = getResources().getDisplayMetrics().widthPixels;
            int x = (int)event.getX();
            if ( x >= ( screenWidth/2) ) {
                int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
                float Xtouch = event.getRawX();
                int sign = Xtouch > 0.5 * ScreenWidth ? 1 : -1;
                float XToMove = 85;
                int durationMs = 50;
                v.animate().translationXBy(sign*XToMove).setDuration(durationMs);
            }else {
                if( x < ( screenWidth/2) ) {
                    int ScreenWidth = getResources().getDisplayMetrics().widthPixels;
                    float xtouch = event.getRawX();
                    int sign = xtouch < 0.5 / ScreenWidth ? 1 : -1;
                    float xToMove = 60; // or whatever amount you want
                    int durationMs = 50;
                    v.animate().translationXBy(sign*xToMove).setDuration(durationMs);
                }
            }
            return false;
        }
    });

Solution

  • Just keep track of xPosition of the object (add/subtract from a class variable every time it moves) add a check in there before moving the object. As in

    if( xPosition < ScreenWidth-buffer ) {
        //run code to move object right
    }
    

    and the opposite (xPosition > buffer) in the code that moves the image left, where buffer is some margin you want on the edge of the screen. For example:

    private float xPosition; // set to initial position in onCreate
    
    //OnTouch Function
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int screenWidth = getResources().getDisplayMetrics().widthPixels;
            float x = event.getRawX();
            int durationMs = 50;
            int buffer = 90;
            if ( x >= ( screenWidth/2) && xPosition < screenWidth-buffer ) {
                float XToMove = 85;
                v.animate().translationXBy(XToMove).setDuration(durationMs);
                xPosition += XToMove;
            }else if( x < ( screenWidth/2) && xPosition > buffer ) {
                float XToMove = -60; // or whatever amount you want
                v.animate().translationXBy(XToMove).setDuration(durationMs);
                xPosition += XToMove;
            }
            return false;
        }
    });