【问题标题】:Animation at the beginning of activity skips frames活动开始时的动画跳过帧
【发布时间】:2015-04-11 04:45:43
【问题描述】:

在视图上调用onGlobalLayoutFinished 后,我正在为活动中的视图设置动画。我的动画在开始时跳过了大约 300 毫秒的帧。如果我将动画延迟超过 ~300 毫秒,它不会跳过任何帧。导致这种情况发生的活动中发生了什么?我怎样才能停止它或者我怎样才能听它何时完成?

我创建了一个非常简单的应用程序来演示这种行为。

AndroidManifest.xml 中<application> 的内容:

<activity
    android:name=".main.TestLagActivity"
    android:label="Test Lag Activity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

TestLagActivity.java:

public class TestLagActivity extends ActionBarActivity { 
  private View mRedSquareView;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test_lag);

    mRedSquareView = findViewById(R.id.activity_test_lag_redSquareView);

    if (mRedSquareView.getViewTreeObserver() != null) {
      mRedSquareView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
          mRedSquareView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
          animate();
        }
      });
    }
  }

  private void animate() {
    ObjectAnimator xAnimator = ObjectAnimator.ofFloat(mRedSquareView, "x", 0, 1000);
    xAnimator.setDuration(1000);
    xAnimator.start();
  }
}

activity_test_lag.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">

  <View
      android:id="@+id/activity_test_lag_redSquareView"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:background="#FF0000"/>

</FrameLayout>

在此演示中,红色方块在 1000 毫秒内从左向右移动 1000 像素。如果没有设置延迟,它将大致跳过前 300 个像素。如果设置了延迟,它的动画效果会很流畅。请看下面的视频。

无延迟(跳帧): https://www.youtube.com/watch?v=dEwvllhvvN0

400ms 延迟(不跳过名声): https://www.youtube.com/watch?v=zW0akPhl_9I&feature=youtu.be

欢迎任何cmets。

【问题讨论】:

  • 您确实需要展示您的 onPause 和 onSaveInstanceState 方法,并可能展示您的视图层次结构。您可能在 onSaveInstanceState 之前的 onPause 中有自定义逻辑阻塞,或者您可能有一个自定义视图在 onSaveInstanceState 中阻塞。
  • "onPause 和 onSaveInstanceState 通常在 MainActivity 中彼此相距 20 毫秒内发生" ...您在 CreatActivity 中有什么不同?
  • @dcow 我应该提到的方法是空的,除了日志语句。
  • @JeffreyBlattman 我指的是开始新活动和关闭屏幕时的区别。
  • @asaini007 不幸的是,我没有。由于其他原因,用户界面已更改,这不再是问题。

标签: android android-layout animation android-activity activity-lifecycle


【解决方案1】:

这是因为 Activity 的过渡动画,该动画是在创建 Activity 时播放的。因此,您的自定义动画会在系统忙于绘制过渡时开始。

我还没有找到一个干净的解决方案。现在,我只是在开始动画之前等待过渡的持续时间,但这远非完美:

// The duration of the animation was found here:
// http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.5_r1/frameworks/base/core/res/res/anim/activity_open_enter.xml 
new Handler().postDelayed(runnable, android.R.integer.config_shortAnimTime);

您也可以改为禁用活动转换:

overridePendingTransition(0, 0);

我仍在寻找一种方法来了解过渡的确切结束时间。如果我找到解决方案,会及时通知您。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-09
    • 2013-10-09
    • 2016-03-25
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    相关资源
    最近更新 更多