Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find out if window animations are enabled in settings

Tags:

android

I know, I can start the Settings-Activity with

Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
startActivityForResult(intent,1);

But how do I know if the animations are enabled in the first place?

I have an animation inside a custom view and only want to show it, if the animations are enabled in the settings. If they are disabled, I'd like to ask the user to enable them the first time he starts the application.

like image 545
roplacebo Avatar asked Sep 23 '11 13:09

roplacebo


2 Answers

Settings.System.TRANSITION_ANIMATION_SCALE and Settings.System.ANIMATOR_DURATION_SCALE are deprecated from API 17.

So I use this method to find out.

private boolean areSystemAnimationsEnabled() {
  float duration, transition;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    duration = Settings.Global.getFloat(
                  context.getContentResolver(), 
                  Settings.Global.ANIMATOR_DURATION_SCALE, 1);
    transition = Settings.Global.getFloat(
                  context.getContentResolver(), 
                  Settings.Global.TRANSITION_ANIMATION_SCALE, 1);
  } else {
    duration = Settings.System.getFloat(
                  context.getContentResolver(), 
                  Settings.System.ANIMATOR_DURATION_SCALE, 1);
    transition = Settings.System.getFloat(
                  context.getContentResolver(), 
                  Settings.System.TRANSITION_ANIMATION_SCALE, 1);
  }
  return (duration != 0 && transition != 0);
}

Or, you can check only for ANIMATOR_DURATION_SCALE...

private float checkSystemAnimationsDuration() {
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    return Settings.Global.getFloat(
        context.getContentResolver(),
        Settings.Global.ANIMATOR_DURATION_SCALE,
        0);
  } else {
    return Settings.System.getFloat(
        context.getContentResolver(),
        Settings.System.ANIMATOR_DURATION_SCALE,
        0);
  }
}

and set your valueAnimator.setDuration() accordingly.

ValueAnimator alphaFirstItemAnimator = new ValueAnimator();
alphaFirstItemAnimator.setObjectValues(0.8F, 0F);
alphaFirstItemAnimator.setDuration((long)(DURATION_ANIMATION_MILLIS * checkSystemAnimationsDuration()));
alphaFirstItemAnimator.setInterpolator(new DecelerateInterpolator());
alphaFirstItemAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  public void onAnimationUpdate(ValueAnimator animation) {

  }
});

I hope it helps someone.

like image 136
Mr.Moustard Avatar answered Sep 23 '22 17:09

Mr.Moustard


Check out http://developer.android.com/reference/android/provider/Settings.System.html.

You can read the flags:

  • TRANSITION_ANIMATION_SCALE
  • WINDOW_ANIMATION_SCALE

If they are 0, then animations are disabled.

like image 43
Sebastian Roth Avatar answered Sep 20 '22 17:09

Sebastian Roth