# TargetBasedAnimation

Artifact: [androidx.compose.animation:animation-core](https://developer.android.com/jetpack/androidx/releases/compose-animation) [View Source](https://cs.android.com/search?q=file:androidx/compose/animation/core/Animation.kt+class:androidx.compose.animation.core.TargetBasedAnimation) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.0.0) Common/All
- Cmn

  ```
  class TargetBasedAnimation<T : Any?, V : AnimationVector> : Animation<T, V>
  ```

*** ** * ** ***

This is a convenient animation wrapper class that works for all target based animations, i.e. animations that has a pre-defined end value, unlike decay.

It assumes that the starting value and velocity, as well as ending value do not change throughout the animation, and cache these values. This caching enables much more convenient query for animation value and velocity (where only playtime needs to be passed into the methods).

**Note** : When interruptions happen to the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation`, a new instance should be created that use the current value and velocity as the starting conditions. This type of interruption handling is the default behavior for both `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animatable` and `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Transition`. Consider using those APIs for the interruption handling, as well as built-in animation lifecycle management.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Transition` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/rememberTransition.composable#rememberTransition(androidx.compose.animation.core.TransitionState,kotlin.String)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/updateTransition.composable#updateTransition(kotlin.Any,kotlin.String)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animatable` |   |

## Summary

| ### Public constructors |
|---|---|
| `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?, V : https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationVector> https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#TargetBasedAnimation(androidx.compose.animation.core.AnimationSpec,androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationVector)( animationSpec: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationSpec<T>, typeConverter: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TwoWayConverter<T, V>, initialValue: T, targetValue: T, initialVelocityVector: V? )` Creates a `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation` with the given start/end conditions of the animation, and the provided `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#(androidx.compose.animation.core.AnimationSpec,androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationVector)`. | Cmn |

| ### Public functions |
|---|---|---|
| `open T` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#getValueFromNanos(kotlin.Long)(playTimeNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Returns the value of the animation at the given play time. | Cmn |
| `open V` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#getVelocityVectorFromNanos(kotlin.Long)(playTimeNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)` Returns the velocity (in `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationVector` form) of the animation at the given play time. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#toString()()` | Cmn |

| ### Public properties |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#durationNanos()` This amount of time in nanoseconds that the animation will run before it finishes | Cmn |
| `T` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#initialValue()` | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#isInfinite()` Whether or not the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animation` represents an infinite animation. | Cmn |
| `open T` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#targetValue()` This is the value that the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animation` will reach when it finishes uninterrupted. | Cmn |
| `open https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TwoWayConverter<T, V>` | `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#typeConverter()` the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TwoWayConverter` that is used to convert animation type `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation` from/to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation` | Cmn |

### Inherited functions

From [androidx.compose.animation.core.Animation](https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animation) `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animation#isFinishedFromNanos(kotlin.Long)(playTimeNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`

Returns whether the animation is finished at the given play time.
Cmn

## Public constructors

### TargetBasedAnimation

Cmn

```
<T : Any?, V : AnimationVector> TargetBasedAnimation(
    animationSpec: AnimationSpec<T>,
    typeConverter: TwoWayConverter<T, V>,
    initialValue: T,
    targetValue: T,
    initialVelocityVector: V? = null
)
```

Creates a `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation` with the given start/end conditions of the animation, and the provided `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation#(androidx.compose.animation.core.AnimationSpec,androidx.compose.animation.core.TwoWayConverter,kotlin.Any,kotlin.Any,androidx.compose.animation.core.AnimationVector)`.

The resulting `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animation` assumes that the start value and velocity, as well as end value do not change throughout the animation, and cache these values. This caching enables much more convenient query for animation value and velocity (where only playtime needs to be passed into the methods).

**Note** : When interruptions happen to the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation`, a new instance should be created that use the current value and velocity as the starting conditions. This type of interruption handling is the default behavior for both `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animatable` and `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Transition`. Consider using those APIs for the interruption handling, as well as built-in animation lifecycle management.

| Parameters |
|---|---|
| `animationSpec: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationSpec<T>` | the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationSpec` that will be used to calculate value/velocity |
| `typeConverter: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TwoWayConverter<T, V>` | the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TwoWayConverter` that is used to convert animation type `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation` from/to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation` |
| `initialValue: T` | the start value of the animation |
| `targetValue: T` | the end value of the animation |
| `initialVelocityVector: V? = null` | the start velocity vector, null by default (meaning 0 velocity). |

## Public functions

### getValueFromNanos

Cmn

```
open fun getValueFromNanos(playTimeNanos: Long): T
```

Returns the value of the animation at the given play time.

| Parameters |
|---|---|
| `playTimeNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | the play time that is used to determine the value of the animation. |

### getVelocityVectorFromNanos

Cmn

```
open fun getVelocityVectorFromNanos(playTimeNanos: Long): V
```

Returns the velocity (in `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationVector` form) of the animation at the given play time.

| Parameters |
|---|---|
| `playTimeNanos: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` | the play time that is used to calculate the velocity of the animation. |

### toString

Cmn

```
open fun toString(): String
```

## Public properties

### durationNanos

Cmn

```
open val durationNanos: Long
```

This amount of time in nanoseconds that the animation will run before it finishes

### initialValue

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.0.0)

```
val initialValue: T
```

### isInfinite

Cmn

```
open val isInfinite: Boolean
```

Whether or not the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animation` represents an infinite animation. That is, one that will not finish by itself, one that needs an external action to stop. For examples, an indeterminate progress bar, which will only stop when it is removed from the composition.

### targetValue

Cmn

```
open val targetValue: T
```

This is the value that the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animation` will reach when it finishes uninterrupted.

### typeConverter

Cmn

```
open val typeConverter: TwoWayConverter<T, V>
```

the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TwoWayConverter` that is used to convert animation type `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation` from/to `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/TargetBasedAnimation`