I have an image view that I draw my game board in for my game. I need to get the X, Y of the touch relative to the image view. For example: If someone touched the very top left Pixel, it would return (0,0) or, say the game board was a 100,100 board, and I touched right in the middle I would get (50,50)...How would one do this?
gameView.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
int x = (int) event.getX() / pieceSize;
int y = (int) event.getY() / pieceSize;
Log.d("Touch Event", "Touch event at "+ x + " " +y);
doMove(x, y);
Log.d("Move", "The move was " + doMove(x,y));
}
gameView.setImageBitmap(Draw(pieceSize));
return true;
}
});
You can use
onTouch(View v, MotionEvent event)
Called when a touch event is dispatched to a view.
And retrieve x and y from the event object.
Of course, by setting an OnTouchListener on the view you are working on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With