简体   繁体   English

在ACTION_MOVE期间,准确地确定手指在Android中的移动距离

[英]Precisely determine how far a finger has moved in Android during ACTION_MOVE

In my onTouchEvent method's ACTION_MOVE I'm attempting to find out how many pixels the users finger has moved during a touch gesture - this amount will be updated every frame and an object on the screen will be moved by the same amount. 在我的onTouchEvent方法的ACTION_MOVE中,我试图找出用户手指在触摸手势中移动了多少像素 - 这个数量将每帧更新,屏幕上的对象将移动相同的量。

This is what I'm doing so far: 这就是我到目前为止所做的事情:

case MotionEvent.ACTION_MOVE: {

            movedByY = oldYPosition - event.getY();

            oldYPosition = event.getY();

}

I'm only really concerned with movement in the Y direction and the above seems fairly simple. 我只关心Y方向的运动而且上面看起来相当简单。

Once I've got the amount, I move my object by the same amount - something like this. 一旦我拿到了金额,我就会移动相同数量的物体 - 就像这样。

myObject.currentYPos -= movedByY;

This kind of works, however, it isn't precise enough. 这类作品 ,但是,它是不够精确。 When moving downwards, the finger gets slightly ahead of the object (object moves too slow) and when moving upwards, the object gets slightly ahead of the finger (object moves too fast). 当向下移动时,手指略微超出物体(物体移动得太慢),当向上移动时,物体略微超出手指(物体移动得太快)。 The slower you move your finger, the bigger the discrepency. 移动手指越慢,差异越大。

Note I can't simply set the object's coordinates to match the finger because that's not what I'm trying to achieve. 注意我不能简单地设置对象的坐标以匹配手指,因为这不是我想要实现的。 If the user places a finger down on the screen and moves it, the object should mimic it's movements extactly but not 'jump to' where it is. 如果用户将手指放在屏幕上并移动它,则该对象应该可以模拟它的移动,但不能“跳到”它所在的位置。

Let's say the object is at 100,100 and the user places their finger down at 600 x 586 and moves 100 pixels upwards, then the object should follow this path and end up at 100 x 0 - and as we're updating this every frame, at any given frame the amount the object as moved, should match the amount the finger has moved exactly . 假设对象位于100,100并且用户将手指向下移动600 x 586并向上移动100个像素,则对象应遵循此路径并以100 x 0结束 - 当我们每帧更新此对象时,任何给定的帧为移动的对象的数量,应匹配手指已经正好移动的量。

I can't figure out why it's moving at a different speed to the finger. 我无法弄清楚为什么它以不同的速度移动到手指。 Any clues would be appreciated. 任何线索将不胜感激。

I managed to get around the problem by working out the movedByY value in my GLRendering thread (in my update() method). 我设法通过在GLRendering线程(在我的update()方法中)计算movingByY值来解决问题。

So, in ACTION_MOVE - I did this: 所以,在ACTION_MOVE中 - 我这样做了:

case MotionEvent.ACTION_MOVE: {

        onTouchYCoord = event.getY();

}

And then in my logic update method, used that value to get the correct amount like so: 然后在我的逻辑更新方法中,使用该值来获取正确的数量,如下所示:

logicYCoord = (int) onTouchYCoord ;

movedByY = logicYCoord -oldYPosition;

oldYPosition = logicYCoord;

Then I update the sprite's position: 然后我更新精灵的位置:

myObject.currentYPos += movedByY;

This works perfectly and the object stays with the finger. 这完美地工作,并且物体保持在手指上。 Clearly, the problem was the fact that I was using a value from the UI Thread which was could have changed at any time. 很明显,问题在于我使用的是UI线程中可能随时更改的值。 This way, I'm guaranteed the value I'm using in my calculations stays consistent by taking a snapshot of it. 这样,我保证我在计算中使用的值通过拍摄它的快照保持一致。

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

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