Specifying the number of repeats / mode in ViewPropertyAnimator?

Is there a way to specify the number of repeats / mode when using ViewPropertyAnimator?

+5
source share
1 answer

Unfortunately, ViewPropertyAnimatorthere is no way to specify the number of repetitions of the animation that it performs. As stated in javadocs for the method startAnimation()in ViewPropertyAnimator:

Launches the base animator for a set of properties. We use one animator that simply starts from 0 to 1, and then uses this fractional value to set each property value appropriately.

, , - , ViewPropertyAnimator.

AnimatorListener ViewPropertyAnimator, :

viewPropertyAnimator.setListener(new Animator.AnimatorListener() {

    ...

    @Override
    public void onAnimationEnd(Animator animation) {
        viewPropertyAnimator.start();
    }

    ...

});
viewPropertyAnimator.start();

, :

int[] repeatCount = {8}; //your repeat count goes here

viewPropertyAnimator.setListener(new Animator.AnimatorListener() {

    ...

    @Override
    public void onAnimationEnd(Animator animation) {
        if(repeatCount-- > 0)
            viewPropertyAnimator.start();
    }

    ...

});
viewPropertyAnimator.start();

, , ViewPropertyAnimator ( ), ViewPropertyAnimator . , , . :

public void animateEndlessly(final View v) {
        ViewPropertyAnimator viewPropertyAnimator = v.animate().scaleX(1.5f).scaleY(1.5f).setDuration(300);
        viewPropertyAnimator.setListener(new Animator.AnimatorListener() {

            ...

            @Override
            public void onAnimationEnd(Animator animation) {
                animateEndlessly(v);
            }

            ...

        });
        viewPropertyAnimator.start();
    }
+3

All Articles