【问题标题】:Make ObjectAnimator animation duration independent of global animator duration scale setting in developer options使 ObjectAnimator 动画持续时间独立于开发人员选项中的全局动画持续时间比例设置
【发布时间】:2015-04-15 11:15:18
【问题描述】:

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

【问题讨论】:

    标签: android animation duration objectanimator animator


    【解决方案1】:

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

    【讨论】:

    • 非常好,这个答案仍然存在,因为链接的域已完全脱机并且 Internet 存档没有它的副本。
    【解决方案2】:

    如果你不想弄乱 setDurationScale,就这样做

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

    【讨论】:

    • 这并不能完全解决问题,因为用户可以关闭动画,将比例设置为 0。这将失败。
    【解决方案3】:

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

    【讨论】:

    • 你是怎么做到的?您能否发布一些示例以寻求帮助。
    猜你喜欢
    • 2018-06-15
    • 2011-11-29
    • 1970-01-01
    • 2014-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多