检查AnimatorSet是否已完成animation?

我正在尝试使用AnimatorSetanimation淡入淡出按钮

Button fades in > Click button > Remaining buttons fade out

所以为了做到这一点,我想onClickListner after the animation is completed设置onClickListner after the animation is completed ,但这似乎不起作用。 单击animation中间的按钮triggers the onClick action

 setQuestion = new AnimatorSet(); setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5); setQuestion.start(); 

这是检查if the animation has finished

 private void checkAnimation() { while (true) { // Check if animation has ended if (setQuestion.isRunning() == false) { assignListners(); break; } } } 

  • 使用单个ObjectAnimator更改多个属性?
  • 您可以在fadeinAnimation5上设置AnimatorListener 。 这将为您提供onAnimationEnd回调。

     fadeinAnimation5.addListener(new AnimatorListener() { @Override public void onAnimationStart(Animator animation) { // ... } @Override public void onAnimationRepeat(Animator animation) { // ... } @Override public void onAnimationEnd(Animator animation) { // ... } @Override public void onAnimationCancel(Animator animation) { // ... } }); 

    或者,正如slott所建议的那样使用AnimatorListenerAdapter

     fadeinAnimation5.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { // ... } } 

    我遇到了类似的问题,这就是我如何解决它:

     private void crossFadeAnimation(final View fadeInTarget, final View fadeOutTarget, long duration){ AnimatorSet mAnimationSet = new AnimatorSet(); ObjectAnimator fadeOut = ObjectAnimator.ofFloat(fadeOutTarget, View.ALPHA, 1f, 0f); fadeOut.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { } @Override public void onAnimationEnd(Animator animation) { fadeOutTarget.setVisibility(View.GONE); } @Override public void onAnimationCancel(Animator animation) { } @Override public void onAnimationRepeat(Animator animation) { } }); fadeOut.setInterpolator(new LinearInterpolator()); ObjectAnimator fadeIn = ObjectAnimator.ofFloat(fadeInTarget, View.ALPHA, 0f, 1f); fadeIn.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) { fadeInTarget.setVisibility(View.VISIBLE); } @Override public void onAnimationEnd(Animator animation) {} @Override public void onAnimationCancel(Animator animation) {} @Override public void onAnimationRepeat(Animator animation) {} }); fadeIn.setInterpolator(new LinearInterpolator()); mAnimationSet.setDuration(duration); mAnimationSet.playTogether(fadeOut, fadeIn); mAnimationSet.start(); }