MotionPredictor


public final class MotionPredictor
extends Object

java.lang.Object
   ↳ android.view.MotionPredictor


Calculate motion predictions. Feed motion events to this class in order to generate predicted future events. The prediction functionality may not be available on all devices: check if a specific source is supported on a given input device using isPredictionAvailable(int, int). Send all of the events that were received from the system to record(MotionEvent) to generate complete, accurate predictions from predict(long). When processing the returned predictions, make sure to consider all of the historical samples.

Summary

Public constructors

MotionPredictor(Context context)

Create a new MotionPredictor for the provided Context.

Public methods

boolean isPredictionAvailable(int deviceId, int source)

Check whether a device supports motion predictions for a given source type.

MotionEvent predict(long predictionTimeNanos)

Get a predicted event for the gesture that has been provided to record(MotionEvent).

void record(MotionEvent event)

Record a movement so that in the future, a prediction for the current gesture can be generated.

Inherited methods

Public constructors

MotionPredictor

Added in API level 34
public MotionPredictor (Context context)

Create a new MotionPredictor for the provided Context.

Parameters
context Context: The context for the predictions This value cannot be null.

Public methods

isPredictionAvailable

Added in API level 34
public boolean isPredictionAvailable (int deviceId, 
                int source)

Check whether a device supports motion predictions for a given source type.

Parameters
deviceId int: The input device id.

source int: The source of input events.

Returns
boolean True if the current device supports predictions, false otherwise.

predict

Added in API level 34
public MotionEvent predict (long predictionTimeNanos)

Get a predicted event for the gesture that has been provided to record(MotionEvent). Predictions may not reach the requested timestamp if the confidence in the prediction results is low.

Parameters
predictionTimeNanos long: The time that the prediction should target, in the SystemClock.uptimeMillis() time base, but in nanoseconds.

Returns
MotionEvent The predicted motion event, or `null` if predictions are not supported, or not possible for the current gesture. Be sure to check the historical data in addition to the latest (getX(), getY()) coordinates for smooth prediction curves.

record

Added in API level 34
public void record (MotionEvent event)

Record a movement so that in the future, a prediction for the current gesture can be generated. Only gestures from one input device at a time should be provided to an instance of MotionPredictor.

Parameters
event MotionEvent: The received event This value cannot be null.

Throws
IllegalArgumentException if an inconsistent MotionEvent stream is sent.