首先我们通过一个button进行一下测试,可以知道:
public class Res extends Activity implements View.OnTouchListener { Button btn = null; int x = 0; int y = 0; int rawx = 0; int rawy = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.button); btn.setOnTouchListener(this); } public boolean onTouch(View view, MotionEvent event) { int eventaction = event.getAction(); switch (eventaction) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.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; case MotionEvent.ACTION_UP: break; } return false; } }
getX()是表示Widget相对于自身左上角的x坐标,而getRawX()是表示相对于屏幕左上角的x坐标值(注意:这个屏幕左上角是手机屏幕左上角,不管activity是否有titleBar或是否全屏幕),getY(),getRawY()一样的道理