Animations in Android
  • Introduction
  • About us
  • Basic animations
  • ConstraintLayout: Core concepts
  • I like to move it, move it!
  • Step 1
  • Step 2
  • Step 2: Solution
  • Step 3
  • Step 3: Solution
  • Step 4
  • Step 4: Solution
  • Step 5
  • Step 5: Solution
  • Step 6
  • Step 7
  • Step 7: Solution
  • Step 8
  • Step 8: Solution
  • MotionLayout
  • Party time!
  • Shared element transition
  • Adding a RecyclerView
  • Activity transitions
  • Adding a second Activity
  • Adding the shared element transition
  • You made it!
  • VectorDrawable
  • VectorDrawable: Solution
  • Using the VectorDrawable
  • RecyclerView item animation & self-contained MotionScene
  • Creating a self-contained MotionScene
  • Creating a self-contained MotionScene : Solution
  • Adding animations in the RecyclerView items: Step 1
  • Adding animations in the RecyclerView items: Step 2
  • RecyclerView animations step 2: Solution
  • Physic based animations
  • Implement a spring animation
  • Spring animation: Solution
  • The finish line!
Powered by GitBook
On this page
  • The Animation class
  • When to use
  • When NOT to use
  • Property animation API
  • When NOT to use
  • Creating an animation
  • ViewPropertyAnimator.class

Basic animations

PreviousAbout usNextConstraintLayout: Core concepts

Last updated 6 years ago

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

The Animation class

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.

When animating properties there are 2 steps: calculating the animated values and setting those values on the object and property in question. ValueAnimatortakes care of the first part; calculating the values, while the ObjectAnimatorclass, is responsible for setting those values on target objects.

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*

Creating an animation

All animations can be either from XML or programmatically.

Example ObjectAnimator:

...
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(btn, View.ROTATION, 360);
objectAnimator.setDuration(5000);
objectAnimator.setRepeatCount(1);
objectAnimator.setRepeatMode(ValueAnimator.REVERSE);
objectAnimator.start();
...
<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" />

ViewPropertyAnimator.class

Property animation API provides also the ViewPropertyAnimator class that is a simple way to animate several properties in parallel, using a single Animator.

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!

*If you want to find more information check this .

article