Emit objects for drawing in the UI in a regular interval using RxJava on Android

前端 未结 1 386
-上瘾入骨i
-上瘾入骨i 2021-01-16 17:47

I am parsing SVG files in an Observable. Points are emitted as \"Path\" object as the XML is parsed. Parsing happens in a separate thread and I want to draw the SVG file poi

相关标签:
1条回答
  • 2021-01-16 17:56

    You need to use the .zip() operator with a .timer(). From original RxJava wiki:

    • zip():

      combine Observables together via a specified function and emit items based on the results of this function

    • timer():

      create an Observable that emits a single item after a given delay

    So, if you use zip() to combine your original Observer with timer(), you can delay the output of each Path every 50 msecs:

    private void drawPath(final String chars) {
        Observable.zip(
            Observable.create(new Observable.OnSubscribe<Path>() {
                // all the drawing stuff here
                ...
            }),
            Observable.timer(0, 50, TimeUnit.MILLISECONDS),
            new Func2<Path, Long, Path>() {
                @Override
                public Path call(Path path, Long aLong) {
                    return path;
                }
            }
        )
        .subscribeOn(Schedulers.newThread())
        .observeOn(AndroidSchedulers.mainThread())
        ...
    }
    
    0 讨论(0)
提交回复
热议问题