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
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
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.