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
Downloading/Caching Google Maps for Offline Use
Android Bluetoothdevice.Getname() Return Null
Mediarecorder Start Failed: -38
How to Get Contacts from Native Phonebook in Android
Android Studio Project Structure (V.S. Eclipse Project Structure)
Android Check Permission for Locationmanager
Format Statement in a String Resource File
Monitor Network Activity in Android Phones
Remove Vertical Padding from Horizontal Progressbar
How to Make an Android Editview 'Done' Button and Hide the Keyboard When Clicked
What Is a Maximum Size of SQLite Database on Android
Forgot Keystore Password, Thinking of Brute-Force Detection. Will It Corrupt the Keystore
Android Ndk Build, Method Could Not Be Resolved
Retrieve String Out of Addvalueeventlistener Firebase
How to Decompile an APK or Dex File on Android Platform
Android How to Implement Bottom Sheet from Material Design Docs