Does the drag and drop button exit the screen?

I have one button and I can drag it anywhere on the screen, but it goes off the screen while dragging, so how to drag only inside the screen so that it does not exit the screen

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; } }); 
0
android ontouchlistener
Feb 25 '14 at 13:37
source share
3 answers

First, use the methods View.setX() , View.setY() , View.setTranslateX() , View.setTranslateY() to move the views on the screen instead of updating the LayoutParams fields. I found that they follow a smoother path.

Secondly, to limit views to an available window, get an available window size using the following function:

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

Then, in the onTouch method, calculate whether the target location exceeds the above dimensions. For example:

 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 } 
+1
Feb 25 '14 at 13:50
source share

Change the Listerner OnTouch section as:

  DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); screenHight = displaymetrics.heightPixels; screenWidth = displaymetrics.widthPixels; @SuppressLint("NewApi") @Override public boolean onTouch(View view, MotionEvent event) { float newX, newY; switch (event.getActionMasked()) { case MotionEvent.ACTION_DOWN: dX = view.getX() - event.getRawX(); dY = view.getY() - event.getRawY(); lastAction = MotionEvent.ACTION_DOWN; break; case MotionEvent.ACTION_MOVE: newX = event.getRawX() + dX; newY = event.getRawY() + dY; // check if the view out of screen if ((newX <= 0 || newX >= screenWidth-view.getWidth()) || (newY <= 0 || newY >= screenHight-view.getHeight())) { lastAction = MotionEvent.ACTION_MOVE; break; } view.setX(newX); view.setY(newY); lastAction = MotionEvent.ACTION_MOVE; break; case MotionEvent.ACTION_UP: if (lastAction == MotionEvent.ACTION_DOWN) Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show(); break; default: return false; } return true; } 

Refer to this solution when dragging the image stays on the screen. stack overflow

+3
Apr 05 '16 at 5:34
source share

Signed only in order to answer this because of the disappointment that you found the same, unfortunately, incorrectly, answering all over the Internet.

To process an object that looms outside the view, configure OnDragListener for the object, then call the handler from there - it will work if you do not use the handler, and pass the view identifier to the handler. In addition, I found that your user can also drop the job by pressing two buttons at the same time, and when you switch actions, if the user touches the screen, you can also lose objects in the same way, so the best job was in that my handler is called from the ACTION_DRAG_ENDED drag and drop listener, and the ACTION_UP touch listener just ensures that all that should be visible is that. Thus, every time the user stops touching the screen, he corrects everything that he does.

 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); } } } } }; 
+1
Feb 03 '15 at 19:45
source share



All Articles