You may want to get the coordinates of the position tapped by the user on the screen of the Android device, such as when you have the stamp using static electricity pressed on the application and check the consistency of the stamp by collating the touched coordinates. .. In that case, you can get multiple coordinates of the tapped position by overriding the onTouchEvent or dispatchTouchEvent method of the Activity class.
Activity class
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
this.onTouchEvent(event);
return super.dispatchTouchEvent(event);
}
private void onTouchEvent(MotionEvent motionEvent) {
//Get the tapped position (excluding when you move your finger without releasing it)
if (event.getActionMasked() == MotionEvent.ACTION_DOWN ||
event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN) {
StringBuilder builder = new StringBuilder();
this.coordinates = new ArrayList<>();
int count = event.getPointerCount();
//Corresponds to when multiple places are tapped
for (int i = 0; i < count; i++) {
int x = (int) event.getX(i);
int y = (int) event.getY(i);
Logger.d("# X: " + x + ", y: " + y + ", PointerID: " + event.getPointerId(i));
builder.append("(" + x + "," + y + "),");
Coordinate coordinate = new Coordinate(x, y);
this.coordinates.add(coordinate);
}
Logger.d("#Coordinate: " + builder.toString());
}
}
Coordinate class
public class Coordinate {
private int x;
private int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Recommended Posts