活动开始时的动画跳转 [英] Animation at the beginning of activity jumps

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

问题描述

我正在为Android应用制作自定义视图的动画。我已经通过属性动画并调用<$ c在 onAnimationUpdate()回调视图中的$ c> invalidate()中,按照 https://developer.android.com/guide/topics/graphics/prop-animation.html

I am working on animating custom Views for my Android app. I have accomplished this via Property Animations and calling invalidate() on the View in the onAnimationUpdate() callback, as per https://developer.android.com/guide/topics/graphics/prop-animation.html:


根据要设置动画的属性或对象,可能需要在View上调用invalidate()方法来强制屏幕重新绘制自身与更新的动画值。您可以在onAnimationUpdate()回调中执行此操作。

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.

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

My problem is that when these animations are run at the beginning of a newly launched Activity, they skip frames at the beginning, causing them to jump very noticeably. I have tried both:


  1. 立即从Activity的 onCreate()开始动画方法

  2. OnGlobalLayout()回调中使用 ViewTreeObserver

  1. Starting the animation immediately from the Activity's onCreate() method
  2. Starting the animation upon the OnGlobalLayout() callback using the ViewTreeObserver of the Activity's root view.

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

I did the latter since I thought maybe the animation was invoked before the layout had been finalized, but the result is the same.

对于日志,我确定 onAnimationUpdate()回调在整个动画中被一致地调用(即从开始到结束,每10-20毫秒左右)。 onAnimationUpdate()只需调用 invalidate() ,这将强制View重绘其自身,最好是立即重绘(但文档仅声称这种情况在未来)。这似乎正是问题所在: onDraw() 在开始时仅被调用一次或两次,直到被调用约250毫秒。此后,它将每隔10到20毫秒恢复一次调用,因为它应该一直存在。但是那段时间会在动画中引起非常明显的滞后。

With logs, I determined that the onAnimationUpdate() callback is called consistently throughout the animation (that is, every 10-20 ms or so, from start to end). onAnimationUpdate() simply calls 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() is only called once or twice at the very start before not being called for about 250 ms. After this, it resumes being called every 10-20 ms, as it should have the whole time. But that block of time causes very noticeable lag in the animation.

要清楚的是,此问题仅在活动开始时发生。如果我只是在开始动画之前设置300 ms的延迟,则动画将一直平稳运行。但我不喜欢这种解决方案,因为它很笨拙。看来问题在于 onDraw()不会在活动开始附近的 invalidate()时立即调用。但是,我不知道为什么会这样,是什么阻止了 onDraw()或如何修复它。

To be clear, this problem only happens at the beginning of the Activity. If I simply set a 300 ms delay before starting the animation, it runs smoothly all the way through. But I don't like that solution, since it's hacky. It seems the problem is that onDraw() is not called immediately upon invalidate() near the beginning of the Activity. But, I can't figure out why this is, what's blocking onDraw(), or how to fix it at all.

我仅找到以下StackOverFlow线程:动画的开头活动会跳过帧,其中发帖人存在相同问题。那里有基本代码,而视频则可以使问题清楚。我也可以发布代码,但是我认为问题出现在最基本的测试应用程序中,这表明还存在其他问题。

I found only this StackOverFlow thread: Animation at the beginning of activity skips frames where the poster has the identical problem. The basic code is there and the videos make the problem clear. I can post my code too, but I think that the fact that the problem appears in the most basic test app shows that there's something else going on.

推荐答案

由于没有附加代码,因此我假设您对活动具有一些过渡动画。如果是这种情况,则可能会引起问题。由于有两个动画同时运行。

Since there are no codes attached, I'm assuming you have some transition animation to the activity. If this is the case, it might cause the problem.Since there are two animations running simultaneously. Disable the transition and give it try.

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

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

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