Android版本: 8.1
切换桌面的时候我们可以看到,状态栏的数字和icon图标都变成颜色,假如我们开发的时候也要添加一个新的view到状态栏,就需要实现同样的方法。
实现这个功能,需要实现DarkReceiver这个接口。
interface DarkReceiver {
void onDarkChanged(Rect area, float darkIntensity, int tint);
}
onDarkChanged就是在当背景色改变时,对应发生的监听回调。
下面开始看源码是怎么操作的
当Window 界面切换的时候,会调用setSystemUiVisibility
传到StatusBar里,
@Override // CommandQueue
public void setSystemUiVisibility(int vis, int fullscreenStackVis, int dockedStackVis,
int mask, Rect fullscreenStackBounds, Rect dockedStackBounds) {
。。。//最后调用了mLightBarController
mLightBarController.onSystemUiVisibilityChanged(fullscreenStackVis, dockedStackVis,
mask, fullscreenStackBounds, dockedStackBounds, sbModeChanged, mStatusBarMode);
}
setSystemUiVisibility最后调用了mLightBarController,这里的mLightBarController是LightBarController
onSystemUiVisibilityChanged
public void onSystemUiVisibilityChanged(int fullscreenStackVis, int dockedStackVis,
int mask, Rect fullscreenStackBounds, Rect dockedStackBounds, boolean sbModeChanged,
int statusBarMode) {
int oldFullscreen = mFullscreenStackVisibility;
int newFullscreen = (oldFullscreen & ~mask) | (fullscreenStackVis & mask);
int diffFullscreen = newFullscreen ^ oldFullscreen;
int oldDocked = mDockedStackVisibility;
int newDocked = (oldDocked & ~mask) | (dockedStackVis & mask);
int diffDocked = newDocked ^ oldDocked;
if ((diffFullscreen & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0
|| (diffDocked & View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) != 0
|| sbModeChanged
|| !mLastFullscreenBounds.equals(fullscreenStackBounds)
|| !mLastDockedBounds.equals(dockedStackBounds)) {
mFullscreenLight = isLight(newFullscreen, statusBarMode,
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
mDockedLight = isLight(newDocked, statusBarMode, View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
updateStatus(fullscreenStackBounds, dockedStackBounds);
}
mFullscreenStackVisibility = newFullscreen;
mDockedStackVisibility = newDocked;
mLastStatusBarMode = statusBarMode;
mLastFullscreenBounds.set(fullscreenStackBounds);
mLastDockedBounds.set(dockedStackBounds);
}
如果参数比较有改变,则会走向updateStatus
private void updateStatus(Rect fullscreenStackBounds, Rect dockedStackBounds) {
boolean hasDockedStack = !dockedStackBounds.isEmpty();
// If both are light or fullscreen is light and there is no docked stack, all icons get
// dark.
if ((mFullscreenLight && mDockedLight) || (mFullscreenLight && !hasDockedStack)) {
mStatusBarIconController.setIconsDarkArea(null);
mStatusBarIconController.getTransitionsController().setIconsDark(true, animateChange());
}
// If no one is light or the fullscreen is not light and there is no docked stack,
// all icons become white.
else if ((!mFullscreenLight && !mDockedLight) || (!mFullscreenLight && !hasDockedStack)) {
mStatusBarIconController.getTransitionsController().setIconsDark(
false, animateChange());
}
// Not the same for every stack, magic!
else {
Rect bounds = mFullscreenLight ? fullscreenStackBounds : dockedStackBounds;
if (bounds.isEmpty()) {
mStatusBarIconController.setIconsDarkArea(null);
} else {
mStatusBarIconController.setIconsDarkArea(bounds);
}
mStatusBarIconController.getTransitionsController().setIconsDark(true, animateChange());
}
}
最终都会走向mStatusBarIconController。
private final DarkIconDispatcher mStatusBarIconController;
这里的DarkIconDispatcher是个接口,实际对象是他的继承者DarkIconDispatcherImpl
这里的setIconsDarkArea
public void setIconsDarkArea(Rect darkArea) {
if (darkArea == null && mTintArea.isEmpty()) {
return;
}
if (darkArea == null) {
mTintArea.setEmpty();
} else {
mTintArea.set(darkArea);
}
applyIconTint();
}
private void applyIconTint() {
for (int i = 0; i < mReceivers.size(); i++) {
mReceivers.valueAt(i).onDarkChanged(mTintArea, mDarkIntensity, mIconTint);
}
}
然后就是更新所有Receivers,通知onDarkChanged。
前面getTransitionsController,
public LightBarTransitionsController getTransitionsController() {
return mTransitionsController;
}
LightBarTransitionsController
mTransitionsController = new LightBarTransitionsController(context,
this::setIconTintInternal);
最终调用的setIconTintInternal,
private void setIconTintInternal(float darkIntensity) {
mDarkIntensity = darkIntensity;
mIconTint = (int) ArgbEvaluator.getInstance().evaluate(darkIntensity,
mLightModeIconColorSingleTone, mDarkModeIconColorSingleTone);
applyIconTint();
}
也是通知其他Receivers去更新。
接着我们看看所有的mReceivers。
public void addDarkReceiver(DarkReceiver receiver) {
mReceivers.put(receiver, receiver);
receiver.onDarkChanged(mTintArea, mDarkIntensity, mIconTint);
}
所有Receivers都是调用addDarkReceiver实现的。
分别有
对应着状态栏里所有的图标显示。
比如,当回调的时候,看看mBattery,
public class BatteryMeterView extends LinearLayout implements
BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener
BatteryMeterView 实现了DarkReceiver接口,
所以注册addDarkReceiver之后,回调的时候会调用BatteryMeterView 的onDarkChanged
@Override
public void onDarkChanged(Rect area, float darkIntensity, int tint) {
mDarkIntensity = darkIntensity;
float intensity = DarkIconDispatcher.isInArea(area, this) ? darkIntensity : 0;
int foreground = getColorForDarkIntensity(intensity, mLightModeFillColor,
mDarkModeFillColor);
int background = getColorForDarkIntensity(intensity, mLightModeBackgroundColor,
mDarkModeBackgroundColor);
mDrawable.setColors(foreground, background);
setTextColor(foreground);
}
最后切换图标染色,实现不同的颜色转换。
假如我们自己开发需要加入状态栏图标,随背景转换颜色,需要自定义view里实现DarkReceiver这个接口,然后实现onDarkChanged,在这里更改颜色背景即可。