# animateFloatAsState

Common/All

## Functions summary

|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html>` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable https://developer.android.com/reference/kotlin/androidx/compose/animation/core/animateFloatAsState.composable#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)( targetValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, animationSpec: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationSpec<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html>, visibilityThreshold: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html, label: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, finishedListener: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? )` Fire-and-forget animation function for `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html`. | Cmn |

## Functions

### animateFloatAsState

Cmn 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/AnimateAsState.kt+function:animateFloatAsState) Added in [1.3.0](https://developer.android.com/jetpack/androidx/releases/compose-animation#1.3.0)

```
@Composable
fun animateFloatAsState(
    targetValue: Float,
    animationSpec: AnimationSpec<Float> = defaultAnimation,
    visibilityThreshold: Float = DefaultFloatVisibilityThreshold,
    label: String = "FloatAnimation",
    finishedListener: ((Float) -> Unit)? = null
): State<Float>
```

Fire-and-forget animation function for `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html`. This Composable function is overloaded for different parameter types such as `https://developer.android.com/reference/kotlin/androidx/compose/ui/unit/Dp`, `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/Color`, `https://developer.android.com/reference/kotlin/androidx/compose/ui/geometry/Offset`, etc. When the provided `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` is changed, the animation will run automatically. If there is already an animation in-flight when `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` changes, the on-going animation will adjust course to animate towards the new target value.

`https://developer.android.com/reference/kotlin/androidx/compose/animation/core/animateFloatAsState.composable#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` returns a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` object. The value of the state object will continuously be updated by the animation until the animation finishes.

Note, `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/animateFloatAsState.composable#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` cannot be canceled/stopped without removing this composable function from the tree. See `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/Animatable` for cancelable animations.

`https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` can be used to define when the animation value is considered close enough to the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` to finish. By default, the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` in the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` will be respected. If a non-default `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` is provided, it will override the visibility threshold in the `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#animateFloatAsState(kotlin.Float,androidx.compose.animation.core.AnimationSpec,kotlin.Float,kotlin.String,kotlin.Function1)` if it's a `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/SpringSpec`.

```kotlin
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer

@Composable
fun alphaAnimation(visible: Boolean) {
    // Animates to 1f or 0f based on [visible].
    // This [animateState] returns a State<Float> object. The value of the State object is
    // being updated by animation. (This method is overloaded for different parameter types.)
    // Here we use the returned [State] object as a property delegate.
    val alpha: Float by animateFloatAsState(if (visible) 1f else 0f)

    // Updates the alpha of a graphics layer with the float animation value. It is more
    // performant to modify alpha in a graphics layer than using `Modifier.alpha`. The former
    // limits the invalidation scope of alpha change to graphicsLayer's draw stage (i.e. no
    // recomposition would be needed). The latter triggers recomposition on each animation
    // frame.
    Box(modifier = Modifier.graphicsLayer { this.alpha = alpha }.background(Color.Red))
}
```

| Parameters |
|---|---|
| `targetValue: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html` | Target value of the animation |
| `animationSpec: https://developer.android.com/reference/kotlin/androidx/compose/animation/core/AnimationSpec<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html> = defaultAnimation` | The animation that will be used to change the value through time. `https://developer.android.com/reference/kotlin/androidx/compose/animation/core/package-summary#spring(kotlin.Float,kotlin.Float,kotlin.Any)` will be used by default. |
| `visibilityThreshold: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html = DefaultFloatVisibilityThreshold` | An optional threshold for deciding when the animation value is considered close enough to the targetValue. |
| `label: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html = "FloatAnimation"` | An optional label to differentiate from other animations in Android Studio. |
| `finishedListener: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? = null` | An optional end listener to get notified when the animation is finished. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html>` | A `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State` object, the value of which is updated by animation. |