How can you make ObjectAnimator independent of the "Animator duration scale" developer options setting when constant and unmodifiable speed is critical?
Transition animation scale controls the speed of transition animations between screens. For example, when you tap an option in your smartphone's settings or back out of a menu. Animator duration scale changes the speed of pretty much every other animation that happens within the OS.
ValueAnimator provides a timing engine for running animation which calculates the animated values and set them on the target objects. By ValueAnimator you can animate a view width, height, update its x and y coordinates or even can change its background.
ObjectAnimator is a Subclass of ValueAnimator, which allows us to set a target object and object property to animate. It is an easy way to change the properties of a view with a specified duration. We can provide the end position and duration of the animation.
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())
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With