How to Get the Touch Position in Android

How to get the Touch position in android?


@Override
public boolean onTouchEvent(MotionEvent event)
{
int x = (int)event.getX();
int y = (int)event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEvent.ACTION_UP:
}

return false;
}

The three cases are so that you can react to different types of events, in this example tapping or dragging or lifting the finger again.

How can I get the x/y location of a touch relative to the entire view when I touch a button?

To get the touch position relative to the view add the touch event's (x,y) to your button's (x,y); to get the button's position relative to it's parent (your view) use the getLeft() and getTop() methods.

The documentation for View has the following under Position:

It is possible to retrieve the location of a view by invoking the
methods getLeft() and getTop(). The former returns the left, or X,
coordinate of the rectangle representing the view. The latter returns
the top, or Y, coordinate of the rectangle representing the view.
These methods both return the location of the view relative to its
parent. For instance, when getLeft() returns 20, that means the view
is located 20 pixels to the right of the left edge of its direct
parent.

Example code for your onTouch method:

float screenX = v.getLeft() + event.getX();   // X in Screen Coordinates
float screenY = v.getTop() + event.getY(); // Y in Screen Coordinates

How to get the absolut position of a touch event?

You can use event.getRawX() and event.getRawY() to get position relative to screen.

Detect touch position in Android

If Using PhoneGap
you can use the HTML/JS.
Refer Below

recommend this link- http://miloq.blogspot.in/2011/05/coordinates-mouse-click-canvas.html

<style type="text/css">

#canvas{background-color: #000;}

</style>

<script type="text/javascript">

document.addEventListener("DOMContentLoaded", init, false);

function init()
{
var canvas = document.getElementById("canvas");
canvas.addEventListener("mousedown", getPosition, false);
}

function getPosition(event)
{
var x = new Number();
var y = new Number();
var canvas = document.getElementById("canvas");

if (event.x != undefined && event.y != undefined)
{
x = event.x;
y = event.y;
}
else // Firefox method to get the position
{
x = event.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = event.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
}

x -= canvas.offsetLeft;
y -= canvas.offsetTop;

alert("x: " + x + " y: " + y);
}

</script>

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

how does detecting touch position on android screen work?

Well, that can be of help to you:
http://developer.android.com/reference/android/view/MotionEvent.html

I suppose the values are the center of the blob or area of the touch. Remember that it can be a tool like a mouse or a stylus so the area will be more precise. It has methods and constants for the supposed area of the finger that is touching.

The link has so many methods about that question, that I will not copy and paste, as its pretty documented, so I think you need to read it.

Some of then:
getPressure()
getSize()
getPointerCount()
getXPrecision()

About the returned value, the section Device Types will give plenty of information.

Some relevant ones:

The interpretation of the contents of a MotionEvent varies significantly depending on
the source class of the device.

On pointing devices with source class SOURCE_CLASS_POINTER such as touch screens, the pointer coordinates specify absolute positions such as view X/Y coordinates. Each complete gesture is represented by a sequence of motion events with actions that describe pointer state transitions and movements.

On joystick devices with source class SOURCE_CLASS_JOYSTICK, the pointer coordinates specify the absolute position of the joystick axes. The joystick axis values are normalized to a range of -1.0 to 1.0

Android: Simplest way to calculate delta of touch position?

Finger remaining stopped on the screen is not a MotionEvent, so this method will not be called.

Try this.

private Handler mHandler = new Handler();
private static final int FINGER_STOP_THRESHOLD = 500;

public boolean onTouchEvent(MotionEvent event) {
touchX = (int) event.getX();
touchY = (int) event.getY();

deltaX = touchX - oldX;
deltaY = touchY - oldY;

oldX = touchX;
oldY = touchY;

mHandler.removeCallbacksAndMessages(null);
if(event.getActionMasked() != MotionEvent.ACTION_UP){
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
deltaX = 0;
deltaY = 0;
}
}, FINGER_STOP_THRESHOLD);
}

return super.onTouchEvent(event);
}

You may change the FINGER_STOP_THRESHOLD to the value you want.

Get the co-ordinates of a touch event on Android

You can use this function : http://developer.android.com/reference/android/view/View.html#setOnTouchListener(android.view.View.OnTouchListener)

You will probably put it in your onCreate method roughly this way (tested this time) :

Activity onCreate code

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView textView = (TextView)findViewById(R.id.textView);
// this is the view on which you will listen for touch events
final View touchView = findViewById(R.id.touchView);
touchView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
textView.setText("Touch coordinates : " +
String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));
return true;
}
});
}

layout code

<?xml version="1.0" encoding="utf-8"?>                                      
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/touchView"
android:background="#f00"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, TestActivity"
/>
</LinearLayout>

Edit: I tried my code and indeed there were a few errors. Anyway, with the layout I give you here it works on my emulator. Can you provide maybe more code/context so I can see what's wrong?

How to get Touch Location on Screen (Android)


public boolean onTouchEvent(MotionEvent e)
{
int xpos=(int) e.getX();
int ypos=(int) e.getY();
switch (e.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d("DEBUG", "On touch (down)" + String.valueOf(xpos) + String.valueOf(ypos));
case MotionEvent.ACTION_UP:
Log.d("DEBUG", "On touch (up)" + String.valueOf(xpos) + String.valueOf(ypos));
case MotionEvent.ACTION_MOVE:
Log.d("DEBUG", "On touch (move)" + String.valueOf(xpos) + String.valueOf(ypos));
break;
}
return true;

}

Then override the draw method

 @Override
public void draw(Canvas canvas) {
super.draw(canvas);
}

So you try

canvas.drawLine(startX, startY, stopX, stopY, paint);

To make a line go straight up try

canvas.drawLine(xpos, ypos, xpos, getHeight() , new Paint());

Get height should be your screen's height (Since your drawing a line straight up)

For this the variables xpos and ypos should be available to all methods so add

int xpos,ypos = 0; 

To the the top of your code (but within the class declaration)

For drawling a between the circles you would need to get the first circles x,y position on touch then set the drawLine method with them, so something like:

currentCircleXpos, lastCircleXpos = 0; //you would set these in the onDraw method when the circle get's drawn.
currentCircleYpos, lastCircleypos = 0;


canvas.drawLine(currentCircleXpos, currentCircleYpos, lastCircleXpos, lastCircleYpos , new Paint());

OnDraw()
method-------

 for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
canvas.drawCircle((x + 1) * xStep, (y + 1) * yStep, 20, pDot);
currentCircleXpos = x;
currentCircleYpos = y; //gets the circle that is being drawn's location
if (y == 0)
{
//canvas.drawLine((x + 1) * xStep, yStep, (x + 1) * xStep, rows * yStep, pDot);
}
}

//canvas.drawLine(xStep, (y + 1) * yStep, cols * xStep, (y + 1) * yStep, pDot);
}

How to get touch position?

To get the touch position start with PreToolManagerListener.onSingleTapConfirmed method in ToolManager.java file. Once you have the touch position, which will be in screen coordinates, you would call PDFViewCtrl.convScreenPtToPagePt To get the coordinates in the particular PDF page. From there you can match up the page coordinates with your map marker.

PDFViewCtrl.convScreenPtToPagePt will handle all page transformations, including layout, zoom, position, rotation, etc.



Related Topics



Leave a reply



Submit