使ObjectAnimator动画持续时间与开发人员选项中的全局动画持续时间比例设置无关

时间:2015-02-14 13:28:09

标签: android animation duration objectanimator animator

当常量和不可修改的速度至关重要时,如何使ObjectAnimator独立于“Animator持续时间刻度”开发人员选项设置?

3 个答案:

答案 0 :(得分:6)

我注意到没有明确的答案。您可以通过反射调用隐藏的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.
  }
}

有关详细信息,请查看此博文:https://arpytoth.com/2016/12/20/android-objectanimator-independent-of-global-animator-duration/

答案 1 :(得分:0)

必须使用隐藏的API来访问setDurationScale对象上的ValueAnimator方法。

答案 2 :(得分:0)

如果您不想弄乱setDurationScale,只需执行

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