FileDocCategorySizeDatePackage
NoisyVelocityTracker.javaAPI DocAndroid 5.1 API4415Thu Mar 12 22:22:42 GMT 2015com.android.systemui.statusbar.phone

NoisyVelocityTracker

public class NoisyVelocityTracker extends Object implements VelocityTrackerInterface
A very simple low-pass velocity filter for motion events for noisy touch screens.

Fields Summary
private static final Pools.SynchronizedPool
sNoisyPool
private static final float
DECAY
private static final boolean
DEBUG
private final int
MAX_EVENTS
private ArrayDeque
mEventBuf
private float
mVX
private float
mVY
Constructors Summary
private NoisyVelocityTracker()

    
Methods Summary
public voidaddMovement(android.view.MotionEvent event)

        if (mEventBuf.size() == MAX_EVENTS) {
            mEventBuf.remove();
        }
        mEventBuf.add(new MotionEventCopy(event.getX(), event.getY(), event.getEventTime()));
    
public voidcomputeCurrentVelocity(int units)

        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);
        }
    
public floatgetXVelocity()

        if (Float.isNaN(mVX) || Float.isInfinite(mVX)) {
            mVX = 0;
        }
        return mVX;
    
public floatgetYVelocity()

        if (Float.isNaN(mVY) || Float.isInfinite(mVX)) {
            mVY = 0;
        }
        return mVY;
    
public static com.android.systemui.statusbar.phone.NoisyVelocityTrackerobtain()

        NoisyVelocityTracker instance = sNoisyPool.acquire();
        return (instance != null) ? instance : new NoisyVelocityTracker();
    
public voidrecycle()

        mEventBuf.clear();
        sNoisyPool.release(this);