【问题标题】:Chained animation with a list of views RxJava2 Android带有视图列表的链式动画 RxJava2 Android
【发布时间】:2018-06-08 03:31:49
【问题描述】:

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

我做了很多研究,唯一接近使用 RxJava2 的方法是使用带有 Completable 类的“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();

【问题讨论】:

    标签: android animation rx-java android-animation rx-java2


    【解决方案1】:

    如果您希望每个动画在最后一个完成后开始,那么使用 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(...);
    

    【讨论】:

    • 感谢您的回复!我更新了我的问题,包括我附带的版本之一......但我真的需要每个 ViewPropertyAnimator 完成使用 withEndAction()
    • @MarianoZorrilla 没问题!既然我看到了您的动画实现,我已经更新了答案。我希望这会有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多