When dragging an image, the screen

I want to implement a movable imageView while dragging it. Drag and drop works fine. But when I drag to the end, the imageView exits the screen. How can I make a movable image only inside the screen?

This is my OnTouch lettering:

public boolean onTouch(View view, MotionEvent event) { 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: view.setY(event.getRawY() + dY); view.setX(event.getRawX() + dX); 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; } 

and this is my layout. the layout contains scrollview and their child and the image I want to drag.

 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="400dp" android:background="#ff0000" android:gravity="center" android:text="View 1" android:textColor="#000" android:textSize="100dp" /> <TextView android:layout_width="match_parent" android:layout_height="400dp" android:background="#00ff00" android:text="View 2" android:textColor="#000" android:textSize="100dp" /> </LinearLayout> </ScrollView> <ImageView android:id="@+id/draggable_view" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="bottom|right" android:layout_marginBottom="20dp" android:layout_marginEnd="20dp" android:background="@drawable/icon" /> </FrameLayout> 
0
android ontouchlistener drag
Apr 04 '16 at 12:29
source share
1 answer

Having spent more time on this, we will find the best solution.

Change the Listerner section in OnTouch 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; } 

It works great :)

+2
Apr 05 '16 at 5:13
source share



All Articles