florent37/KotlinPleaseAnimate

Execution speed is too fast, causing the animation execution sequence to change

tcqq opened this issue · 1 comments

tcqq commented

@florent37 If two different animations are executed quickly at the same time, the original execution sequence may change, resulting in is not the expected effect, I want to add a stop animation should be able to solve this problem, but I did not find the stop the animation method, can you add a stop animation method? Or how should this situation be solved?

tcqq commented

I used this method to solve the above problem, But is there an easier way to solve the above problem?

    ExpectAnim mShowAnim;
    ExpectAnim mHideAnim;
    boolean mIsShowAnim;
    boolean mIsHideAnim;

    private void setFabAnim(boolean show) {
        Observable
                .just(show)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (mHideAnim != null) {
                            if (mHideAnim.isPlaying() && show) {
                                d.dispose();
                                mIsHideAnim = true;
                                Timber.d("ShowAnim start");
                            }
                        } else if (mShowAnim != null) {
                            if (mShowAnim.isPlaying() && !show) {
                                d.dispose();
                                mIsShowAnim = true;
                                Timber.d("HideAnim start");
                            }
                        }
                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        if (aBoolean) {
                            Timber.d("Execute start anim");
                            showAnim();
                        } else {
                            Timber.d("Execute hide anim");
                            hideAnim();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.e(e.getLocalizedMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void showAnim() {
        mShowAnim = new ExpectAnim()
                .expect(mFloatingActionButton)
                .toBe(
                        scale(1, 1),
                        visible()
                )
                .toAnimation()
                .setDuration(200)
                .addStartListener(expectAnim -> mIsShowAnim = false)
                .addEndListener(expectAnim -> {
                    if (mIsShowAnim) {
                        hideAnim();
                    }
                })
                .start();
    }

    private void hideAnim() {
        mHideAnim = new ExpectAnim()
                .expect(mFloatingActionButton)
                .toBe(
                        scale(0, 0),
                        invisible()
                )
                .toAnimation()
                .setDuration(200)
                .addStartListener(expectAnim -> mIsHideAnim = false)
                .addEndListener(expectAnim -> {
                    if (mIsHideAnim) {
                        showAnim();
                    }
                })
                .start();
    }