安卓动画监听器

51

当触摸ImageView时,我正在启动一个淡入动画:

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

我知道需要使用动画监听器才能确定动画何时完成,但如何附加它以便我可以获取刚刚完成动画的视图?我希望在动画完成后设置视图的可见性。

谢谢


1
我想知道如何为 ImageView 添加动画监听器。 - beans
6个回答

133

我认为你需要这个。

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

    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});

如果你只需要使用方法的子集,你可以使用AnimatorListenerAdapter,然后只覆盖你需要的方法(例如onAnimationEnd())。 - user2891659

27

如果只需要结束动作,使用.withEndAction(Runnable)即可。

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

5
仅适用于 API 等级 16 或更高的版本。 - Roel
4
我正在针对API等级22进行开发,但在TranslateAnimation类中没有看到withEndAction()方法。 - Someone Somewhere
@Roel,你能给我提供一个链接吗?我想要检查api级别。 - mahdi pishguy
1
我也看不到 withEndAction,同样的情况也发生在 @SomeoneSomewhere 那里。 - HendraWD

7

如果有人需要 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.
        }

    })

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)

你好,我有一个关于你的代码的问题,我在我的问题中使用了它 - 你可以看一下吗:https://stackoverflow.com/questions/59253778/update-attributes-during-rotation-animation - KayD

3

我的函数 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;
}

定义动画监听器:
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) {

        }
    };

为视图设置动画:
  view.startAnimation(animateRoationLayout(animationListener));

1

如果你正在使用 kotlinandroidx,那么有一些事件方法可供使用。

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

示例

val animator = ObjectAnimator.ofFloat(progressBar, View.ALPHA, 1f, 0.5f)
animator.doOnEnd {
    //
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接