Android Animation Listener

后端 未结 5 1063
眼角桃花
眼角桃花 2020-12-03 04:37

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

    myImageView.setOnTouchListener(new View.OnTouchListener() {
    publ         


        
相关标签:
5条回答
  • 2020-12-03 04:52

    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));
    
    0 讨论(0)
  • 2020-12-03 04:55

    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) {
    
        }
    });
    
    0 讨论(0)
  • 2020-12-03 05:00

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

    fadeInAnimation.withEndAction(new Runnable() {
        @Override
        public void run() {
            ... do stuff
        }
    })
    
    0 讨论(0)
  • 2020-12-03 05:04

    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.
            }
    
        })
    
    0 讨论(0)
  • 2020-12-03 05:07

    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)
    
    0 讨论(0)
提交回复
热议问题