Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 277
user1612686 Avatar asked Feb 14 '15 13:02

user1612686


People also ask

What is the meaning of animator duration scale?

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.

Which of the following options helps you to hold a time value pair for animation in Android?

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.

What is ObjectAnimator?

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.


2 Answers

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())
like image 42
ueen Avatar answered Nov 14 '22 22:11

ueen


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/

like image 74
Arpad Toth Avatar answered Nov 14 '22 23:11

Arpad Toth