Make ObjectAnimator animation duration independent of global animator duration scale setting in developer options

纵饮孤独 提交于 2021-02-18 12:29:17

问题


How can you make ObjectAnimator independent of the "Animator duration scale" developer options setting when constant and unmodifiable speed is critical?


回答1:


I noticed there is no explicit answer for this. You can do this by calling the hidden API via reflection:

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

For more details you can check this blog post: https://arpytoth.com/2016/12/20/android-objectanimator-independent-of-global-animator-duration/




回答2:


Had to use hidden API to access the setDurationScale method on the ValueAnimator object.




回答3:


If you dont want to mess with setDurationScale, just do this

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


来源:https://stackoverflow.com/questions/28516100/make-objectanimator-animation-duration-independent-of-global-animator-duration-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!