【问题标题】:Didn't get the correct x y value in onTouch在 onTouch 中没有得到正确的 x y 值
【发布时间】:2012-01-17 10:41:41
【问题描述】:

我想在屏幕上拖动时移动图像,但 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

替代点是正确的。但它是怎么发生的?我该如何解决这个问题?

【问题讨论】:

  • 你说 x 和 y 不正确,然后你说“交替点是正确的”。 ??那么它是正确的还是错误的呢?问之前先下定决心:)
  • 这是我在屏幕上从 x 坐标 102 拖动到 114 时的输出。但其他一些值也随之而来
  • 问题已解决....我已将触摸监听器添加到视图中。它每时每刻都在重绘。我在布局中添加了触摸监听器。谢谢

标签: android


【解决方案1】:

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

【讨论】:

  • 问题已解决.... 我已将触摸侦听器添加到视图中。它每时每刻都在重绘。我在布局中添加了触摸监听器。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-20
  • 1970-01-01
  • 2022-10-19
  • 2022-08-16
  • 2017-03-27
相关资源
最近更新 更多