51

OnTouch of an ImageView I'm starting a fade in animation:

    myImageView.setOnTouchListener(new View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {
    v.startAnimation(fadeInAnimation);

I know it's need an animation listener to find out when the animation is complete but how do I attach this so that I can get the view that the animation has just completed on... I want to set the visibility of the view after the animation is done.

Thanks

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
beans
  • 1,765
  • 5
  • 25
  • 31

6 Answers6

133

I think you need this.

fadeInAnimation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {

    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});
MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
peanut
  • 1,570
  • 1
  • 12
  • 9
  • If you only need to use a subset of the methods, you can use [AnimatorListenerAdapter](https://developer.android.com/reference/android/animation/AnimatorListenerAdapter) and then only override the ones you need (e.g. onAnimationEnd()) – user2891659 Jun 17 '19 at 15:41
26

If you only need an end-action it would suffice to use .withEndAction(Runnable)

fadeInAnimation.withEndAction(new Runnable() {
    @Override
    public void run() {
        ... do stuff
    }
})
jonas
  • 984
  • 10
  • 15
7

In case someone needs the solution in kotlin:

fadeInAnimation.setAnimationListener(object: Animation.AnimationListener {
        override fun onAnimationRepeat(animation: Animation?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onAnimationEnd(animation: Animation?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

        override fun onAnimationStart(animation: Animation?) {
            TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        }

    })
Noelia
  • 3,698
  • 4
  • 17
  • 22
4

Using Kotlin

        //OR using Code
        val rotateAnimation = RotateAnimation(
                0f, 359f,
                Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f

        )
        rotateAnimation.duration = 300
        rotateAnimation.repeatCount = 2

        //Either way you can add Listener like this
        rotateAnimation.setAnimationListener(object : Animation.AnimationListener {

            override fun onAnimationStart(animation: Animation?) {
            }

            override fun onAnimationRepeat(animation: Animation?) {
            }

            override fun onAnimationEnd(animation: Animation?) {

                val rand = Random()
                val ballHit = rand.nextInt(50) + 1
                Toast.makeText(context, "ballHit : " + ballHit, Toast.LENGTH_SHORT).show()
            }
        })

        ivBall.startAnimation(rotateAnimation)
Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
  • hello, I have a question about your code, i used it for my question - can you maybe take a look at it: https://stackoverflow.com/questions/59253778/update-attributes-during-rotation-animation – KayD Dec 09 '19 at 18:11
3

My function setAnimation

private Animation animateRoationLayout(Animation.AnimationListener animationListener) {
    Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.level_expand_rotation);
    anim.setInterpolator(new LinearInterpolator()); // for smooth animation
    anim.setAnimationListener(animationListener);
    return anim;
}

Define Animation Listener :

final Animation.AnimationListener animationListener =new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            Toast.makeText(getActivity(),"Animation Have Done", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    };

Set Animation For View :

  view.startAnimation(animateRoationLayout(animationListener));
Tung Tran
  • 439
  • 6
  • 8
1

If you are using kotlin and androidx. There are some event methods

Animator.doOnEnd
Animator.doOnStart
Animator.doOnCancel
Animator.doOnRepeat
Animator.doOnPause
Animator.doOnResume

Example

val animator = ObjectAnimator.ofFloat(progressBar, View.ALPHA, 1f, 0.5f)
animator.doOnEnd {
    //
}
Linh
  • 57,942
  • 23
  • 262
  • 279