Android View.startAnimation()与Robolectric 3.2.1一起导致无止境循环
我正在使用Robolectric3.2.1进行一些UT。在我的测试活动的onCreate()期间,我启动了一个信息,如下所示:Android View.startAnimation()与Robolectric 3.2.1一起导致无止境循环,android,unit-testing,robolectric,Android,Unit Testing,Robolectric,我正在使用Robolectric3.2.1进行一些UT。在我的测试活动的onCreate()期间,我启动了一个信息,如下所示: protected void onCreate(Bundle savedInstanceState){ ... mImageView.startAnimation(createAnim()); ... } 创建动画集的方法: private AnimationSet createAnim(){ AnimationSet scaleAndAlph
protected void onCreate(Bundle savedInstanceState){
...
mImageView.startAnimation(createAnim());
...
}
创建动画集的方法:
private AnimationSet createAnim(){
AnimationSet scaleAndAlphaAnim = (AnimationSet)AnimationUtils.loadAnimation(this, R.anim.radar_scale_alpha_anim);
scaleAndAlphaAnim.setInterpolator(new LinearInterpolator());
for (Animation anim : scaleAndAlphaAnim.getAnimations()) {
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(1500L);
}
return scaleAndAlphaAnim;
}
和动画文件:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:fromXScale="0.75"
android:fromYScale="0.75"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
主线程在mImageView.startAnimation(createAnim())处陷入无休止的循环中。但是代码在我的真实设备上运行良好。现在我很困惑。有人能告诉我为什么吗?我会在机器人上制造问题,在我添加scaleAndAlphaAnim.setRepeatCount(Animation.INFINITE)后,这种现象就会消失。经过进一步调试,我发现在ShadowView的AnimationRunner.run()方法中,animation.getRepeatCount()==animation.INFINITE被视为打破循环的条件之一,当动画的默认repeatCount为0时,我将在Robolectric GitHub上创建问题。添加scaleAndAlphaAnim.setRepeatCount(Animation.INFINITE)后,该现象将消失。通过进一步调试,我发现在ShadowView的AnimationRunner.run()方法中,animation.getRepeatCount()==animation.INFINITE被视为中断循环的条件之一,而动画的默认repeatCount为0。
Robolectric.setupActivity(TestActivity.class);