Move any RelativeLayout with your finger (without changing layout settings)

I have an idea, and I want to drag it with my finger. I wanted to get xDelta and yDelta and just translate the view matrix (not the image, any view, RelativeLayout, for example). I am wondering how to do this:

  • Override RelativeLayout onDraw and apply translation matrix. Is it possible?
  • I am currently doing this using animations in every onTouch event. The animation lasts 0, and the start / end of x / y is the same (setFillAfter = true). He just puts the point of view where I want. But isn't that too expensive?

Can't I manipulate a matrix of any kind directly through onDraw? I tried something like this:

@Override protected void onDraw(Canvas canvas) { if (getDx() != 0 && getDy() != 0) { m = canvas.getMatrix(); m.postTranslate(dx, dy); canvas.setMatrix(m); } super.onDraw(canvas); } 

But it seems like I'm doing it wrong.

+4
source share
4 answers

If you want to implement a drag gesture, you can use onTouchListener and set a padding in your MotionEvent.ACTION_MOVE using setPadding for this view.

Here is a sample code:

 case MotionEvent.ACTION_MOVE: yourView.setPadding((int)event.getRawX(), (int)event.getRawY(), 0, 0); 

Example for onTouchListener: -

 OnTouchListener drag= new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch(event.getAction() & MotionEvent.ACTION_MASK){ case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_POINTER_DOWN: break; case MotionEvent.ACTION_MOVE: img.setPadding((int)event.getRawX(), (int)event.getRawY(), 0, 0); break; case MotionEvent.ACTION_UP: break; case MotionEvent.ACTION_POINTER_UP: break; } return true; } }; 

You need to create a drag and drop image at runtime and adjust its position. In this code example, I did not adjust the position, so the dragged image is not created at the touch position. You also need to consider the height and width of the image to be dragged.

Hope this helps!

+6
source

I hate being the one who tells you that your problem is even simpler than you thought, but here's what it looks like:

 @Override protected void onDraw(Canvas canvas) { if (getDx() != 0 && getDy() != 0) { canvas.translate(getDx(), getDy()); } super.onDraw(canvas); } 

Hope this helps!

+2
source

If you want to transfer your layout forever (in case of temporary use, we can use animation), then use the Drag And Drop API on the official android page . Hope this helps you.

+1
source

A couple of things that will help in achieving it:

  • Make sure your view initially has enough space in the direction you want to use. Like match_parent etc.

  • Use GestureDetector.onSimpleGestureListener and override onDraw (return true;), onScroll - connect it with onTouchEvent and you will immediately get the distance , or you can calculate yourself using event1 and event2

  • Store this information in variables (variables) and call invalidate ()

  • in the onDraw method - use this information canvas.translate (.. use these variables ..); super.onDraw (canvas);

and your result will be achieved.

PS: the drawing starts with the top left being 0.0

+1
source

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


All Articles