结束动画事件android

发布于 2024-12-07 02:20:44 字数 156 浏览 1 评论 0原文

我在视图中有一个淡出动画(位于片段内),每次动画发生时,视图完成后都会再次重新绘制自身。我找到了一种解决方法 view.SetVisibility(View.GONE) 。但它不会等待动画完成。我想仅在动画完成后执行此 setVisibility 代码。最好的方法是什么?

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 around doing view.SetVisibility(View.GONE) . But it doesn't wait for the animation to finish. I would like to execute this setVisibility code only after the animation has finished. What is the best way to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半仙 2024-12-14 02:20:45

您可以将动画侦听器添加到动画对象中,例如

anim.setAnimationListener(new Animation.AnimationListener(){
    @Override
    public void onAnimationStart(Animation arg0) {
    }           
    @Override
    public void onAnimationRepeat(Animation arg0) {
    }           
    @Override
    public void onAnimationEnd(Animation arg0) {
    }
});

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) {
    }
});
谎言月老 2024-12-14 02:20:45

功能上与已接受的答案相同,但以更简洁的方式:

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

享受:)

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

和影子一齐双人舞 2024-12-14 02:20:45

只需获取您的动画对象并向其添加动画侦听器即可。
这是示例代码:

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**


            }
        });

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**


            }
        });
热鲨 2024-12-14 02:20:45

您还可以使用 Animation.setFillAfter

You can also achieve this using Animation.setFillAfter

千鲤 2024-12-14 02:20:45

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

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
        }
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文