当需要恒定且不可更改的速度时,您如何使ObjectAnimator与“动画持续时间比例”开发人员选项设置无关?
当需要恒定且不可更改的速度时,您如何使ObjectAnimator与“动画持续时间比例”开发人员选项设置无关?
// Get duration scale from the global settings.
float durationScale = Settings.Global.getFloat(context.getContentResolver(),
Settings.Global.ANIMATOR_DURATION_SCALE, 0);
// If global duration scale is not 1 (default), try to override it
// for the current application.
if (durationScale != 1) {
try {
ValueAnimator.class.getMethod("setDurationScale", float.class).invoke(null, 1f);
durationScale = 1f;
} catch (Throwable t) {
// It means something bad happened, and animations are still
// altered by the global settings. You should warn the user and
// exit application.
}
}
更多详细信息,请查看这篇博客文章:https://arpytoth.com/2016/12/20/android-objectanimator-independent-of-global-animator-duration/
//in activity, kotlin
val animScale = Settings.Global.getFloat(this.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f)
animator.setDuration((timeInMs/animScale).toLong())
不得不使用隐藏的 API 访问 ValueAnimator 对象上的 setDurationScale 方法。