Remote Compose Storybook Русский GitHub

animateRemote*AsState · Spring

A size animated by a spring.

animateRemote*AsState · Spring: the document drawn by the Remote Compose player
Open the live, interactive demo →

Change its controls, read the remote document it produces and the diff after every change.

What it is

Smoothly animates a value such as size or transparency when it changes, with a chosen speed and feel (ease, bounce, spring).

Good for

  • Expanding cards
  • Fading content in and out
  • Playful bouncy feedback

Code

The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.

import androidx.compose.remote.creation.compose.action.valueChangeAction
import androidx.compose.remote.creation.compose.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.clickable
import androidx.compose.remote.creation.compose.modifier.clip
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.shapes.RemoteRoundedCornerShape
import androidx.compose.remote.creation.compose.state.MutableRemoteBoolean
import androidx.compose.remote.creation.compose.state.animateRemoteDpAsState
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.remoteSpring
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color

@Composable
@RemoteComposable
fun AnimateDpSpring(dampingRatio: Float, stiffness: Float, small: Int, large: Int) {
    val isExpanded = remember { MutableRemoteBoolean(false) }
    val animatedSize =
        animateRemoteDpAsState(
            targetValue = isExpanded.select(large.rdp, small.rdp),
            animationSpec = remoteSpring(dampingRatio = dampingRatio, stiffness = stiffness),
        )
    RemoteBox(
        RemoteModifier.size(animatedSize)
            .clip(RemoteRoundedCornerShape(20.rdp))
            .background(Color(0xFF2E7D32).rc)
            .clickable(valueChangeAction(isExpanded, !isExpanded)),
        contentAlignment = RemoteAlignment.Center,
    ) {
        RemoteText("Tap".rs, color = Color.White.rc)
    }
}

API

creation.compose.state.animateRemoteFloatAsState

RemoteFloat animateRemoteFloatAsState(RemoteFloat targetValue, optional RemoteAnimationSpec animationSpec, optional float initialValue, optional float wrap)

creation.compose.state.animateRemoteDpAsState

RemoteDp animateRemoteDpAsState(RemoteDp targetValue, optional RemoteAnimationSpec animationSpec, optional float initialValue)

creation.compose.state.remoteTween

RemoteTweenSpec remoteTween(optional int durationMillis, optional RemoteEasing easing)

creation.compose.state.remoteSpring

RemoteAnimationSpec remoteSpring(optional float dampingRatio, optional float stiffness, optional float stopThreshold, optional int boundaryMode)

creation.compose.state.RemoteEasing

class RemoteEasing
  RemoteEasing.Companion Companion
  RemoteEasing cubic(float x1, float y1, float x2, float y2)
  RemoteEasing spline(float[] points)
  RemoteEasing Accelerate
  RemoteEasing Anticipate
  RemoteEasing Bounce
  RemoteEasing Decelerate
  RemoteEasing Elastic
  RemoteEasing Linear
  RemoteEasing Overshoot
  RemoteEasing Standard

Reference

Other stories of this component

More in Animation