MotionEventPredictor


public interface MotionEventPredictor


There is a gap between the time a user touches the screen and that information is reported to the app; a motion predictor is a utility that provides predicted android.view.MotionEvent based on the previously received ones. Obtain a new predictor instance using newInstance; put the motion events you receive into it with record, and call predict to retrieve the predicted android.view.MotionEvent that would occur at the moment the next frame is rendered on the display.

Summary

Public methods

default static @NonNull MotionEventPredictor

Create a new motion predictor associated to a specific android.view.View.

abstract @Nullable MotionEvent

Compute a prediction

abstract void

Record a user's movement to the predictor.

Public methods

newInstance

Added in 1.0.0-beta05
default static @NonNull MotionEventPredictor newInstance(@NonNull View view)

Create a new motion predictor associated to a specific android.view.View. For devices running Android versions before U, the predicions are provided by a library based on a Kalman filter; from Android U, a system API is available, but predictions may not be supported for all strokes (for instance, it may be limited to stylus events). In these cases, the Kalman filter library will be used; to determine if a `MotionEvent` will be handled by the system prediction, use isPredictionAvailable.

Parameters
@NonNull View view

the view to associated to this predictor

Returns
@NonNull MotionEventPredictor

the new predictor instance

predict

Added in 1.0.0-beta05
abstract @Nullable MotionEvent predict()

Compute a prediction

Returns
@Nullable MotionEvent

the predicted android.view.MotionEvent, or null if not possible to make a prediction.

record

Added in 1.0.0-beta05
abstract void record(@NonNull MotionEvent event)

Record a user's movement to the predictor. You should call this for every android.view.MotionEvent that is received by the associated android.view.View.

Parameters
@NonNull MotionEvent event

the android.view.MotionEvent the associated view received and that needs to be recorded.

Throws
java.lang.IllegalArgumentException

if an inconsistent MotionEvent stream is sent.