跳ImageView的同时拖动。的getX()和getY()以数值跳跃 [英] Jumping ImageView while dragging. getX() and getY() values are jumping
问题描述
我为拖动查看创建的onTouchListener。拖动图像流畅,如果我使用 getRawX()
和 getRawY()
。这问题是图像会跳转到第二个指针,当你把第二个指针向下然后将第一个指针。
这onTouchListener试图通过跟踪 pointerId
来解决这个问题。这个onTouchListener的问题是拖动某一项时ImageView的,围绕pretty的ImageView的跳跃疯狂。该的getX()
和 getY()以
值四处跳动。
我觉得我正确地这样做。我不希望有编写自定义视图这一点,因为我已经实现了一个scaleGestureDetector,并写了一个自定义的rotateGestureDetector这项工作。一切工作正常,但我需要解决我的时候用得到的问题 getRawX()
和 getRawY()
。
有谁知道我在做什么错在这里?
下面是我的onTouchListener:
最后View.OnTouchListener onTouchListener =新View.OnTouchListener()
{
@覆盖
公共布尔onTouch(视图V,MotionEvent事件)
{
relativeLayoutParams =(RelativeLayout.LayoutParams)v.getLayoutParams();
最终诠释行动= event.getAction();
开关(行动及放大器; MotionEvent.ACTION_MASK)
{
案例MotionEvent.ACTION_DOWN:
{
最终浮动X = event.getX();
最终浮动Y = event.getY();
//当用户开始拖动
lastX = X;
lastY = Y;
activePointerId = event.getPointerId(0);
打破;
}
案例MotionEvent.ACTION_MOVE:
{
//如果用户的手指是在拖动期间
最终诠释pointerIndex = event.findPointerIndex(activePointerId);
最终浮动X = event.getX(pointerIndex);
最终浮动Y = event.getY(pointerIndex);
//计算改变x和改变ÿ
最终浮动DX = X - lastX;
最终浮动DY = Y - lastY;
//更新利润率移动视图
relativeLayoutParams.leftMargin + = DX;
relativeLayoutParams.topMargin + = DY;
v.setLayoutParams(relativeLayoutParams);
//保存在用户的手指对于下一ACTION_MOVE
lastX = X;
lastY = Y;
v.invalidate();
打破;
}
案例MotionEvent.ACTION_UP:
{
activePointerId = INVALID_POINTER_ID;
打破;
}
案例MotionEvent.ACTION_CANCEL:
{
activePointerId = INVALID_POINTER_ID;
打破;
}
案例MotionEvent.ACTION_POINTER_UP:
{
//提取指针的留下的触摸传感器的索引
最终诠释pointerIndex =(行动及放大器; MotionEvent.ACTION_POINTER_INDEX_MASK)>> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
最终诠释pointerId = event.getPointerId(pointerIndex);
如果(pointerId == activePointerId)
{
//这是我们主动指针往上走。选择一个新的
//活跃的指针,并相应地调整
最终诠释newPointerIndex = pointerIndex == 0? 1:0;
lastX =(INT)event.getX(newPointerIndex);
lastY =(INT)event.getY(newPointerIndex);
activePointerId = event.getPointerId(newPointerIndex);
}
打破;
}
}
返回true;
}
};
image1.setOnTouchListener(onTouchListener);
这个问题很简单,但意外。 getRawX / Y()
返回绝对坐标,而的getX / Y()
返回坐标相对于视图。我会提出的观点,重置 lastX / Y
,并且图像不会在同一个地方了,所以当我得到新的价值,他们会关闭。在这种情况下,我只需要在我的原来 $ P $(当使用`getRawX / Y'不是这样的)。
所以,解决的办法是简单地删除以下内容:
//保存在用户的手指是在未来ACTION_MOVE
lastX = X;
lastY = Y;
我希望这将帮助别人的未来,因为我看到过别人这个问题,他们也有类似的code,我(重置 lastX / Y
)
I've created an onTouchListener for dragging Views. Images drag smoothly if I use getRawX()
and getRawY()
. The problem with that is the image will jump to the second pointer when you place a second pointer down then lift the first pointer.
This onTouchListener attempts to fix that issue by keeping track of the pointerId
. The problem with this onTouchListener is while dragging an ImageView, the ImageView jumps around pretty crazily. The getX()
and getY()
values jump around.
I feel like I'm doing this correctly. I don't want to have to write a custom view for this because I've already implemented a scaleGestureDetector and written a custom rotateGestureDetector that work. Everything works fine but I need to fix the issue I get when using getRawX()
and getRawY()
.
Does anybody know what I'm doing wrong here?
Here's my onTouchListener:
final View.OnTouchListener onTouchListener = new View.OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
relativeLayoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams();
final int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK)
{
case MotionEvent.ACTION_DOWN:
{
final float x = event.getX();
final float y = event.getY();
// Where the user started the drag
lastX = x;
lastY = y;
activePointerId = event.getPointerId(0);
break;
}
case MotionEvent.ACTION_MOVE:
{
// Where the user's finger is during the drag
final int pointerIndex = event.findPointerIndex(activePointerId);
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);
// Calculate change in x and change in y
final float dx = x - lastX;
final float dy = y - lastY;
// Update the margins to move the view
relativeLayoutParams.leftMargin += dx;
relativeLayoutParams.topMargin += dy;
v.setLayoutParams(relativeLayoutParams);
// Save where the user's finger was for the next ACTION_MOVE
lastX = x;
lastY = y;
v.invalidate();
break;
}
case MotionEvent.ACTION_UP:
{
activePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_CANCEL:
{
activePointerId = INVALID_POINTER_ID;
break;
}
case MotionEvent.ACTION_POINTER_UP:
{
// Extract the index of the pointer that left the touch sensor
final int pointerIndex = (action & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = event.getPointerId(pointerIndex);
if(pointerId == activePointerId)
{
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
lastX = (int) event.getX(newPointerIndex);
lastY = (int) event.getY(newPointerIndex);
activePointerId = event.getPointerId(newPointerIndex);
}
break;
}
}
return true;
}
};
image1.setOnTouchListener(onTouchListener);
The issue was simple, but unexpected. getRawX/Y()
returns absolute coordinates, while getX/Y()
returns coordinates relative to the view. I would move the view, reset lastX/Y
, and the image wouldn't be in the same spot anymore so when I get new values they'd be off. In this case I only needed where I originally pressed the image (not the case when using `getRawX/Y').
So, the solution was to simply remove the following:
// Save where the user's finger was for the next ACTION_MOVE
lastX = x;
lastY = y;
I hope this will help somebody in the future, because I've seen others with this problem, and they had similar code to me (resetting lastX/Y
)
这篇关于跳ImageView的同时拖动。的getX()和getY()以数值跳跃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!