http://blog.csdn.net/ztp800201/article/details/17218067
Public classResextendsActivityImplementsView.ontouchlistener {Button btn=NULL; intx = 0; inty = 0; intRAWX = 0; intRawy = 0; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); BTN=(Button) Findviewbyid (R.id.button); Btn.setontouchlistener ( This); } Public BooleanOnTouch (view view, Motionevent event) {intEventaction =event.getaction (); Switch(eventaction) { CaseMotionevent.action_down: Break; Casemotionevent.action_move:x= (int) Event.getx (); Y= (int) event.gety (); RAWX= (int) event.getrawx (); Rawy= (int) Event.getrawy (); LOG.D ("DEBUG", "getx=" + x + "gety=" + y + "n" + "getrawx=" +rawx+ "getrawy=" + Rawy + "n"); Break; Casemotionevent.action_up: Break; } return false; } }
When you touch a button, x, Y is relative to the upper-left point of the button (the control itself).
The Rawx,rawy is always relative to the screen position.
Know:
GETRAWX: The coordinates of the touch point relative to the screen
getx: The coordinates of the touch point relative to the button
gettop: The upper-left corner of the button is
getleft: The x-coordinate of the upper-left corner of the button relative to the parent view (linerlayout)
GetX () is the x-coordinate of the widget relative to its upper-left corner, and Getrawx () is the x-coordinate value relative to the upper-left corner of the screen (note: The upper-left corner of the screen is the upper-left corner of the phone screen, regardless of whether the activity has titlebar or full screen), GetY (), Getrawy () The same reason
The difference between getx () and GETRAWX () in Motionevent