简体   繁体   English

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

[英]Strange jumping values for onTouchListener using a RelativeLayout in Android

I wrote the following function in order to drag around an ImageView on the screen. 我编写了以下函数,以便在屏幕上的ImageView周围拖动。 Seems to work alright except while dragging the image jumps around like crazy. 似乎工作正常,除了拖动图像时像疯了似的跳来跳去。

Looking at the log the problem seems to be I'm getting incorrect values for X and Y in between every correct value. 查看日志,问题似乎出在每个正确值之间,我得到的X和Y值不正确。 I'm not sure why though. 我不确定为什么。 Can anyone help me fix this? 谁能帮我解决这个问题?

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;
 }

};

sample of log output: 日志输出示例:

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

Actually this is happening because you are changing the margins of the same view that has the onTouchListener set. 实际上,这是因为您要更改具有onTouchListener设置的同一视图的页边距。 To avoid this, you should use separate views - one view should grab the getX() or getY() from its onTouchListener and use that info to change the margins on the other view. 为了避免这种情况,您应该使用单独的视图-一个视图应该从其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>

@I82Much I had this same issue some while ago i also was using the getX() method but instead changed it to getRawX() and it works fine. @ I82不久前我也遇到了同样的问题,我也使用了getX()方法,但是改为将其更改为getRawX(),并且工作正常。 Hope it helps 希望能帮助到你

Finally figured it out. 终于想通了。 Problem was I was using event.getX() so I think as I was dragging my finger around it was calculating a value based on the ImageView as well as one based on the underlying surface. 问题是我正在使用event.getX(),所以我想在我拖着它的同时,它正在基于ImageView以及基于基础表面的值进行计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM