使用这种方式:
1. 你在 Gradle 中使用这个
android {
//...
testOptions {
animationsDisabled = true
}
// ...
}
2.在ADB for device中使用
adb shell settings put global window_animation_scale 0 &
adb shell settings put global transition_animation_scale 0 &
adb shell settings put global animator_duration_scale 0 &
3.使用规则
class DisableAnimationsRule : TestRule {
override fun apply(base: Statement, description: Description): Statement {
return object : Statement() {
@Throws(Throwable::class)
override fun evaluate() {
// disable animations for test run
changeAnimationStatus(enable = false)
try {
base.evaluate()
} finally {
// enable after test run
changeAnimationStatus(enable = true)
}
}
}
}
@Throws(IOException::class)
private fun changeAnimationStatus(enable:Boolean = true) {
with(UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())){
executeShellCommand("settings put global transition_animation_scale ${if(enable) 1 else 0}")
executeShellCommand("settings put global window_animation_scale ${if(enable) 1 else 0}")
executeShellCommand("settings put global animator_duration_scale ${if(enable) 1 else 0}")
}
}
}