FileDocCategorySizeDatePackage
VelocityTracker.javaAPI DocAndroid 5.1 API10692Thu Mar 12 22:22:10 GMT 2015android.view

VelocityTracker

public final class VelocityTracker extends Object
Helper for tracking the velocity of touch events, for implementing flinging and other such gestures. Use {@link #obtain} to retrieve a new instance of the class when you are going to begin tracking. Put the motion events you receive into it with {@link #addMovement(MotionEvent)}. When you want to determine the velocity call {@link #computeCurrentVelocity(int)} and then call {@link #getXVelocity(int)} and {@link #getYVelocity(int)} to retrieve the velocity for each pointer id.

Fields Summary
private static final android.util.Pools.SynchronizedPool
sPool
private static final int
ACTIVE_POINTER_ID
private long
mPtr
private final String
mStrategy
Constructors Summary
private VelocityTracker(String strategy)

        mPtr = nativeInitialize(strategy);
        mStrategy = strategy;
    
Methods Summary
public voidaddMovement(MotionEvent event)
Add a user's movement to the tracker. You should call this for the initial {@link MotionEvent#ACTION_DOWN}, the following {@link MotionEvent#ACTION_MOVE} events that you receive, and the final {@link MotionEvent#ACTION_UP}. You can, however, call this for whichever events you desire.

param
event The MotionEvent you received and would like to track.

        if (event == null) {
            throw new IllegalArgumentException("event must not be null");
        }
        nativeAddMovement(mPtr, event);
    
public voidclear()
Reset the velocity tracker back to its initial state.

        nativeClear(mPtr);
    
public voidcomputeCurrentVelocity(int units)
Equivalent to invoking {@link #computeCurrentVelocity(int, float)} with a maximum velocity of Float.MAX_VALUE.

see
#computeCurrentVelocity(int, float)

        nativeComputeCurrentVelocity(mPtr, units, Float.MAX_VALUE);
    
public voidcomputeCurrentVelocity(int units, float maxVelocity)
Compute the current velocity based on the points that have been collected. Only call this when you actually want to retrieve velocity information, as it is relatively expensive. You can then retrieve the velocity with {@link #getXVelocity()} and {@link #getYVelocity()}.

param
units The units you would like the velocity in. A value of 1 provides pixels per millisecond, 1000 provides pixels per second, etc.
param
maxVelocity The maximum velocity that can be computed by this method. This value must be declared in the same unit as the units parameter. This value must be positive.

        nativeComputeCurrentVelocity(mPtr, units, maxVelocity);
    
protected voidfinalize()

        try {
            if (mPtr != 0) {
                nativeDispose(mPtr);
                mPtr = 0;
            }
        } finally {
            super.finalize();
        }
    
public booleangetEstimator(int id, android.view.VelocityTracker$Estimator outEstimator)
Get an estimator for the movements of a pointer using past movements of the pointer to predict future movements. It is not necessary to call {@link #computeCurrentVelocity(int)} before calling this method.

param
id Which pointer's velocity to return.
param
outEstimator The estimator to populate.
return
True if an estimator was obtained, false if there is no information available about the pointer.
hide
For internal use only. Not a final API.

        if (outEstimator == null) {
            throw new IllegalArgumentException("outEstimator must not be null");
        }
        return nativeGetEstimator(mPtr, id, outEstimator);
    
public floatgetXVelocity()
Retrieve the last computed X velocity. You must first call {@link #computeCurrentVelocity(int)} before calling this function.

return
The previously computed X velocity.

        return nativeGetXVelocity(mPtr, ACTIVE_POINTER_ID);
    
public floatgetXVelocity(int id)
Retrieve the last computed X velocity. You must first call {@link #computeCurrentVelocity(int)} before calling this function.

param
id Which pointer's velocity to return.
return
The previously computed X velocity.

        return nativeGetXVelocity(mPtr, id);
    
public floatgetYVelocity()
Retrieve the last computed Y velocity. You must first call {@link #computeCurrentVelocity(int)} before calling this function.

return
The previously computed Y velocity.

        return nativeGetYVelocity(mPtr, ACTIVE_POINTER_ID);
    
public floatgetYVelocity(int id)
Retrieve the last computed Y velocity. You must first call {@link #computeCurrentVelocity(int)} before calling this function.

param
id Which pointer's velocity to return.
return
The previously computed Y velocity.

        return nativeGetYVelocity(mPtr, id);
    
private static native voidnativeAddMovement(long ptr, MotionEvent event)

private static native voidnativeClear(long ptr)

private static native voidnativeComputeCurrentVelocity(long ptr, int units, float maxVelocity)

private static native voidnativeDispose(long ptr)

private static native booleannativeGetEstimator(long ptr, int id, android.view.VelocityTracker$Estimator outEstimator)

private static native floatnativeGetXVelocity(long ptr, int id)

private static native floatnativeGetYVelocity(long ptr, int id)

private static native longnativeInitialize(java.lang.String strategy)

public static android.view.VelocityTrackerobtain(java.lang.String strategy)
Obtains a velocity tracker with the specified strategy. For testing and comparison purposes only.

param
strategy The strategy, or null to use the default.
return
The velocity tracker.
hide

        if (strategy == null) {
            return obtain();
        }
        return new VelocityTracker(strategy);
    
public static android.view.VelocityTrackerobtain()
Retrieve a new VelocityTracker object to watch the velocity of a motion. Be sure to call {@link #recycle} when done. You should generally only maintain an active object while tracking a movement, so that the VelocityTracker can be re-used elsewhere.

return
Returns a new VelocityTracker.


         
         
         
           
             
           
           
             

                                                        
        
        VelocityTracker instance = sPool.acquire();
        return (instance != null) ? instance : new VelocityTracker(null);
    
public voidrecycle()
Return a VelocityTracker object back to be re-used by others. You must not touch the object after calling this function.

        if (mStrategy == null) {
            clear();
            sPool.release(this);
        }