简体   繁体   English

Android,无限重复动画与instrumentation.waitForIdleSync()

[英]Android, Infinitely repeating animation vs. instrumentation.waitForIdleSync()

I have project with a custom view which is animated. 我有一个项目,其中包含一个动画的custom view The animation is a simple alpha fade which repeats itself infinitely. 动画是一个简单的alpha淡入淡出,无限重复。 The view is a cursor , and it makes the cursor look like it is blinking. 视图是一个cursor ,它使cursor看起来像是闪烁。 When I started writing functional unit tests for the project, I ran into the problem that the getActivity() method would hang in the setUp method due to the animation. 当我开始为项目编写功能单元测试时,我遇到了由于动画而导致getActivity()方法在setUp方法中挂起的问题。 getActivity() calls waitForIdleSync() before returning, and since the animation is started at the beginning, getActivity() never returns. getActivity()在返回之前调用waitForIdleSync() ,并且由于动画waitForIdleSync()开始,因此getActivity()永远不会返回。 This actually took me quite a bit of time to figure out, i would start the test and it would sit there doing nothing... This would also happen 100% of the time. 这实际上花了我相当多的时间来弄清楚,我会开始测试,它会坐在那里什么都不做......这也会100%的时间发生。

I wouldn't consider this a BUG, because technically if the animation is running, the device isn't idle, however this is an ISSUE. 我不认为这是一个BUG,因为从技术上讲,如果动画正在运行,设备不会空闲,但这是一个问题。 Any suggestions or workarounds to allow the animation to run, from the beginning, but also use the waitForIdleSync during ActivityInstrumentationTestCase2s would be greatly appreciated. 任何建议或变通方法允许动画从一开始就运行,但在ActivityInstrumentationTestCase2s期间也使用waitForIdleSync将不胜感激。 I could implement a workaround to not use waitForIdleSync() by waiting for other events to provide synchronization with test actions and app actions, however I still HAVE to call the getActivity() at some point. 我可以通过等待其他事件提供与测试操作和应用程序操作的同步来实现不使用waitForIdleSync()的解决方法,但是我仍然需要在某个时候调用getActivity() Once in this method, it never returns with an infinite repeating animation. 一旦进入此方法,它永远不会返回无限重复动画。 Also the animation has to start sometime before the end of the onRestart method. 此外,动画必须在onRestart方法结束之前的某个时间开始。 Ideally, there would be a waitForIdle(int timeout) method. 理想情况下,会有一个waitForIdle(int timeout)方法。

I am starting the animation at the wrong time, or am I not starting the animation using the correct methods? 我在错误的时间开始动画,或者我没有使用正确的方法启动动画? I start/stop the animation programmatically because there is a need to start and stop the blinking behavior in the app. 我以编程方式启动/停止动画,因为需要启动和停止应用程序中的闪烁行为。 below is the code which I use to start the animation. 下面是我用来启动动画的代码。 the startBlinking(Context) method is called from within the cursors constructor... cursors构造函数中调用startBlinking(Context)方法...

public void startBlinking(Context context){
    Animation blink = AnimationUtils.loadAnimation(context, R.anim.cursor_blink);
    blink.setRepeatMode(Animation.RESTART);
    blink.setRepeatCount(Animation.INFINITE);
    startAnimation(blink);
}

I had this issue and reported it as a bug on AOSP , but I don't expect anyone will fix it any time soon. 我有这个问题,并将其报告为AOSP上的一个错误 ,但我不希望任何人会在短期内修复它。 My workaround was to remove the animation (which was challenging, as it was the indefinite progress view). 我的解决方法是删除动画(这是具有挑战性的,因为它是无限期的进度视图)。 One option for your case would be to change the animation timing in your tests so it doesn't animate every frame. 您的案例的一个选项是更改测试中的动画时序,以便它不会为每个帧设置动画。 Not ideal, but better than freezing I suspect. 不太理想,但比我怀疑的更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM