开发者

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

I know, I can start the Settings-Activity with

Intent intent = new Intent(Settings.ACTION_DISPLAY_SETTINGS);
startActiv开发者_开发知识库ityForResult(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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜