1

Does anyone know how I can make an animation to repeat in ViewPropertyAnimator?

I've tried everything, but nothing works, the ViewPropertyAnimator does not accept the methods of other types of animation

user3901032
  • 121
  • 1
  • 1
  • 5

1 Answers1

0

Wrapping the ViewPropertyAnimator before you call start() will do the trick. Below I wrote a wrapper class that will repeat the animation when animation ends. You can modify the class to suit your needs.

package main.java.app.com.animation;
import android.animation.Animator;
import android.support.v4.view.ViewPropertyAnimatorListener;
import android.view.ViewPropertyAnimator;

/**
 * Created by nikola on 9/7/14.
 */
public class RepeatableViewPropertyAnimatorWrapper implements Animator.AnimatorListener {

    private static RepeatableViewPropertyAnimatorWrapper mInstance;
    private  ViewPropertyAnimator mViewPropertyAnimator;
    private Animator.AnimatorListener mViewPropertyAnimatorListener;
    private boolean willRepeat = true;
    private Animator mCurrentAnimator;

    public static RepeatableViewPropertyAnimatorWrapper wrap(ViewPropertyAnimator animator){
        mInstance = new RepeatableViewPropertyAnimatorWrapper(animator);
        return mInstance; 
    }
    public static RepeatableViewPropertyAnimatorWrapper withAnimatorListener(Animator.AnimatorListener animatorListener){
            mInstance.setAnimatorListener(animatorListener);
        return mInstance;
    }
    private void setAnimatorListener(Animator.AnimatorListener animatorListener) {
        mViewPropertyAnimatorListener = animatorListener;
    }

    public RepeatableViewPropertyAnimatorWrapper(ViewPropertyAnimator animator){
        mViewPropertyAnimator = animator;
    }
    public RepeatableViewPropertyAnimatorWrapper(ViewPropertyAnimator animator, Animator.AnimatorListener animatorListener){
        mViewPropertyAnimator = animator;
        mViewPropertyAnimatorListener = animatorListener;
    }
    public boolean isRepeating() {
        return willRepeat;
    }

    public void setRepeat(boolean willRepeat) {
        this.willRepeat = willRepeat;
    }

    private void setup(){
        mViewPropertyAnimator.setListener(this);
    }

    @Override
    public void onAnimationStart(Animator animation) {
        mCurrentAnimator = animation;
        if(mViewPropertyAnimatorListener != null){
            mViewPropertyAnimatorListener.onAnimationStart(animation);
        }
    }

    @Override
    public void onAnimationEnd(Animator animation) {
        if(mViewPropertyAnimatorListener != null){
            mViewPropertyAnimatorListener.onAnimationEnd(animation);
        }
        repeatAnimation();

    }
    public void start(){
        mViewPropertyAnimator.start();
    }

    private void repeatAnimation() {
        if(willRepeat){
            start();
            onAnimationRepeat(mCurrentAnimator);
        }
        mInstance = null;

    }

    @Override
    public void onAnimationCancel(Animator animation) {
        if(mViewPropertyAnimatorListener != null){
            mViewPropertyAnimatorListener.onAnimationCancel(animation);
        }
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        if(mViewPropertyAnimatorListener != null){
            mViewPropertyAnimatorListener.onAnimationRepeat(animation);
        }

    }

Usage:

RepeatableViewPropertyAnimatorWrapper.wrap(view.animate().alpha(0).setDuration(500)).withAnimatorListener(myListener).start();

Please note that I haven't test this code and bear in mind that depending on the ViewPropertyAnimator implementation set values may change.

Nikola Despotoski
  • 49,966
  • 15
  • 119
  • 148
  • tks so much guy, this is my code.http://stackoverflow.com/questions/25345129/how-can-i-repeat-a-property-animation – user3901032 Sep 08 '14 at 00:28