Skip to content

Running the Animations

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

In order to run specified Animation you just subscribe to the AnimationCompletable wrapping that animation:

AnimationBuilder.forView(someView)
    .alpha(0.5f)
    .duration(300L)
    .interpolator(new LinearInterpolator())
    .buildCompletable()
    .subscribe();

This will execute the specified animation and call onCompleted() once it's finished, or call onError() if something goes wrong. You can handle those callbacks in subscriber you use for subscribing (if any). This is standard RxJava stuff, no need for further explanation.

Handling animation events

One of the features on AnimationCompletable is that it enables you to define action to execute for each of the animation events:

  • AnimationCompletable#doOnAnimationReady(Consumer<View> doOnAnimationReady) - called once the animation is ready to be executed so before the animation starts
  • AnimationCompletable#doOnAnimationStart(Consumer<View> doOnAnimationReady) - called once the animation starts
  • AnimationCompletable#doOnAnimationCancel(Consumer<View> doOnAnimationReady) - called if the animation is canceled before it ends naturally
  • AnimationCompletable#doOnAnimationEnd(Consumer<View> doOnAnimationReady) - called if the animation completes successfully without interruption

Sample usage:

AnimationBuilder.forView(view)
    .alpha(0f)
    .duration(100L)
    .buildCompletable() // Creates AnimationCompletable
    .doOnAnimationStart(new Consumer<View>() {
        @Override public void accept(View view) throws Exception { // Do something }
    })
    .doOnAnimationStart(new Consumer<View>() {
        @Override public void accept(View view) throws Exception { // Do something }
    })
    .doOnAnimationCancel(new Consumer<View>() {
        @Override public void accept(View view) throws Exception { // Do something }
    })
    .doOnAnimationEnd(new Consumer<View>() {
        @Override public void accept(View view) throws Exception { // Do something }
    })
    .subscribe();