活动开始时的动画会跳过帧 [英] Animation at the beginning of activity skips frames

查看:175
本文介绍了活动开始时的动画会跳过帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在视图上调用 onGlobalLayoutFinished 后,我正在为活动中的视图添加动画。我的动画开始时跳过了约300毫秒的帧。如果我将动画延迟了大约300毫秒以上,则它不会跳过任何帧。导致这种情况发生的活动中发生了什么?我该如何停止它或如何听完它?

I am animating a view in an activity after onGlobalLayoutFinished is called on the view. My animation is skipping ~300 ms worth of frames in the beginning. If I delay the animation by more than ~300ms, it does not skip any frames. What is going on in the activity that is causing this to happen? How can I stop it or how can I listen for when it is finished?

我已经创建了一个简单的简单应用程序来演示此行为。

I have created a dead simple app to demonstrate this behavior.

AndroidManifest.xml中< application> 的内容:

contents of <application> in AndroidManifest.xml:

<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个像素。如果设置了延迟,则动画会平滑。请观看下面的视频。

In this demo, a red square moves from left to right 1000 pixels over 1000 milliseconds. If no delay is set, it skips roughly the first 300 pixels. If a delay is set, it animates smoothly. Please see videos below.

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

<强> 400毫秒的延迟(不跳过成名)::
https://www.youtube.com/watch?v=zW0akPhl_9I&feature=youtu.be

欢迎任何评论。

推荐答案

发生这种情况是由于活动的过渡动画在创建活动时播放的。因此,您的自定义动画会在系统忙于绘制过渡时启动。

This happens because of the activity's transition animation, which is played while the activity is being created. As a result, your custom animation starts while the system is busy drawing the transition.

我还没有找到一个好的解决方案。现在,我只是在等待过渡的持续时间,然后再开始制作动画,但这远非完美:

I haven't found a clean solution yet. For now, I'm simply waiting for the duration of the transition before starting my animation, but it is far from being perfect:

// 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);

您还可以改为禁用活动转换,

You could also disable activity transitions instead, with:

overridePendingTransition(0, 0);

我仍在寻找一种方法来监听转换的确切结束时间。如果我找到解决方案,将会及时通知您。

I'm still looking for a way to listen to the exact end time of the transition. Will keep you posted if I ever find a solution.

这篇关于活动开始时的动画会跳过帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆