android - 在 onTouch 中没有得到正确的 x y 值

标签 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

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

最佳答案

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

关于android - 在 onTouch 中没有得到正确的 x y 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8893216/

相关文章:

android - PagerAdapter 总是返回空白

android - 渲染前测量文本长度

Android Things Pin 命名约定

java - 如何清除字符串数组 Android/Java 中的结果

java - 安卓 NDK 计时器

android - 如何在我的 Android 应用程序中应用 ICS 样式

android - HTML5 视频 - 在 Android 设备上精确寻找帧?

android - 如何以编程方式设置 listView 的 state_pressed 颜色

android - 从 getSharedPreferences(自定义文件)迁移到 getDefaultSharedPreferences()

java - View.OnClickListener 在 Android 中用作参数