使用Android上的RelativeLayout为onTouchListener奇怪的跳跃值 [英] Strange jumping values for onTouchListener using a RelativeLayout in Android

查看:456
本文介绍了使用Android上的RelativeLayout为onTouchListener奇怪的跳跃值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为了拖动一个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 131
11-27 19:43:34.504:信息/拖(3530):84拖288
11-27 19:43:34.519:信息/拖(3530):拖132 134
11-27 19:43:34.539:信息/拖(3530):84拖292
11-27 19:43:34.554:信息/拖(3530):拖132 139
11-27 19:43:34.574:信息/拖(3530):84拖294
11-27 19:43:34.594:信息/拖(3530):拖132 142
11-27 19:43:34.609:信息/拖(3530):84拖294


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

解决方案

。希望它能帮助

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

这篇关于使用Android上的RelativeLayout为onTouchListener奇怪的跳跃值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆