How to save X and Y in layout?

I have a FrameLayout that has a thumb image that the user can drag and drop.

The thumb width is 10dp and the height is 10dp.

f = (FrameLayout) findViewById(R.id.fl); f.setOnTouchListener(flt); f.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); width = f.getMeasuredWidth(); height = f.getMeasuredHeight(); @Override public boolean onTouch(View v, MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: if (x<0) { x = x + 10; iv.setX(x); iv.setY(y); } if (x>f.getWidth()) { x = x - 10; iv.setX(x); iv.setY(y); } else { iv.setX(x); iv.setY(y); } // Write your code to perform an action on down break; case MotionEvent.ACTION_MOVE: if (x<0) { x = x + 10; iv.setX(x); iv.setY(y); } if (x>f.getWidth()) { x = x - 10; iv.setX(x); iv.setY(y); } else { iv.setX(x); iv.setY(y); } // Write your code to perform an action on contineus touch move break; case MotionEvent.ACTION_UP: // Write your code to perform an action on touch up break; } // TODO Auto-generated method stub return true; } 

My goal is that if use drags the window out of view on the left, give thumb x + 10 to keep it in view, and if the user drags to the right outside the view, let thumb x-10 keep in sight. But the thumb just disappears if I drag left and right outside the FrameLayout frame.

Here is my XML:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="20dp" android:id="@+id/ll" android:background="#000000" > <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/palette2" android:id="@+id/fl" > <ImageView android:id="@+id/iv" android:layout_width="10dp" android:layout_height="10dp" android:src="@drawable/esquare" /> </FrameLayout> </LinearLayout> 

How can I change the code so that I can achieve the result?

0
source share
1 answer

Have you tried the code from fooobar.com/questions/897917 / ... ?

You let the image go beyond the border, and then try to fix it. Just don't let it go over the border in the first place. Check the coordinates before processing the event, otherwise just a break.

 f = (FrameLayout) findViewById(R.id.fl); f.setOnTouchListener(flt); f.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); width = f.getMeasuredWidth(); height = f.getMeasuredHeight(); @Override public boolean onTouch(View v, MotionEvent event) { float x = event.getX(); float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Write your code to perform an action on down break; case MotionEvent.ACTION_MOVE: if ( (x <= 0 || x >= width) || (y <= 0 || y >= height) ) break; iv.setX(x); iv.setY(y); break; case MotionEvent.ACTION_UP: // Write your code to perform an action on touch up break; } // TODO Auto-generated method stub return true; } 
+1
source

Source: https://habr.com/ru/post/897918/


All Articles