MotionPredictor
Stay organized with collections
Save and categorize content based on your preferences.
class 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
. Send all of the events that were received from the system to record
to generate complete, accurate predictions from predict
. When processing the returned predictions, make sure to consider all of the android.view.MotionEvent#getHistoricalAxisValue.
Summary
Public constructors | |
---|---|
MotionPredictor(context: Context) Create a new MotionPredictor for the provided |
Public methods | |
---|---|
Boolean |
isPredictionAvailable(deviceId: Int, source: Int) Check whether a device supports motion predictions for a given source type. |
MotionEvent? |
Get a predicted event for the gesture that has been provided to |
Unit |
record(event: MotionEvent) Record a movement so that in the future, a prediction for the current gesture can be generated. |
Public constructors
MotionPredictor
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
fun isPredictionAvailable(
deviceId: Int,
source: Int
): Boolean
Check whether a device supports motion predictions for a given source type.
Return | |
---|---|
Boolean |
True if the current device supports predictions, false otherwise. |
predict
fun predict(predictionTimeNanos: Long): MotionEvent?
Get a predicted event for the gesture that has been provided to record
. 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 android.os.SystemClock#uptimeMillis time base, but in nanoseconds. |
Return | |
---|---|
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 (android.view.MotionEvent#getX, android.view.MotionEvent#getY) coordinates for smooth prediction curves. |
record
fun record(event: MotionEvent): Unit
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 . |
Exceptions | |
---|---|
java.lang.IllegalArgumentException |
if an inconsistent MotionEvent stream is sent. |