# Implement a spring animation

We are going to apply the spring animation to our avatar view in the Detail activity.&#x20;

First we need to set the stiffness and the damping ratio of our animation. To do so we are going to create a `SpringForce` object.

![Different level of stiffnesses](/files/-LOZ81iddwnl_WwpEZiw)

![Different levels of damping ratios](/files/-LOZ88cv9FWXhKrcKcCz)

In the `SpringForce` object we are going to set the stiffness level to STIFFNESS\_LOW and the damping ratio to DAMPING\_RATIO\_HIGH\_BOUNCY. The final position should be 0f since we don't want our view to translate at the end of the animation, but instead to return to the position it was before the animation.

Once you're done, we will create 2 `SpringAnimation` objects: one with `DynamicAnimation.TRANSLATION_X` and the other one with `DynamicAnimation.TRANSLATION_Y`. To both of them we need to apply the `SpringForce` object we just created as their spring.

Finally, we need a onTouchListener for getting coordinates of our view and for applying our animations.

{% code title="DetailActivity.kt" %}

```
var dX = 0f
var dY = 0f

root.setOnTouchListener { view, motionEvent ->
    when (motionEvent?.action) {
        MotionEvent.ACTION_DOWN -> {
            dX = motionEvent.rawX
            dY = motionEvent.rawY
        }

        MotionEvent.ACTION_MOVE -> {
            val newX = motionEvent.rawX - dX
            val newY = motionEvent.rawY - dY
            iv_avatar.animate().translationX(newX).translationY(newY).duration = 0
            springAnimationX.animateToFinalPosition(0f)
            springAnimationY.animateToFinalPosition(0f)
        }
    }
    true
}
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://elizacamber.gitbook.io/animations-2018/implement-a-spring-animation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
