activity 跳开始的动画

Animation at the beginning of activity jumps

我正在为我的 Android 应用制作自定义视图的动画。我通过 Property Animations and calling invalidate() on the View in the onAnimationUpdate() callback, as per https://developer.android.com/guide/topics/graphics/prop-animation.html:

完成了这个

Depending on what property or object you are animating, you might need to call the invalidate() method on a View to force the screen to redraw itself with the updated animated values. You do this in the onAnimationUpdate() callback.

我的问题是,当这些动画 运行 在 新推出的 Activity 的开头时,它们会在开头跳过帧,从而导致它们跳得非常明显。我都试过了:

  1. 立即从 Activity 的 onCreate() 方法开始动画
  2. 使用 Activity 的根视图的 ViewTreeObserverOnGlobalLayout() 回调时启动动画。

我选择了后者,因为我认为可能在布局完成之前调用了动画,但结果是一样的。

通过日志,我确定 onAnimationUpdate() 回调在整个动画过程中被一致调用(即,从开始到结束,每 10-20 毫秒左右)。 onAnimationUpdate() 简单地调用 invalidate(), which should force the View to redraw itself, ideally immediately (but the documentation only claims this happens "at some point in the future"). That seems to be precisely the problem: onDraw() 在一开始只调用一次或两次,然后大约 250 毫秒没有被调用。在此之后,它会恢复每 10-20 毫秒被调用一次,因为它应该一直如此。但是那段时间会导致动画出现非常明显的延迟。

需要说明的是,这个问题只发生在 Activity 的开头。如果我在开始动画之前简单地设置 300 毫秒的延迟,它 运行 会一直顺利进行。但我不喜欢那个解决方案,因为它很老套。问题似乎是 onDraw() 没有在 Activity 开头附近的 invalidate() 上立即被调用。但是,我不知道为什么会这样,是什么阻碍了 onDraw(),也不知道如何解决它。

我只发现这个 Whosebug 线程:Animation at the beginning of activity skips frames 其中 poster 有 相同的 问题。基本代码在那里,视频使问题变得清晰。我也可以 post 我的代码,但我认为问题出现在最基本的测试应用程序中这一事实表明还有其他事情正在发生。

由于没有附加代码,我假设您有一些到 activity 的过渡动画。如果是这种情况,可能会导致 problem.Since 同时有两个动画 运行。禁用转换并试一试。

startActivity(intent);
getActivity().overridePendingTransition(0, 0); 

听起来您的绘图循环 UI 线程快没电了。

我会使用 traceview 来确保没有任何方法阻止您的绘制调用。 http://tools.android.com/tips/traceview

这应该可以帮助您确定调用的是什么而不是 onDraw 方法。