> For the complete documentation index, see [llms.txt](https://elizacamber.gitbook.io/animations-2018/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://elizacamber.gitbook.io/animations-2018/basic-animations.md).

# Basic animations

You can't learn how to run if you don't start walking first.

![](/files/-LO-oGyQTyzEuxQ1eCuA)

## The Animation class&#x20;

#### **android.view\.animation**

The Animation class has 5 known subclasses

* AlphaAnimation (will fade in or out your view)
* RotateAnimation (will rotate your view)
* ScaleAnimation (will scale up or down your view)
* TranslateAnimation (will move your view)
* AnimationSet (for a combination of animations that should be played together on the **same** view)

### When to use

When you want to animate one property of a view

### When **NOT** to use

1. When you want to animate more than one properties or
2. you want to animate a clickable view

## **Property animation API**

**android.animation**

It makes it easy to animate any kind of property on any object by allowing you to define a start and end value and apply a time-based change on this attribute.

The Animator class is the superclass for classes which provide basic support for animations which can be started, ended, and have AnimatorListeners attached to them. These classes are:

* Animator set
* ValueAnimator
* ObjectAnimator
* TimeAnimator

**ValueAnimator:** provides a simple timing engine for running animations which calculate animated values. It runs in a custom handler to ensure that property changes happen on the UI thread.

**ObjectAnimator:**  sets the animated values to the target objects.

**TimeAnimator**: provides a simple callback mechanism to listeners that is synchronized with all other animators in the system

**AnimatorSet**: This class plays a set of Animator objects in the specified order more or less as the AnimationSet class we saw earlier. That said, the animator set is more flexible and animations can be set up to play together, in sequence, or after a specified delay. In addition you can set it to play different animations on different objects and not just on the same one.

{% hint style="info" %}
When animating properties there are 2 steps: calculating the animated values and setting those values on the object and property in question. `ValueAnimator`takes care of the first part; calculating the values, while the `ObjectAnimator`class, is responsible for setting those values on target objects.
{% endhint %}

### When **NOT** to use

1. I found only one case of a simple animation that this did not work, when I wanted to animate only one layer of a `LayerDrawable`\*

\*If you want to find more information check this [article](https://medium.com/@elizacamber/animating-a-single-item-from-a-layer-list-97a8090b3539).

## Creating an animation

All animations can be either from XML or programmatically.

Example `ObjectAnimator`:

{% tabs %}
{% tab title="SomeActivity.java" %}

```
...
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, View.ROTATION, 360);
objectAnimator.setDuration(5000);
objectAnimator.setRepeatCount(1);
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.start();
...

```

{% endtab %}

{% tab title="object\_animator.xml" %}

```
<objectAnimator
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="1000"
   android:valueFrom="0"
   android:valueTo="360"
   android:valueType="floatType"
   android:propertyName="rotation"
   android:repeatCount="1"
   android:repeatMode="reverse" />
```

{% endtab %}
{% endtabs %}

## **ViewPropertyAnimator.class**

Property animation API provides also the ViewPropertyAnimator class that is a simple way to animate several properties in parallel, using a single Animato&#x72;**.**

```
ViewPropertyAnimator propertyAnimator = 
btn.animate().rotation(360).setDuration(5000);
propertyAnimator.alpha(0f).setDuration(5000);
```

**The good:** it provides better performance for several simultaneous animations, because it will optimize invalidate calls to take place only once for several properties instead of each animated property independently causing its own invalidation. It also has a much simpler syntax!

**The bad:** it’s only for animating properties in parallel!
