大家!我在RxJava2上遇到问题,无法设置视图列表并一个接一个地设置动画(一旦每个视图完成其动画处理)。
我做了很多研究,使用RxJava2唯一接近的是将andThen()
运算符与Completable
类一起使用,这不是我想要的。
我的主要想法是:
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(...);