简体   繁体   English

限制视图的移动以防止移出屏幕

[英]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;
        }
    });

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. 只需跟踪对象的xPosition(每次移动时从类变量中添加/减去)就可以在移动对象之前在其中添加检查。 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. 和向左移动图像的代码中的相反位置( xPosition > buffer ),其中buffer是您想要在屏幕边缘上的一些边距。 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;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM