平滑滚动/惯性滚动/动量滚动
我在 Android 中有一个由翻译矩阵控制的 OpenGL ES 视图。我试图找出一种方法来获得一点动力滚动的感觉,就像在谷歌地图应用程序或 iPhone 中看到的那样。谢谢。
I have an OpenGL ES View in Android thats controlled by a matrix for translation. Im trying to figure out a way to get a hint of momentum scrolling as seen in the google maps app or the iPhone. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果你的问题是二维的,那很简单
你的onTouch函数将找到你手指的加速度。我忘记了如何获得远距离加速度的公式。它应该是位置对时间变量的二阶导数。但您应该始终将 deltaX、deltaY 转换为加速度。为了方便起见,您实际上不需要在那里放置准确的内容。编辑:我不知道为什么我没有看到它,但功能都在那里......
acceleration.x = 2(newposition.x -position.x - speed.x * elapsedTime) / (elapsedTime * elapsedTime);
获得加速度后,您可以使用该代码设置新位置。这是简单的二维物理动力学。通过你的加速度,你可以找到你的速度,通过你的速度,你可以找到你的下一个位置。
质量和摩擦力可以让你定义它的行驶速度以及它自身减速的速度。您可能需要调整代码,因为如果您必须向后滚动才能减慢速度,那么这种动态就不太好。
在每一帧结束时,您必须将加速度重置为 (0,0)。即使在触摸后的每个新帧上,也应将加速度设置为某个值。它应该工作得很好:)
If your problem is in 2d, it is quite simple
Your onTouch function will find the acceleration of your finger. I forgot the formula on how to get the acceleration from a distance. It should be the second derivative of position with a time variable. But you should always convert your deltaX, deltaY in acceleration. To make it easy you don't really need to put something accurate there. Edit: I don't know why I didn't see it but the function was all there...
acceleration.x = 2(newposition.x - position.x - speed.x * elapsedTime) / (elapsedTime * elapsedTime);
Once you have your acceleration you can set your new position with that code. This is simple physic dynamics in 2d. With your acceleration you can find your speed and with your speed you can find your next position.
Mass and friction will let you define how fast it goes and how fast it will slow down by itself. You probably will have to tweak the code because this dynamic isn't exactly nice if you have to have to scroll backward to slow down.
At the end of each frame, you will have to reset your acceleration to (0,0). And on each new frame after a touch even, the acceleration should be set to something. It should work very well :)
像这样的东西:
Something like this: