没有在onTouch中获得正确的x y值

时间:2012-01-17 10:41:42

标签: android

我想在屏幕上拖动时移动图像,但ACTION_MOVE中的x和y值不正确。我在ACTION_MOVE中打印了x和y值。在每个替代中,它打印正确的x和y值。

public boolean onTouch(View v, MotionEvent event) {
  ImageView view = (ImageView) v;
  float x=0;
  float y=0;

  switch (event.getAction()) {
  case MotionEvent.ACTION_DOWN:

     break;

 case MotionEvent.ACTION_UP:

     break;


  case MotionEvent.ACTION_MOVE:
      x=event.getX();
      y=event.getY();
      System.out.println("x= "+x+" y="+y);
      int width = 100, height = 100;
      lp = new AbsoluteLayout.LayoutParams(width, height, (int) (x), (int) (y));

    break;
  }
  mLayout.updateViewLayout(view, lp);

  return true;
 }

这是我的代码。打印输出是:

01-17 10:31:11.721: I/System.out(3983): x= 71.0 y=91.0
01-17 10:31:11.746: I/System.out(3983): x= 102.0 y=24.0
01-17 10:31:11.776: I/System.out(3983): x= 72.0 y=91.0
01-17 10:31:11.806: I/System.out(3983): x= 103.0 y=24.0
01-17 10:31:11.826: I/System.out(3983): x= 73.0 y=91.0
01-17 10:31:11.851: I/System.out(3983): x= 104.0 y=24.0
01-17 10:31:11.871: I/System.out(3983): x= 74.0 y=91.0
01-17 10:31:11.896: I/System.out(3983): x= 105.0 y=24.0
01-17 10:31:11.921: I/System.out(3983): x= 75.0 y=91.0
01-17 10:31:11.941: I/System.out(3983): x= 107.0 y=24.0
01-17 10:31:11.957: I/System.out(3983): x= 76.0 y=90.0
01-17 10:31:11.976: I/System.out(3983): x= 109.0 y=24.0
01-17 10:31:11.996: I/System.out(3983): x= 77.0 y=90.0
01-17 10:31:12.026: I/System.out(3983): x= 110.0 y=24.0
01-17 10:31:12.056: I/System.out(3983): x= 78.0 y=90.0
01-17 10:31:12.101: I/System.out(3983): x= 111.0 y=24.0
01-17 10:31:12.131: I/System.out(3983): x= 79.0 y=89.0
01-17 10:31:12.186: I/System.out(3983): x= 112.0 y=24.0
01-17 10:31:12.231: I/System.out(3983): x= 80.0 y=89.0
01-17 10:31:12.276: I/System.out(3983): x= 113.0 y=24.0
01-17 10:31:12.296: I/System.out(3983): x= 80.0 y=88.0
01-17 10:31:12.316: I/System.out(3983): x= 114.0 y=24.0
01-17 10:31:12.336: I/System.out(3983): x= 81.0 y=88.0

备用点是正确的。但它是如何发生的?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果您查看此question的答案,他说视图可能会内置触摸事件,这可能会干扰您的触摸事件。检查您的观点是否有。