Skip to content

Base Classes

Ivan Škorić edited this page Oct 31, 2017 · 4 revisions

Base classes are used to wrap Android animation APIs (more specifically ViewPropertyAnimator) in RxJava2 syntax and provide you with a generic way to define animations. This page goes through each of those classes and explains their role in the library.

Animation

Animation class holds parameters for a single animation. It is immutable and it's used for holding animation parameters before they're passed to the ViewPropertyAnimator once the animation needs to start. It can hold all parameters that a ViewPropertyAnimator can receive (view, translation, scale, rotation, duration, etc.)

The only way to create the Animation object is to use the AnimationBuilder class.

AnimationBuilder

AnimationBuilder does what it says - builds Animation object. The way to use it is to call AnimationBuilder.for(view) with a view that you want to animate passed in. After that you can append animation properties same as you would do for ViewPropertyAnimator. Once you're done, call .build() to create the Animation instance. Here's a sample code:

Animation a = AnimationBuilder.forView(someView)
    .alpha(0.5f)
    .duration(300L)
    .interpolator(new LinearInterpolator())
    .build();

AnimationCompletable

This is where we bring Android animations to the reactive world. AnimationCompletable is the extension of RxJava2 Completable, but adapted to running animations.

The features that it provides compared to the normal Completable is running the specified animation once subscribed to, as well as stoping the animation once disposed. On top of that, you can specify actions to be executed at a particular point in the animation, such as before, when started, when finished or when canceled.

Relations between base classes

You can create AnimationCompetable by providing the Animation to run once subscribed to:

Animation a = AnimationBuilder.forView(someView)
    .alpha(0.5f)
    .duration(300L)
    .interpolator(new LinearInterpolator())
    .build();

AnimationCompletable ac = new AnimationCompletable(a);

Animation class also has toCompletable() method which you can use to conveniently get AnimationCompletable object from Animation:

AnimationCompletable ac = AnimationBuilder.forView(someView)
    ...
    .build()
    .toCompletable();

Or even more conveniently:

AnimationCompletable ac = AnimationBuilder.forView(someView)
    ...
    .buildCompletable();

To see how to finally run animations, check the next section: Running the Animations