Android开发中MotionEvent坐标获取方法分析

  public class MainActivity extends Activity implements OnTouchListener {

  private Button btn;

  private int x = 0, y = 0;

  private int rawX = 0, rawY = 0;

  @Override

  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  btn = (Button) findViewById(R.id.btn);

  btn.setOnTouchListener(this);

  }

  @Override

  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.e("homer", "x = " + x + "; y = " + y + "; rawX = " + rawX + "; rawY = " + rawY);

  break;

  case MotionEvent.ACTION_UP:

  break;

  }

  return false;

  }

  }