如何客制化Launcher的主菜单图标(转)
[Description]
在Launcher上,为了保持手机UI风格的统一,希望把用户自己安装的第三方apk图标或者系统预置的应用图标用特定的图标来显示,如何修改?
[Solution]
1. 请修改AppInfo.java的构造函数,如下:
public AppInfo(Context context, LauncherActivityInfoCompat info, UserHandleCompat user, IconCache iconCache) {
this.componentName = info.getComponentName();
this.container = ItemInfo.NO_ID;
flags = initFlags(info);
firstInstallTime = info.getFirstInstallTime();
iconCache.getTitleAndIcon(this, info, true /* useLowResIcon */);
//mtk add
if ( (info.getApplicationInfo().flags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) != 0) {
this.iconBitmap=Bitmap.createBitmap(72, 72, Bitmap.Config.ARGB_8888); // 具体方法可以自行决定,这里只是例子
}
//mtk add
intent = makeLaunchIntent(context, info, user);
this.user = user;
}
2. 如果是用户安装的第三方app,请修改AllAppsList.java,修改updatePackage方法如下:
......
AppInfo applicationInfo = findApplicationInfoLocked( info.getComponentName().getPackageName(), user, info.getComponentName().getClassName());
if (applicationInfo == null) {
add(new AppInfo(context, info, user, mIconCache));
} else {
mIconCache.getTitleAndIcon(applicationInfo, info, true /* useLowResIcon */);
//mtk add
在此处添加修改applicationInfo.iconBitmap的代码(同上)
//mtk add
modified.add(applicationInfo);
}
......
3. 如果是系统预置的某个应用,请修改IconCache.java文件的cacheLocked方法,将
entry.icon = Utilities.createBadgedIconBitmap(info.getIcon(mIconDpi), info.getUser(), mContext);
修改为客制化的图标
举例如下:
if("com.android.contacts".equals(componentName.getPackageName())) {
Drawable drawable = getFullResIcon(mContext.getResources(), R.drawable.ic_hw_allbackup);
entry.icon = Utilities.createBadgedIconBitmap(drawable, info.getUser(), mContext);
} else {
entry.icon = Utilities.createBadgedIconBitmap(info.getIcon(mIconDpi), info.getUser(), mContext);
}
PS:72/72表示主菜单Icon的图标大小,color format是ARGB8888。这个设置默认把用户自己安装的apk Icon刷成黑色。createBitmap这个方法有多种重载方式,用户可以根据实际需求,通过方法参数(颜色/bitmap/图片资源id等)来构造自己的主菜单Icon风格。