End animation event android

前端 未结 5 934
隐瞒了意图╮
隐瞒了意图╮ 2020-12-05 12:34

I have a fadeout animation in a view (which is inside a fragment), and everytime the animation happens, after it finishes the view redraws itself again. I found a work aroun

相关标签:
5条回答
  • 2020-12-05 12:59

    You can add Animation listener to your animation object like

    anim.setAnimationListener(new Animation.AnimationListener(){
        @Override
        public void onAnimationStart(Animation arg0) {
        }           
        @Override
        public void onAnimationRepeat(Animation arg0) {
        }           
        @Override
        public void onAnimationEnd(Animation arg0) {
        }
    });
    
    0 讨论(0)
  • 2020-12-05 13:03

    You can also achieve this using Animation.setFillAfter

    0 讨论(0)
  • 2020-12-05 13:06

    Example for Kotlin

    var fadeOutImage = findViewById<ImageView>(R.id.fade_out_Image)
        val fadeOutAnimation = R.anim.fade_out_animation
        val animation = AnimationUtils.loadAnimation(this, fadeOutAnimation)
        fadeOutImage.startAnimation(animation)
    
        animation.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(p0: Animation?) {
    //                not implemented
            }
    
            override fun onAnimationRepeat(p0: Animation?) {
    //                not implemented
            }
    
            override fun onAnimationEnd(p0: Animation?) {
                fadeOutImage.visibility = View.INVISIBLE
            }
        })
    
    0 讨论(0)
  • 2020-12-05 13:16

    Functionally the same as the accepted answer but in a much more concise way:

    // Add/Remove any animation parameter
    theView.animate()
            .alpha(0)
            .setDuration(2000)
            .withEndAction(new Runnable() {
                @Override
                public void run() {
                    theView.setVisibility(View.GONE);
                }
            });
    

    Enjoy :)

    0 讨论(0)
  • 2020-12-05 13:16

    Simply take your animation object and add animation listener to it. Here is the example code :

    rotateAnimation.setAnimationListener(new AnimationListener() {
    
                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
    
                }
    
                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Auto-generated method stub
    
    **// WRITE HERE WHATEVER YOU WANT ON THE COMPLETION OF THE ANIMATION**
    
    
                }
            });
    
    0 讨论(0)
提交回复
热议问题