使用Android上的RelativeLayout为onTouchListener奇怪的跳跃值奇怪、Android、RelativeLayout、onTouchListener

2023-09-06 22:08:16 作者:好听的游戏名字七个字:三分长相七分妆

我为了拖动一个ImageView的在屏幕上写下了以下的功能。似乎除了同时拖动图像周围跳跃像疯了似的好吗工作。

看问题似乎是我得到的每一个正确的值之间的X和Y的值不正确的日志。我不知道为什么,虽然。谁能帮我解决这个问题?

  hereOnTouchListener imageListener =新OnTouchListener(){@覆盖公共布尔onTouch(视图V,MotionEvent事件){  // TODO自动生成方法存根  INT eventAction = event.getAction();  INT X =(int)的event.getX();  INT Y =(int)的event.getY();  如果(eventAction == MotionEvent.ACTION_DOWN){   拖动= TRUE;   tempParams =新RelativeLayout.LayoutParams(v.getWidth(),v.getHeight());Log.i(V宽度和高度,v.getWidth()++ v.getHeight());  }  如果(eventAction == MotionEvent.ACTION_UP){   Log.i(拖了,拖了+ X ++ Y);   拖动= FALSE;  }否则如果(eventAction == MotionEvent.ACTION_MOVE){   如果(拖){    Log.i(拖,拖+ X ++ Y);    tempParams.leftMargin = X;    tempParams.topMargin = Y;    v.setLayoutParams(tempParams);   // v.setPadding(X,Y 0,0);    v.invalidate();   }  }  返回true; }}; 

日志输出的示例:

  19 11-27:43:34.484:信息/拖(3530):拖131 13111-27 19:43:34.504:信息/拖(3530):84拖28811-27 19:43:34.519:信息/拖(3530):拖132 13411-27 19:43:34.539:信息/拖(3530):84拖29211-27 19:43:34.554:信息/拖(3530):拖132 13911-27 19:43:34.574:信息/拖(3530):84拖29411-27 19:43:34.594:信息/拖(3530):拖132 14211-27 19:43:34.609:信息/拖(3530):84拖294 

@ I82Much我一些前段时间我使用的getX()方法也是有同样的问题,而是把它改为getRawX()和正常工作解决方案

。希望它能帮助

Android布局控件之RelativeLayout详解

I wrote the following function in order to drag around an ImageView on the screen. 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. 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

解决方案

@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. Hope it helps