拖动按钮是否会离开屏幕?

时间:2014-02-25 13:37:36

标签: android ontouchlistener

我有一个按钮,我可以将它拖动到屏幕上的任何位置,但它在拖动时会移出屏幕,所以如何只在屏幕内拖动,这样它就不会出现在屏幕外

Button.setOnTouchListener(new TextView.OnTouchListener() {
        public boolean onTouch(View view, MotionEvent event) {              

            X_button = (int) event.getRawX();
            Y_button= (int) event.getRawY();


            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:

                RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                _xDelta = X_button - lParams.leftMargin;
                _yDelta = Y_button - lParams.topMargin;
                break;
            case MotionEvent.ACTION_UP:                 
                 if(!isMoving)
                 {
                    view.performClick();                        
                 }  

                 isMoving=false;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                break;
            case MotionEvent.ACTION_POINTER_UP:
                break;
            case MotionEvent.ACTION_MOVE:                   
                isMoving=true;                  
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                        .getLayoutParams();
                layoutParams.leftMargin = X_button - _xDelta;
                layoutParams.topMargin = Y_button - _yDelta;
                layoutParams.rightMargin = -250;
                layoutParams.bottomMargin = -250;
                view.setLayoutParams(layoutParams);

                break;
            }
            return true;
        }
    });

3 个答案:

答案 0 :(得分:5)

将OnTouch listerner部分修改为:

    function checkName(val) {    
    var alpha = document.getElementById(val).value;    
    var filter = /^[a-zA-Z0-9 ]*$/;    
    if (!filter.test(alpha)) {
        alert("Please Enter Alphanumeric Only");
        return false;                                                   
    } 
    return true;   
  }  

当拖动图像停留在屏幕上时,请参阅此解决方案。 https://stackoverflow.com/a/36417605/4324288

答案 1 :(得分:1)

首先,使用方法View.setX()View.setY()View.setTranslateX()View.setTranslateY()在屏幕上移动视图,而不是更新LayoutParams的边距。我发现它们的表现更加顺畅。

其次,要将视图限制为可用窗口,请使用以下函数获取可用的窗口大小:

DisplayMetrics metrics = getResources().getDisplayMetrics();
int windowWidth = metrics.widthPixels;
int windowHeight = metrics.heightPixels

接下来,在onTouch方法中,计算目标位置是否超过上述尺寸。例如:

if( currentXLocation + deltaX > windowWidth ){

    // this will ensure that target location 
    // is always <= windowHeight
    deltaX = windowWidth - currentXLocation; 

} else if( currentXLocation + deltaX < 0){

    deltaX = -(currentXLocation);

} else if (...){

    // perform similar calculations for the rest 

}

答案 2 :(得分:1)

因为在互联网上找到相同的,不幸的错误的答案而感到沮丧,所以请注意回答这个问题。

要处理在视图外部绘制的对象,请为对象设置OnDragListener,然后从那里调用处理程序 - 如果您不使用处理程序,它将崩溃 - 并将视图ID传递给处理程序。除此之外,我发现你的用户也可以通过在完全相同的时间触摸两个按钮来破坏工作,当你切换活动时,如果用户触摸屏幕,那么你也可以丢失对象 - 所以最有效的是我的处理程序从拖动侦听器ACTION_DRAG_ENDED调用,触摸侦听器ACTION_UP只确保应该可见的所有内容。这样,每当用户停止触摸屏幕时,它将修复他/她所做的任何事情。

private final class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                ClipData data = ClipData.newPlainText("", "");
                DragShadowBuilder shadowBuilder = new DragShadowBuilder(view);
                view.startDrag(data, shadowBuilder, view, 0);
                view.setVisibility(View.INVISIBLE);
                return true;
            case MotionEvent.ACTION_UP:
                drophandler.sendEmptyMessage(0);
            case MotionEvent.ACTION_MOVE:
                return true;
            default:
                break;
            }
        return true;
    }
}

class MyDragListener implements OnDragListener {
    @Override
    public boolean onDrag(View v, DragEvent event) {
        View view = (View) event.getLocalState();
        Button drag = (Button) view;
        int action = event.getAction();

        switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                break;
            case DragEvent.ACTION_DRAG_ENTERED:
                break;
            case DragEvent.ACTION_DRAG_EXITED:
                break;
            case DragEvent.ACTION_DROP:
                Button target = (Button) v;
                //do stuff
                return true;
            case DragEvent.ACTION_DRAG_ENDED:
                drophandler.sendEmptyMessage(0);
                return true;
            default:
                break;
        }
        return true;
    }
}

private Handler drophandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        for(int x = 0; x < 3 + difficulty; x++){
            for(int y = 0; y < 5 + difficulty; y++){
                if (buttons[x][y] != null){
                    buttons[x][y].setVisibility(View.VISIBLE);
                }
            }
        }
    }
};