Android Animation Listener

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


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) {

    }
});

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

fadeInAnimation.withEndAction(new Runnable() {
    @Override
    public void run() {
        ... do stuff
    }
})

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

    })

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)

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));