SpringForce

class SpringForce : Force
kotlin.Any
   ↳ androidx.dynamicanimation.animation.SpringForce

Spring Force defines the characteristics of the spring being used in the animation.

By configuring the stiffness and damping ratio, callers can create a spring with the look and feel suits their use case. Stiffness corresponds to the spring constant. The stiffer the spring is, the harder it is to stretch it, the faster it undergoes dampening.

Spring damping ratio describes how oscillations in a system decay after a disturbance. When damping ratio > 1* (i.e. over-damped), the object will quickly return to the rest position without overshooting. If damping ratio equals to 1 (i.e. critically damped), the object will return to equilibrium within the shortest amount of time. When damping ratio is less than 1 (i.e. under-damped), the mass tends to overshoot, and return, and overshoot again. Without any damping (i.e. damping ratio = 0), the mass will oscillate forever.

Summary

Constants

static Float

Damping ratio for a very bouncy spring.

static Float

Damping ratio for a spring with low bounciness.

static Float

Damping ratio for a medium bouncy spring.

static Float

Damping ratio for a spring with no bounciness.

static Float

Stiffness constant for extremely stiff spring.

static Float

Stiffness constant for a spring with low stiffness.

static Float

Stiffness constant for medium stiff spring.

static Float

Stiffness constant for a spring with very low stiffness.

Public constructors

Creates a spring force.

<init>(finalPosition: Float)

Creates a spring with a given final rest position.

Public methods

Float

Returns the damping ratio of the spring.

Float

Returns the rest position of the spring.

Float

Gets the stiffness of the spring.

SpringForce!
setDampingRatio(dampingRatio: Float)

Spring damping ratio describes how oscillations in a system decay after a disturbance.

SpringForce!
setFinalPosition(finalPosition: Float)

Sets the rest position of the spring.

SpringForce!
setStiffness(stiffness: Float)

Sets the stiffness of a spring.

Constants

DAMPING_RATIO_HIGH_BOUNCY

static val DAMPING_RATIO_HIGH_BOUNCY: Float

Damping ratio for a very bouncy spring. Note for under-damped springs (i.e. damping ratio < 1), the lower the damping ratio, the more bouncy the spring.

Value: 0.2f

DAMPING_RATIO_LOW_BOUNCY

static val DAMPING_RATIO_LOW_BOUNCY: Float

Damping ratio for a spring with low bounciness. Note for under-damped springs (i.e. damping ratio < 1), the lower the damping ratio, the higher the bounciness.

Value: 0.75f

DAMPING_RATIO_MEDIUM_BOUNCY

static val DAMPING_RATIO_MEDIUM_BOUNCY: Float

Damping ratio for a medium bouncy spring. This is also the default damping ratio for spring force. Note for under-damped springs (i.e. damping ratio < 1), the lower the damping ratio, the more bouncy the spring.

Value: 0.5f

DAMPING_RATIO_NO_BOUNCY

static val DAMPING_RATIO_NO_BOUNCY: Float

Damping ratio for a spring with no bounciness. This damping ratio will create a critically damped spring that returns to equilibrium within the shortest amount of time without oscillating.

Value: 1f

STIFFNESS_HIGH

static val STIFFNESS_HIGH: Float

Stiffness constant for extremely stiff spring.

Value: 10_000f

STIFFNESS_LOW

static val STIFFNESS_LOW: Float

Stiffness constant for a spring with low stiffness.

Value: 200f

STIFFNESS_MEDIUM

static val STIFFNESS_MEDIUM: Float

Stiffness constant for medium stiff spring. This is the default stiffness for spring force.

Value: 1500f

STIFFNESS_VERY_LOW

static val STIFFNESS_VERY_LOW: Float

Stiffness constant for a spring with very low stiffness.

Value: 50f

Public constructors

<init>

SpringForce()

Creates a spring force. Note that final position of the spring must be set through setFinalPosition(float) before the spring animation starts.

<init>

SpringForce(finalPosition: Float)

Creates a spring with a given final rest position.

Parameters
finalPosition Float: final position of the spring when it reaches equilibrium

Public methods

getDampingRatio

fun getDampingRatio(): Float

Returns the damping ratio of the spring.

Return
Float: damping ratio of the spring

getFinalPosition

fun getFinalPosition(): Float

Returns the rest position of the spring.

Return
Float: rest position of the spring

getStiffness

fun getStiffness(): Float

Gets the stiffness of the spring.

Return
Float: the stiffness of the spring

setDampingRatio

fun setDampingRatio(dampingRatio: Float): SpringForce!

Spring damping ratio describes how oscillations in a system decay after a disturbance.

When damping ratio > 1 (over-damped), the object will quickly return to the rest position without overshooting. If damping ratio equals to 1 (i.e. critically damped), the object will return to equilibrium within the shortest amount of time. When damping ratio is less than 1 (i.e. under-damped), the mass tends to overshoot, and return, and overshoot again. Without any damping (i.e. damping ratio = 0), the mass will oscillate forever.

Default damping ratio is DAMPING_RATIO_MEDIUM_BOUNCY.

Parameters
dampingRatio Float: damping ratio of the spring, it should be non-negative
Return
SpringForce!: the spring force that the given damping ratio is set on
Exceptions
IllegalArgumentException if the {@param dampingRatio} is negative.

setFinalPosition

fun setFinalPosition(finalPosition: Float): SpringForce!

Sets the rest position of the spring.

Parameters
finalPosition Float: rest position of the spring
Return
SpringForce!: the spring force that the given final position is set on

setStiffness

fun setStiffness(stiffness: Float): SpringForce!

Sets the stiffness of a spring. The more stiff a spring is, the more force it applies to the object attached when the spring is not at the final position. Default stiffness is STIFFNESS_MEDIUM.

Parameters
stiffness Float: non-negative stiffness constant of a spring
Return
SpringForce!: the spring force that the given stiffness is set on
Exceptions
IllegalArgumentException if the given spring stiffness is not positive