平滑滚动/惯性滚动/气势滚动 [英] smooth scroll/inertial scrolling/momentum scroll

查看:380
本文介绍了平滑滚动/惯性滚动/气势滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OpenGL ES视图在一个矩阵转换控制Android的多数民众赞成。林试图找出一种方式来获得的动能暗示滚动中的谷歌地图应用程序或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.

推荐答案

如果你的问题是2D的,这是很简单的。

If your problem is in 2d, it is quite simple

  1. 您需要获得所用时间在每一帧
  2. 您onTouch功能会发现你的手指的加速度。我忘了如何让从远处加速度的公式。它应该是与时间可变位置的二阶导数。但是,你应该始终把你DELTAX,移动deltaY加速。为了方便你并不真的需要把一些东西准确那里。编辑:我不知道为什么我没有看到,但功能基本都在那里......

  1. You need to get the elapsed time in each frame
  2. 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. 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);

acceleration.x = 2(newposition.x - position.x - speed.x * elapsedTime) / (elapsedTime * elapsedTime);

一旦你有你的加速,你可以设置你与code的新位置。这是简单的2D物理动力学。随着你的加速,你可以找到你的速度和你的速度,你可以找到你的下一个位置。

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.

speed.x = (float) (mass * acceleration.x * elapsed + speed.x);
speed.y = (float) (mass * acceleration.y * elapsed + speed.y);


position.x += mass * acceleration.x / 2 * elapsed * elapsed + speed.x * elapsed;
position.y += mass * acceleration.y / 2 * elapsed * elapsed + speed.y * elapsed;


speed.x *= friction;
speed.y *= friction;

质量和摩擦就会让你定义如何快速去,它会多快自行放慢。您可能必须调整的code,因为这个动态不完全是,如果你有不得不向后滚动来减缓不错。

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.

在每一帧结束时,你将有你的加速度重置为(0,0)。和上后的触摸甚至每个新帧,加速度应设置什么。它应该很好地工作:)

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 :)

这篇关于平滑滚动/惯性滚动/气势滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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