一、简介
private void writeAnimationScaleOption(int which, ListPreference pref, Object newValue) {
try {
float scale = newValue != null ? Float.parseFloat(newValue.toString()) : 1;
mWindowManager.setAnimationScale(which, scale);
updateAnimationScaleValue(which, pref);
} catch (RemoteException e) {
}
}
@Override
public void setAnimationScale(int which, float scale) {
if (!checkCallingPermission(android.Manifest.permission.SET_ANIMATION_SCALE,
"setAnimationScale()")) {
throw new SecurityException("Requires SET_ANIMATION_SCALE permission");
}
scale = fixScale(scale);
switch (which) {
case 0: mWindowAnimationScaleSetting = scale; break;
case 1: mTransitionAnimationScaleSetting = scale; break;
case 2: mAnimatorDurationScaleSetting = scale; break;
}
// Persist setting
mH.sendEmptyMessage(H.PERSIST_ANIMATION_SCALE);
}
3、dump缩放因子值。
利用“adb shell dumpsys window w -a”打印dump信息,从dump信息中可看见如下信息,这三个值对应于Setting中三个缩放因子。
public void setAnimation(Animation anim, long startTime) {
......
mAnimation.scaleCurrentDuration(mService.getWindowAnimationScaleLocked());
......
}
public void setAnimation(Animation anim, int width, int height, boolean skipFirstFrame) {
......
anim.scaleCurrentDuration(mService.getTransitionAnimationScaleLocked());
......
}
很容易看出调用Animation.scaleCurrentDuration()函数来重置动画时长为duration*scale。
public static IWindowManager getWindowManagerService() {
synchronized (WindowManagerGlobal.class) {
if (sWindowManagerService == null) {
sWindowManagerService = IWindowManager.Stub.asInterface(
ServiceManager.getService("window"));
try {
sWindowManagerService = getWindowManagerService();
ValueAnimator.setDurationScale(sWindowManagerService.getCurrentAnimatorScale());
} catch (RemoteException e) {
Log.e(TAG, "Failed to get WindowManagerService, cannot set animator scale", e);
}
}
return sWindowManagerService;
}
}
public static IWindowSession getWindowSession() {
synchronized (WindowManagerGlobal.class) {
if (sWindowSession == null) {
try {
InputMethodManager imm = InputMethodManager.getInstance();
IWindowManager windowManager = getWindowManagerService();
sWindowSession = windowManager.openSession(
new IWindowSessionCallback.Stub() {
@Override
public void onAnimatorScaleChanged(float scale) {
ValueAnimator.setDurationScale(scale);
}
},
imm.getClient(), imm.getInputContext());
} catch (RemoteException e) {
Log.e(TAG, "Failed to open window session", e);
}
}
return sWindowSession;
}
}
三、总结
Setting中三个选项是一个动画时长的乘积因子,作用目标分别是Window、Activity、View。对于View来说,如果动画未使用ValueAnimator.sDurationScale做一个乘积计算,那么动画时长自然不受Setting的影响。