【问题标题】:Resume animation before start another Activity在开始另一个活动之前恢复动画
【发布时间】:2019-01-03 12:43:55
【问题描述】:

我有序列动画。动画方向为 420 毫米。动画完成后,我尝试使用 ActivityOptionsCompat 启动另一个活动(从下到上动画)。这是我的代码

private void startSceneAnimation() {
    if (imageView != null) {
        ((AnimationDrawable) imageView.getBackground()).start();
        new Handler().postDelayed(() -> {
            Intent intent = new Intent(LoginTestActivity.this,
                    SPLoginActivity.class);
            ActivityOptionsCompat options = ActivityOptionsCompat.
                    makeSceneTransitionAnimation(LoginTestActivity.this,
                            logo,
                            ViewCompat.getTransitionName(logo));
            startActivity(intent, options.toBundle());
        }, 420);
    }
}

一切正常,但我有一个问题。在动画仍然完成时是否可以开始新活动(没有停止动画)。我尝试在 300 毫米后开始新活动,但我的序列动画完成了

【问题讨论】:

    标签: android material-design android-animation


    【解决方案1】:

    AnimationDrawable 在 UI 线程上执行。在没有为Looper 传入参数的情况下生成的匿名Handler 实例也在UI 线程上运行。因此,在AnimationDrawable 执行完成之前,可能不会在Handler 执行完任何时间后将活动排队以更改,因为它们都在同一个Thread

    尝试改用Timer

    Timer timer = new Timer();
    timer.schedule(new TimerTask(){
      public void run() {
        Intent intent = new Intent(LoginTestActivity.this,
                    SPLoginActivity.class);
            ActivityOptionsCompat options = ActivityOptionsCompat.
                    makeSceneTransitionAnimation(LoginTestActivity.this,
                            logo,
                            ViewCompat.getTransitionName(logo));
            startActivity(intent, options.toBundle());
      }
    }, 300);
    

    Timer runs in a background thread,并且由于它快速退出它不应该导致任何内存泄漏 AFAIK。

    编辑:似乎在 Android 框架中启动了一个 Activity internally(第 4614 行),并且可以是 wonky if attempted from outside the UI thread。您可以尝试在 UI 线程消息队列的前面发布新的 Intent 并查看它是否有效,但它可能会导致动画“停止”,因为现在正在使用同一个 UI 线程来处理活动启动。

    Timer timer = new Timer();
        timer.schedule(new TimerTask(){
          public void run() {
            new Handler(Looper.getMainLooper()).postAtFrontOfQueue(() -> {
                Intent intent = new Intent(LoginTestActivity.this,
                        SPLoginActivity.class);
                ActivityOptionsCompat options = ActivityOptionsCompat.
                        makeSceneTransitionAnimation(LoginTestActivity.this,
                                logo,
                                ViewCompat.getTransitionName(logo));
                startActivity(intent, options.toBundle());
              });
          }
        }, 300);
    

    【讨论】:

    • 我试过了,但应用程序崩溃了。这是一个日志结果:Can't create handler inside thread Thread @Mercato
    • 你有更详细的崩溃日志吗?它指的是哪一行
    • 这里是。 java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程 Thread[Timer-0,5,main] 内创建处理程序
    • 在这一行 ViewCompat.getTransitionName @ Mercato
    • 你是对的,但是动画停止了,不幸的是@Mercato
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2012-12-30
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    相关资源
    最近更新 更多