if (NoisyVelocityTracker.DEBUG) {
Log.v("FlingTracker", "computing velocities for " + mEventBuf.size() + " events");
}
mVX = mVY = 0;
MotionEventCopy last = null;
int i = 0;
float totalweight = 0f;
float weight = 10f;
for (final Iterator<MotionEventCopy> iter = mEventBuf.iterator();
iter.hasNext();) {
final MotionEventCopy event = iter.next();
if (last != null) {
final float dt = (float) (event.t - last.t) / units;
final float dx = (event.x - last.x);
final float dy = (event.y - last.y);
if (NoisyVelocityTracker.DEBUG) {
Log.v("FlingTracker", String.format(
" [%d] (t=%d %.1f,%.1f) dx=%.1f dy=%.1f dt=%f vx=%.1f vy=%.1f",
i, event.t, event.x, event.y,
dx, dy, dt,
(dx/dt),
(dy/dt)
));
}
if (event.t == last.t) {
// Really not sure what to do with events that happened at the same time,
// so we'll skip subsequent events.
continue;
}
mVX += weight * dx / dt;
mVY += weight * dy / dt;
totalweight += weight;
weight *= DECAY;
}
last = event;
i++;
}
if (totalweight > 0) {
mVX /= totalweight;
mVY /= totalweight;
} else {
// so as not to contaminate the velocities with NaN
mVX = mVY = 0;
}
if (NoisyVelocityTracker.DEBUG) {
Log.v("FlingTracker", "computed: vx=" + mVX + " vy=" + mVY);
}