How can I repeat a property animation?

笑着哭i 提交于 2019-12-01 16:47:34

Since ViewPropertyAnimator is only good for simple animations, use more advanced ObjectAnimator class - basically method setRepeatCount and additionally setRepeatMode.

This is actually possible. Here's an example of rotating a view:

        final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000);

        animator.setListener(new android.animation.Animator.AnimatorListener() {
            ...

            @Override
            public void onAnimationEnd(final android.animation.Animator animation) {
                animation.setListener(null);
                view.setRotation(0);
                view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
            }

        });

You can also use "withEndAction" instead of a listener.

You can use CycleInterpolator. For example, like this:

    int durationMs = 60000;
    int cycleDurationMs = 1000;
    view.setAlpha(0f);
    view.animate().alpha(1f)
            .setInterpolator(new CycleInterpolator(durationMs / cycleDurationMs))
            .setDuration(durationMs)
            .start();
final ViewPropertyAnimator animator = view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000); //Animator object

    animator.setListener(new android.animation.Animator.AnimatorListener() {
        ...

        @Override
        public void onAnimationEnd(final android.animation.Animator animation) {
            animation.setListener(this); //It listens for animation's ending and we are passing this to start onAniationEnd method when animation ends, So it works in loop
            view.setRotation(0);
            view.animate().rotation(360).setInterpolator(new LinearInterpolator()).setDuration(1000).setListener(this).start();
        }

    });
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!