ozodrukh / CircularReveal

Lollipop ViewAnimationUtils.createCircularReveal for everyone 4.0+
MIT License
2.43k stars 391 forks source link

Can't add Animation Listener #8

Closed sheharyarn closed 9 years ago

sheharyarn commented 9 years ago

I'm unable to add an AnimatorListener to an instance of your SupportAnimator. On a regular Circular Reveal Animation, I just do this:

Animator reveal = ViewAnimationUtils.createCircularReveal(view, cX, cY, 0, radius);
reveal.setInterpolator(new AccelerateInterpolator(2.0f));
reveal.setDuration(500);
reveal.addListener(new Animator.AnimatorListener() {
    public void onAnimationStart(Animator animation) {}
    public void onAnimationCancel(Animator animation) {}
    public void onAnimationRepeat(Animator animation) {}
    public void onAnimationEnd(Animator animation) {
        // DO SOME STUFF HERE
    }
});
reveal.start();

There doesn't seem to be a setListener() or addListener() or anything similar.

ozodrukh commented 9 years ago

You can add listener through:

SupportAnimator reveal = ViewAnimationUtils.createCircularReveal(view, cX, cY, 0, radius);
if(reveal.isNativeAnimator()){
     ObjectAnimator a = (ObjectAnimator) reveal.get();
     //TODO add listener to android.animation.ObjectAnimator
}else{
     ObjectAnimator a = (ObjectAnimator) reveal.get();
     //TODO add listener to nineoldandroids
}
sheharyarn commented 9 years ago

It seems to be working on Pre-lollipop devices but doesn't work on Lollipop. I get this error:

java.lang.ClassCastException: android.animation.RevealAnimator cannot be cast to android.animation.ObjectAnimator

This is my code:

if (reveal.isNativeAnimator()) {
    android.animation.ObjectAnimator a = (android.animation.ObjectAnimator) reveal.get();
    a.addListener(new Animator.AnimatorListener() {
        public void onAnimationCancel (Animator animation) { }
        public void onAnimationRepeat (Animator animation) { }
        public void onAnimationStart  (Animator animation) { commonOnStartStuff(); }
        public void onAnimationEnd    (Animator animation) { commonOnEndStuff();   }
    });
} else {
    com.nineoldandroids.animation.ObjectAnimator a = (com.nineoldandroids.animation.ObjectAnimator) reveal.get();
    a.addListener(new com.nineoldandroids.animation.Animator.AnimatorListener() {
        public void onAnimationCancel (com.nineoldandroids.animation.Animator animation) { }
        public void onAnimationRepeat (com.nineoldandroids.animation.Animator animation) { }
        public void onAnimationStart  (com.nineoldandroids.animation.Animator animation) { commonOnStartStuff(); }
        public void onAnimationEnd    (com.nineoldandroids.animation.Animator animation) { commonOnEndStuff();   }
    });
}
sheharyarn commented 9 years ago

Okay, with a little messing around, I was able to fix it. I changed ObjectAnimator to Animator. This is my code:

if (reveal.isNativeAnimator()) {
    Animator a = (Animator) reveal.get();
    a.addListener(new Animator.AnimatorListener() {
        // ...
    });
} else {
    com.nineoldandroids.animation.ObjectAnimator a = (com.nineoldandroids.animation.ObjectAnimator) reveal.get();
    a.addListener(new com.nineoldandroids.animation.Animator.AnimatorListener() {
        // ...
    });
}

I don't think that I should close this issue yet, I think this should be implemented within the library. Anyways, awesome lib and thanks for the quick reply!

ozodrukh commented 9 years ago

Thanks, have you an idea to solve this problem?

sheharyarn commented 9 years ago

Yeah, a custom implementation of `AnimatorListerner' that does this?