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

3

7

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

Diamine answered 14/2, 2015 at 13:28 Comment(0)
D
1

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

Diamine answered 14/2, 2015 at 15:54 Comment(1)
how did you did that? Could u pls post some example for help.Douglas
P
14

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/

Palumbo answered 20/12, 2016 at 9:14 Comment(1)
Very nice this answer is still here as the linked domain has gone entirely offline and the Internet Archive doesn't have a copy of it.Inconvincible
B
2

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())
Bulter answered 1/7, 2020 at 9:5 Comment(1)
This doesn't really solve the issue entirely as the user can turn off the animation, which sets the scale to 0. This would then fail.Ratliff
D
1

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

Diamine answered 14/2, 2015 at 15:54 Comment(1)
how did you did that? Could u pls post some example for help.Douglas

© 2022 - 2024 — McMap. All rights reserved.