带有视图列表的链接动画 RxJava2 Android

Chained animation with a list of views RxJava2 Android

大家好!我在使用 RxJava2 设置视图列表并逐个设置动画时遇到问题(一旦每个视图完成其动画)。

我做了很多研究,唯一接近使用 RxJava2 的方法是使用带有 Completable class 的“andThen()”运算符,这不是我想要的为.

我的主要想法是:

Observable<List<View>> observable = Observable.fromArray(listOfViews);

并发出单个 View 动画,等待动画完成并移动到下一个。

这是我要实现的目标的示例:Animation example

提前致谢!

更新

这不是我正在寻找的解决方案,但我设法使用 delay 制作了一个版本,但想法是:等待动画完成然后移动到下一个 onNext

目前我的解决方案:

List<View> listOfViews = new ArrayList<>();
listOfViews.add(buttonOne);
listOfViews.add(buttonTwo);
listOfViews.add(buttonThree);

Observable
       .fromIterable(listOfViews)
       .concatMap(view -> Observable.just(view)
               .delay(1000, TimeUnit.MILLISECONDS)
               .observeOn(AndroidSchedulers.mainThread())
               .doOnNext(item -> item.animate()
                       .scaleX(2)
                       .scaleY(2)
                       .setDuration(1000)
                       .start()))
       .toList()
       .subscribe();

如果你希望每个动画都在最后一个动画完成后开始,那么使用 Completables 是正确的解决方案,示例:

// Assuming you have a source of views to animate
Observable<View> views = Observable.just(button1, button2, button3);

// Create a Completable that completes when the animation ends
public Completable animateView(View view) {
    return Completable
             .create(emitter -> {
                  Animation animation = view.animate();
                  emitter.setCancellable(() -> animation.cancel());
                  animation
                      .scaleX(2f)
                      .scaleY(2f)
                      .setDuration(1000)
                      .withEndAction(() -> emitter.onComplete())
                      .start();
             });
}

// Execute each animation sequentially
views
    .concatMapCompletable(v -> animateView(v))
    .subscribe(...);