問題背景
最近定制項(xiàng)目中菌瘫,客戶反饋?zhàn)约旱腶pk在收到通知時(shí),狀態(tài)欄圖標(biāo)全是白色底方框圖片硝拧,不能正常顯示用戶代碼里面加載的圖片資源椿疗。
問題原因
這個(gè)是Android N對(duì)通知欄圖標(biāo)處理設(shè)計(jì)如此,處理邏輯如下:
- 首先判斷APK的target version是Android L之后膜蠢,如果是則直接會(huì)主動(dòng)上色
- 如果在Android L之前則判斷圖片是否為Grayscale(灰度圖)堪藐,如果為灰度圖的話也會(huì)有一個(gè)上色的操作.
涉及的代碼有兩塊:
- frameworks/base/core/java/android/app/Notification.java
/**
* Apply any necessariy colors to the small icon
*/
private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
if (colorable) {
contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
PorterDuff.Mode.SRC_ATOP, -1);
}
contentView.setInt(R.id.notification_header, "setOriginalIconColor",
colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
}
colorable決定系統(tǒng)會(huì)不會(huì)主動(dòng)上色,這塊代碼主要影響NotificationHeaderView的圖標(biāo)顯示
- frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationIconAreaController.java
/**
* Applies {@link #mIconTint} to the notification icons.
*/
private void applyNotificationIconsTint() {
for (int i = 0; i < mNotificationIcons.getChildCount(); i++) {
StatusBarIconView v = (StatusBarIconView) mNotificationIcons.getChildAt(i);
boolean isPreL = Boolean.TRUE.equals(v.getTag(R.id.icon_is_pre_L));
boolean colorize = !isPreL || NotificationUtils.isGrayscale(v, mNotificationColorUtil);
if (false) {
v.setImageTintList(ColorStateList.valueOf(
StatusBarIconController.getTint(mTintArea, v, mIconTint)));
}
}
}
這塊代碼主要影響狀態(tài)欄的圖標(biāo)挑围,知道了這兩處的位置就可以Android N進(jìn)行客制化設(shè)置礁竞,比如小米會(huì)提取應(yīng)用的Launcher Icon來做填充