# MotionEventPredictor

Artifact: [androidx.input:input-motionprediction](https://developer.android.com/jetpack/androidx/releases/input) [View Source](https://cs.android.com/search?q=file:androidx/input/motionprediction/MotionEventPredictor.java+class:androidx.input.motionprediction.MotionEventPredictor) Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

*** ** * ** ***


```
public interface MotionEventPredictor
```

<br />

*** ** * ** ***

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 `https://developer.android.com/reference/android/view/MotionEvent.html` based on the previously received ones. Obtain a new predictor instance using `https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor#newInstance(android.view.View)`; put the motion events you receive into it with `https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor#record(android.view.MotionEvent)`, and call `https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor#predict()` to retrieve the predicted `https://developer.android.com/reference/android/view/MotionEvent.html` that would occur at the moment the next frame is rendered on the display.

## Summary

| ### Public methods |
|---|---|
| `default static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor` | `https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor#newInstance(android.view.View)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html view)` Create a new motion predictor associated to a specific `https://developer.android.com/reference/android/view/View.html`. |
| `abstract @https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/view/MotionEvent.html` | `https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor#predict()()` Compute a prediction |
| `abstract void` | `https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor#record(android.view.MotionEvent)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/MotionEvent.html event)` Record a user's movement to the predictor. |

## Public methods

### newInstance

Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

```
default static @NonNull MotionEventPredictor newInstance(@NonNull View view)
```

Create a new motion predictor associated to a specific `https://developer.android.com/reference/android/view/View.html`. 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 `https://developer.android.com/reference/android/view/MotionPredictor.html#isPredictionAvailable(int, int)`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/View.html view` | the view to associated to this predictor |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/input/motionprediction/MotionEventPredictor` | the new predictor instance |

### predict

Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

```
abstract @Nullable MotionEvent predict()
```

Compute a prediction

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/Nullable https://developer.android.com/reference/android/view/MotionEvent.html` | the predicted `https://developer.android.com/reference/android/view/MotionEvent.html`, or null if not possible to make a prediction. |

### record

Added in [1.0.1](https://developer.android.com/jetpack/androidx/releases/input#1.0.1)

```
abstract void record(@NonNull MotionEvent event)
```

Record a user's movement to the predictor. You should call this for every `https://developer.android.com/reference/android/view/MotionEvent.html` that is received by the associated `https://developer.android.com/reference/android/view/View.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/view/MotionEvent.html event` | the `https://developer.android.com/reference/android/view/MotionEvent.html` the associated view received and that needs to be recorded. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | if an inconsistent MotionEvent stream is sent. |