关于Android8的亮度调节,分析到了DisplayPowerController的updatePowerState()方法
再将亮度值进行“加工”以后,会执行animateScreenBrightness()方法
animateScreenBrightness
private void animateScreenBrightness(int target, int rate) {
if (DEBUG) {
Slog.d(TAG, "Animating brightness: target=" + target +", rate=" + rate);
}
//这里进行亮度值的设置 返回值为true标志着亮度值有变化
if (mScreenBrightnessRampAnimator.animateTo(target, rate)) {
Trace.traceCounter(Trace.TRACE_TAG_POWER, "TargetScreenBrightness", target);
try {
mBatteryStats.noteScreenBrightness(target);//计算耗电量
} catch (RemoteException ex) {
// same process
}
}
}
这里关于RampAnimator.animateTo(target, rate)方法
animateTo
public boolean animateTo(int target, int rate) {
// Immediately jump to the target the first time.
//第一次运行的时候 直接设置亮度值 没有动画
if (mFirstTime || rate <= 0) {
if (mFirstTime || target != mCurrentValue) {
mFirstTime = false;//除了第一次创建这个类之外 这个在调用这个方法之后都是false 接下来只会在rate<=0的时候进来这个判断
mRate = 0;//设置为0 直接设置亮度
mTargetValue = target;
mCurrentValue = target;
mProperty.setValue(mObject, target);//设置亮度值的方法
if (mAnimating) {
mAnimating = false;
cancelAnimationCallback();
}
if (mListener != null) {
mListener.onAnimationEnd();
}
return true;
}
return false;
}
// Adjust the rate based on the closest target.
// If a faster rate is specified, then use the new rate so that we converge
// more rapidly based on the new request.
// If a slower rate is specified, then use the new rate only if the current
// value is somewhere in between the new and the old target meaning that
// we will be ramping in a different direction to get there.
// Otherwise, continue at the previous rate.
if (!mAnimating
|| rate > mRate
|| (target <= mCurrentValue && mCurrentValue <= mTargetValue)
|| (mTargetValue <= mCurrentValue && mCurrentValue <= target)) {
mRate = rate;
}
final boolean changed = (mTargetValue != target);//true表示有变化
mTargetValue = target;//设置目标亮度
// Start animating.
if (!mAnimating && target != mCurrentValue) {
//调整亮度
mAnimating = true;
mAnimatedValue = mCurrentValue;
mLastFrameTimeNanos = System.nanoTime();
postAnimationCallback();
}
return changed;
}
在这里,第一次运行的时候,直接设置亮度,没有动画;后面在对亮度进行设置时首先判断是否需要动画,如果需要设置是缓慢变化还是快速变化;然后调用 postAnimationCallback()方法
private void postAnimationCallback() {
mChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, mAnimationCallback, null);
}
然后看一下执行亮度调节的mAnimationCallback
private final Runnable mAnimationCallback = new Runnable() {
@Override // Choreographer callback
public void run() {
final long frameTimeNanos = mChoreographer.getFrameTimeNanos()