get touch event coordinates relative to the parent - android

I have a linear layout that has an image in it
I am trying to get the touch event coordinates of the image relative to the parent that contains it.
Do you know I can I get these? getX and getY are returning position relative to self

The MotionEvent class has methods getX() to get X touch point corresponding to that view and getRawX() to get X touch point corresponding to screen.
So to get X touch point corresponding to parent you can get that by a simple calculation:
view.getLeft() + motionEvent.getX()
The getLeft() returns Left position of this view relative to its parent

try this:
public boolean onTouch(View v, MotionEvent event) {
System.out.println((v.getLeft() + event.getX()) + "," + (v.getTop() + event.getY()));
return false;
}

Related

How can I set any view to the middle of my touch position?

How can I set any view to the middle of my touch position using onTouchEvent?
Red Circle Is My Click Position
Black Circle Is The View Like Button, ImageView, And So On...
Can someone leave an example?
Edit
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
imgCircle.setX(event.getX() - imgCircle.getWidth() / 2);
imgCircle.setY(event.getY() - imgCircle.getHeight() / 2);
}
return false;
}
Example of a solution:
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
yourView.setX(x);
yourView.setY(y);
return false;
}
You get the coordinates from click and set them to the view. If you have multiple views inside of one you might have to get relative position of the parent view and then subtract the clicked position from parent position.
If you want to set the view position to the centre just subract half of view width from x and half of height from y.
EDIT
How to set position to centre of view:
yourView.setX(x-yourView.getWidth()/2);
yourView.setY(y-yourView.getHeight()/2);

How to get the absolut position of a touch event?

I made a Recylerview and added a button to the left bottom of the Element.
Now when I use:
private View.OnTouchListener onWidthClickListener =
new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
int x = (int) event.getX();
Log.d(TAG, "Width: " + x);
}
return false;
}
};
I get the position of the courser but it is absolute to the button and I want it relative to the hole Screen.
Because it is a RecylerView the button has no fixed position relative to Screen, therefor I can't just add or subtract the values of the layout.
If you want the coordinates relative to the top left corner of the device screen, then use the raw values.
int x = (int)event.getRawX();
int y = (int)event.getRawY();
Exaplain:
getX() and getY()--> return you coordinates relative to the View .
MotionEvent.getRawX() and MotionEvent.getRawY() -->return absolute coordinates,regarding the screen of the device.
For more details:doc
You can use event.getRawX() and event.getRawY() to get position relative to screen.

Get touch position while dragging

I have some views which I like to drag around. The views are within
a LinearLayout, which itself is within a scrollview.
I want to get the position of the current finger (touch), to
make a smooth scroll on my scrollview, depending of the
height of the current drag.
I start my draggin after a long click with the
View build-in listener startDrag
view.startDrag(null, shadowBuilder, view, 0);
I am also able to get the relativ position of the drag
on the view that is currently hovered by
view.setOnDragListener(new OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
//get event positions
}
});
But this only works for the view where the drag shadow is currently on
and DragEvent only provides relative positions, not raw positions.
What I need is the position of the finger, while the drag happens. Unfortunately all onTouchEvents get consumed while dragging.
Does anybody know how I get this to work?
Ps: One way that works (kind of) which I currently use, is to calculate the
position of the touch via the dragEvent.relativePosition in combination
with the view position. But isn't there a better way to go?
OK, since there seems not to be an easier answer, I will provide my own relativly easy solution for further readers, with no need of gesture detection.
At first you need the view that receives the drag event and at second the
drag event ifself (or at least the x and y coordinates).
With fetching of the view position and some simple addition you get the
raw touch position.
This method is tested with the show pointer position developer options
to provide the correct data.
The method for the calculation looks like this:
/**
* #param item the view that received the drag event
* #param event the event from {#link android.view.View.OnDragListener#onDrag(View, DragEvent)}
* #return the coordinates of the touch on x and y axis relative to the screen
*/
public static Point getTouchPositionFromDragEvent(View item, DragEvent event) {
Rect rItem = new Rect();
item.getGlobalVisibleRect(rItem);
return new Point(rItem.left + Math.round(event.getX()), rItem.top + Math.round(event.getY()));
}
Call this method inside of your onDragListener implementation:
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//etc etc. do some stuff with the drag event
break;
case DragEvent.ACTION_DRAG_LOCATION:
Point touchPosition = getTouchPositionFromDragEvent(v, event);
//do something with the position (a scroll i.e);
break;
default:
}
return true;
}
Addtional:
If you want to determinate if the touch is inside of a specific view, you can
do something like this:
public static boolean isTouchInsideOfView(View view, Point touchPosition) {
Rect rScroll = new Rect();
view.getGlobalVisibleRect(rScroll);
return isTouchInsideOfRect(touchPosition, rScroll);
}
public static boolean isTouchInsideOfRect(Point touchPosition, Rect rScroll) {
return touchPosition.x > rScroll.left && touchPosition.x < rScroll.right //within x axis / width
&& touchPosition.y > rScroll.top && touchPosition.y < rScroll.bottom; //withing y axis / height
}
It is also possible to implement a smooth scroll on a ListView based on this solution.
So that the user can drag an item out of a list, and scroll the list via dragging the item either to the top or bottom of the list view.
Cheers.

x and y coordinates of image in Layout

How to getthe X and y coordinates of an image in Layout in Android?
And how to get top, bottom, left and right of an image in Layout in Android?
by using Touch listeners you can get X and Y values of view
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getX();
float y = event.getY();
return false;
}
});
for x and y coordinates of any view compared to its parents , use :
getLeft() , getTop() , getRight() ,and getBottom() .
for raw x and y coordinates of any view compared to the screen/window , use:
getLocationInWindow(int[] location) , getLocationOnScreen(int[] location)
do note that all of those methods won't work till the view is positioned and layout procedure has finished.
more methods are written on the API here.

How to know the X and Y coordinates of a ImageView?

I want to know the X and Y co-ordinates of a Imageview . I have to click on a single imageview , but on different positions i have to perfrom different tasks.
Kindly suggest me how can i know the X and Y co-ordinates of Imageview.
Sorry , I am not posting any code regarding this.
OnTouchListener has a paramater of event where you can get the x and y of the touch.
View.getX() and View.getY() will give you the left most and top most pixels on the screen respectively. Use those in conjunction with View.getWidth() and View.getHeight() and you got the entire pixel grid that the ImageView takes up.
set onTouchListener ........... then use even.getX() and event.getY() ;
you could add an OnTouchListener() to image view
img.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
event.getX() //cordinates wrt view
event.getRawX() //abs cordinates on screen
event.getY() //cordinates wrt view
event.getRawY() //abs cordinates on screen
return false;
}
});

Categories

Resources