简体   繁体   中英

Android onTouch

I have been searching through the site and haven't quite found what I was looking for. There are 1,000+ posts to look through. What I am trying to do is get the location of where the user is pressing and based on which quarter of the screen, call another method that will display a new background image. I know I need to use the OnTouch and implement the OnTouchListener at the beginning. I also know about

windowwidth = getWindowManager().getDefaultDisplay().getWidth();
windowheight = getWindowManager().getDefaultDisplay().getHeight();

to figure out the width and height of the screen because different phones have different sizes. I just cannot seem to be able to put it all together. Any help would be appriciated.

Here's an article that deals with retrieving coordinates on the screen when it is pressed. You'll just need to divide the height and width by 2 in order to create your quarters and you'll be on your way.

link: http://blorb.tumblr.com/post/347157141/simple-ontouch-press-and-release-code-for-android

Inside the onTouch listener of your view just use this code :

String.valueOf(event.getX()) + "x" + String.valueOf(event.getY()));

You will get th X and Y coordinates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM