Android 使ObjectAnimator动画持续时间独立于“开发人员选项”中的“全局动画持续时间比例”设置

Android 使ObjectAnimator动画持续时间独立于“开发人员选项”中的“全局动画持续时间比例”设置,android,animation,duration,objectanimator,animator,Android,Animation,Duration,Objectanimator,Animator,当恒定且不可修改的速度非常关键时,如何使ObjectAnimator独立于“Animator duration scale”开发人员选项设置?必须使用隐藏API访问ValueAnimator对象上的设置DurationScale方法。我注意到没有明确的答案。您可以通过反射调用隐藏的API来实现这一点: // Get duration scale from the global settings. float durationScale = Settings.Global.getFloat(con

当恒定且不可修改的速度非常关键时,如何使ObjectAnimator独立于“Animator duration scale”开发人员选项设置?

必须使用隐藏API访问
ValueAnimator
对象上的
设置DurationScale
方法。

我注意到没有明确的答案。您可以通过反射调用隐藏的API来实现这一点:

// 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.
  }
}

有关更多详细信息,请查看此博文:

如果不想弄乱setDurationScale,请执行此操作

//in activity, kotlin
val animScale = Settings.Global.getFloat(this.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f)
animator.setDuration((timeInMs/animScale).toLong())

你是怎么做到的?你可以发布一些帮助的例子。这并不能完全解决这个问题,因为用户可以关闭动画,将比例设置为0。这样就会失败。