7

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

4

3 回答 3

12

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

于 2016-12-20T09:14:28.413 回答
2

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

//in activity, kotlin
val animScale = Settings.Global.getFloat(this.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f)
animator.setDuration((timeInMs/animScale).toLong())
于 2020-07-01T09:05:44.530 回答
1

不得不使用隐藏的 API 来访问对象setDurationScale上的方法ValueAnimator

于 2015-02-14T15:54:51.733 回答