动画结束后我该怎么做?

发布于 2024-12-02 16:01:51 字数 472 浏览 1 评论 0原文

我有一个 ImageView,用于通过 AnimationDrawable 显示进度。当我想显示进度微调器时,我这样做:

animDrawable.start();
ObjectAnimator.ofFloat(view, "alpha", 1.0f).setDuration(300).start();

当我想隐藏微调器时,我这样做:

ObjectAnimator.ofFloat(view, "alpha", 0.0f).setDuration(300).start();
animDrawable.stop();

但是,这会产生动画立即停止的效果。我希望它仅在 ObjectAnimator 完全褪色到 0.0 alpha 后停止。有没有办法可以按照“AnimationCompleted”回调来设置某些内容?

I have an ImageView that I use to show progress via an AnimationDrawable. When I want to show my progress spinner, I do this:

animDrawable.start();
ObjectAnimator.ofFloat(view, "alpha", 1.0f).setDuration(300).start();

When I want to hide the spinner, I do this:

ObjectAnimator.ofFloat(view, "alpha", 0.0f).setDuration(300).start();
animDrawable.stop();

However, this has the effect that the animation stops immediately. I would like it to stop only after the ObjectAnimator has completely faded to 0.0 alpha. Is there a way I can setup something along the lines of an "AnimationCompleted" callback?

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

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

发布评论

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

评论(5

无风消散 2024-12-09 16:01:51

更现代的方法是使用 ViewPropertyAnimator

view.animate()
    .alpha(0f)
    .withEndAction(new Runnable() {
      @Override
      public void run() {
        // Do something.
      }
    })
    .start();

或者,如果您使用的是 RetroLambda:

view.animate()
    .alpha(0f)
    .withEndAction(() -> {
      // Do something.
    })
    .start();

The more modern way of doing this is to use the ViewPropertyAnimator:

view.animate()
    .alpha(0f)
    .withEndAction(new Runnable() {
      @Override
      public void run() {
        // Do something.
      }
    })
    .start();

Or, if you're using RetroLambda:

view.animate()
    .alpha(0f)
    .withEndAction(() -> {
      // Do something.
    })
    .start();
我三岁 2024-12-09 16:01:51

对于有关 ObjectAnimator 对象的原始问题,您可以设置一个 Animator.AnimatorListener 对象,该对象定义多个动画状态回调。您想要覆盖 public void onAnimationEnd(Animator Animation)

animation.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    Toast.makeText(VideoEditorActivity.this, "animation ended", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });

To your original question about the ObjectAnimator object you can set up an Animator.AnimatorListener object which defines several animation state callbacks. You want to override public void onAnimationEnd(Animator animation)

animation.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    Toast.makeText(VideoEditorActivity.this, "animation ended", Toast.LENGTH_LONG).show();
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
小红帽 2024-12-09 16:01:51

使用 androidx,您可以使用 doOnEnd 方法,该方法在动画结束时调用

val anim = ObjectAnimator.ofFloat(eva_image, View.TRANSLATION_Y, 0f, 500f)
anim.setDuration(500)
anim.doOnEnd { Toast.makeText(this, "Anim End", Toast.LENGTH_SHORT).show() }
anim.start()

With androidx you can use doOnEnd method which invoked when the animation has ended

val anim = ObjectAnimator.ofFloat(eva_image, View.TRANSLATION_Y, 0f, 500f)
anim.setDuration(500)
anim.doOnEnd { Toast.makeText(this, "Anim End", Toast.LENGTH_SHORT).show() }
anim.start()
假扮的天使 2024-12-09 16:01:51

您还可以查看 postOnAnimation(Runnable)

文档链接: postOnAnimation(java.lang.Runnable)

You could also look into postOnAnimation(Runnable)

Link to Docs: postOnAnimation(java.lang.Runnable)

你是暖光i 2024-12-09 16:01:51

您可以将此代码与 Kotlin 上的对象动画器一起使用

ObjectAnimator.ofInt(
                view,
                "alpha",
                1.0f
            ).apply {
                duration = 300
                // some other params here
                doOnEnd { // your logic when the animation ended }
                start()
            }

You can use this code with Object animator on Kotlin

ObjectAnimator.ofInt(
                view,
                "alpha",
                1.0f
            ).apply {
                duration = 300
                // some other params here
                doOnEnd { // your logic when the animation ended }
                start()
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文