使ObjectAnimator动画的持续时间独立于开发者选项中全局动画持续时间比例设置

7

当需要恒定且不可更改的速度时,您如何使ObjectAnimator与“动画持续时间比例”开发人员选项设置无关?

3个回答

13
我注意到这件事没有明确的答案。你可以通过反射调用隐藏的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/


非常好,这个答案仍然存在,因为链接的域名已经完全离线,而互联网档案馆没有它的副本。 - i336_

2
如果您不想处理setDurationScale,只需执行以下操作:
//in activity, kotlin
val animScale = Settings.Global.getFloat(this.contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1f)
animator.setDuration((timeInMs/animScale).toLong())

4
这并不能完全解决问题,因为用户可以关闭动画,这会将比例设置为0,从而导致失败。 - Felipe Ribeiro R. Magalhaes

0

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


1
你是怎么做到的?能否请您提供一些示例来帮助我。 - Nitesh Tiwari

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接