Get the Touch Position Inside the Imageview in Android

Get the touch position inside the imageview in android

You can get the top left corner of your view as follows:

int[] viewCoords = new int[2];
imageView.getLocationOnScreen(viewCoords);

From this and the touch coordinates you can calculate the point inside the ImageView:

int touchX = (int) event.getX();
int touchY = (int) event.getY();

int imageX = touchX - viewCoords[0]; // viewCoords[0] is the X coordinate
int imageY = touchY - viewCoords[1]; // viewCoords[1] is the y coordinate

Android: how to detect touch location on ImageView if the image view is scaled by matrix?

getX and getY will return the touch location in the ImageView's coordinate system. If you're looking for the point within the image's coordinate system, you can use the inverse matrix of the matrix used by the ImageView. I've done something like the following:

// calculate inverse matrix
Matrix inverse = new Matrix();
imageView.getImageMatrix().invert(inverse);

// map touch point from ImageView to image
float[] touchPoint = new float[] {event.getX(), event.getY()};
inverse.mapPoints(touchPoint);
// touchPoint now contains x and y in image's coordinate system

Get X/Y coordinates on ImageView touch

Android: How do I get the x y coordinates within an image / ImageView?

imageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View view) {
int[] values = new int[2];
view.getLocationOnScreen(values);
Log.d("X & Y",values[0]+" "+values[1]);
}
});

Something like this?

Get touch event coordinate on an imageview

Write your code to detect the touch at a specified point using a fixed resolution, e.g. 480x640. Then get the resolution of the device the app is running on using DisplayMetrics. Calculate xScale and yScale (e.g. DisplayMetrics.widthPixels / 480) and multiply your x and y by these scale factors.

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

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 touching position of large image in android

OnTouchListener{
@Override
public boolean onTouch(View v, MotionEvent mv) {
float x = mv.getRawX();
float y = mv.getRawY();
return true;
}

This will give you the screen position of the touch. If you need to calculate the X/Y within the image which isn't the full size of the screen you can get the start X/Y coordinates of the image with

v = (ImageView) findViewById(R.id.myImageView);
float v_x = v.getTop();
float v_y = v.getLeft();
float real_x = x - v_x;
float real_y = y - v_y;


Related Topics



Leave a reply



Submit