How do I do something when an animation finishes?

前端 未结 4 1034
攒了一身酷
攒了一身酷 2020-12-01 08:51

I have an ImageView that I use to show progress via an AnimationDrawable. When I want to show my progress spinner, I do this:

anim         


        
相关标签:
4条回答
  • 2020-12-01 09:28

    With androidx you can use doOnEnd method which invoked when the animation has ended

    val anim = ObjectAnimator.ofFloat(eva_image, View.TRANSLATION_Y, 0f, 500f)
    anim.setDuration(500)
    anim.doOnEnd { Toast.makeText(this, "Anim End", Toast.LENGTH_SHORT).show() }
    anim.start()
    
    0 讨论(0)
  • 2020-12-01 09:31

    To your original question about the ObjectAnimator object you can set up an Animator.AnimatorListener object which defines several animation state callbacks. You want to override public void onAnimationEnd(Animator animation)

    animation.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animation) {
    
                    }
    
                    @Override
                    public void onAnimationEnd(Animator animation) {
                        Toast.makeText(VideoEditorActivity.this, "animation ended", Toast.LENGTH_LONG).show();
                    }
    
                    @Override
                    public void onAnimationCancel(Animator animation) {
    
                    }
    
                    @Override
                    public void onAnimationRepeat(Animator animation) {
    
                    }
                });
    
    0 讨论(0)
  • 2020-12-01 09:33

    You could also look into postOnAnimation(Runnable)

    Link to Docs: postOnAnimation(java.lang.Runnable)

    0 讨论(0)
  • 2020-12-01 09:34

    The more modern way of doing this is to use the ViewPropertyAnimator:

    view.animate()
        .alpha(0f)
        .withEndAction(new Runnable() {
          @Override
          public void run() {
            // Do something.
          }
        })
        .start();
    

    Or, if you're using RetroLambda:

    view.animate()
        .alpha(0f)
        .withEndAction(() -> {
          // Do something.
        })
        .start();
    
    0 讨论(0)
提交回复
热议问题