Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/solr/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android中使用RelativeLayout的onTouchListener的奇怪跳跃值_Android - Fatal编程技术网

Android中使用RelativeLayout的onTouchListener的奇怪跳跃值

Android中使用RelativeLayout的onTouchListener的奇怪跳跃值,android,Android,为了在屏幕上拖动ImageView,我编写了以下函数。似乎工作正常,但拖动图像时会疯狂地跳跃 查看日志,问题似乎是我在每个正确值之间得到了不正确的X和Y值。我不知道为什么。有人能帮我修一下吗 hereOnTouchListener imageListener = new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method

为了在屏幕上拖动ImageView,我编写了以下函数。似乎工作正常,但拖动图像时会疯狂地跳跃

查看日志,问题似乎是我在每个正确值之间得到了不正确的X和Y值。我不知道为什么。有人能帮我修一下吗

hereOnTouchListener imageListener = new OnTouchListener(){

@Override
public boolean onTouch(View v, MotionEvent event) {

  // TODO Auto-generated method stub
  int eventAction = event.getAction();

  int X = (int)event.getX();
  int Y = (int)event.getY();
  if (eventAction == MotionEvent.ACTION_DOWN){
   dragging = true;
   tempParams = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
Log.i("v width and height", v.getWidth() + " " + v.getHeight());
  }     
  if (eventAction == MotionEvent.ACTION_UP){
   Log.i("dragging up","dragging up" + X + " " + Y);
   dragging = false;
  } else if (eventAction == MotionEvent.ACTION_MOVE){
   if (dragging){
    Log.i("dragging","dragging " + X + " " + Y);
    tempParams.leftMargin = X;
    tempParams.topMargin = Y;
    v.setLayoutParams(tempParams);
   // v.setPadding(X, Y, 0, 0);
    v.invalidate();
   }
  }


  return true;
 }

};
日志输出示例:

11-27 19:43:34.484: INFO/dragging(3530): dragging 131 131
11-27 19:43:34.504: INFO/dragging(3530): dragging 84 288
11-27 19:43:34.519: INFO/dragging(3530): dragging 132 134
11-27 19:43:34.539: INFO/dragging(3530): dragging 84 292
11-27 19:43:34.554: INFO/dragging(3530): dragging 132 139
11-27 19:43:34.574: INFO/dragging(3530): dragging 84 294
11-27 19:43:34.594: INFO/dragging(3530): dragging 132 142
11-27 19:43:34.609: INFO/dragging(3530): dragging 84 294

我终于明白了。问题是我使用的是event.getX(),所以当我用手指拖动它时,我想它是基于ImageView以及基于底层表面的值来计算的。

实际上,这是因为您正在更改设置了onTouchListener的同一视图的边距。为了避免这种情况,您应该使用单独的视图-一个视图应该从其onTouchListener中获取getX()或getY(),并使用该信息更改另一个视图的边距

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/touch_listener"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</FrameLayout>


@i82不久前,我也遇到了同样的问题,我也在使用getX()方法,但将其改为getRawX(),效果很好。希望对你有所帮助。重新格式化您的问题,使其更具可读性。尝试此方法,似乎没有帮助。我有一个RelativeLayout,里面有一个ImageView,ImageView上有一个onTouchListener。它仍然非常紧张。@dpk-发生这种情况是因为您的ImageView位于截取触摸的相对位置窗口内。无论谁在听触摸,都需要和你的亲戚处于相同的嵌套级别。我用一个代码示例更新了我的答案。我最终做了一些不同的事情——我将OnTouchListener移动到包装RelativeLayout,并让它移动其子ImageView。我不确定这是否理想,但它做到了。