Android8.1 Launcher3修改app圖示
阿新 • • 發佈:2018-12-03
Android8.1 Launcher3修改app圖示
我們有一個需求是將預設的瀏覽器替換為chrome(谷歌瀏覽器),但是需要將chrome的圖示替換成我們重新設計的圖示:
首先將chrome加入進來:
vendor/mediatek/proprietary/packages/3rd-party/chrome/
這個目錄下建立一個chrome模組,
將apk放到這裡,並新增mk檔案:
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := chrome LOCAL_MODULE_TAGS := optional LOCAL_SRC_FILES := $(LOCAL_MODULE).apk LOCAL_MODULE_CLASS := APPS LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX) LOCAL_CERTIFICATE := PRESIGNED LOCAL_MULTILIB := 32 LOCAL_DEX_PREOPT := ture include $(BUILD_PREBUILT)
然後在device/mediatek/common/device.mk中新增一句:
PRODUCT_PACKAGES += chrome
這樣就能順利的將chrome編譯到我們的專案中來了;然後在mk中找到原生的瀏覽器的定義地方,將他註釋就ok。
下面就是替換chrome的圖示
Launcher中基本關於圖片的處理都在這個類中:
src/com/android/launcher3/IconCache.java
當然圖示的生成和我們要做的替換也是不例外的,共有3個地方:
a.
@Thunk synchronized void addIconToDBAndMemCache(LauncherActivityInfo app, PackageInfo info, long userSerial, boolean replaceExisting) { final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser()); CacheEntry entry = null; if (!replaceExisting) { entry = mCache.get(key); // We can't reuse the entry if the high-res icon is not present. if (entry == null || entry.isLowResIcon || entry.icon == null) { entry = null; } } if (entry == null) { entry = new CacheEntry(); entry.icon = LauncherIcons.createIconBitmap(getFullResIcon(app), mContext); /* begin */ if (app.getComponentName() != null && app.getComponentName().getPackageName() != null && app.getComponentName().getPackageName().equals("com.android.chrome")) { entry.icon = IconUtil.getBitmap(mContext, R.drawable.icon_chrome); } /* end */ } entry.title = app.getLabel(); entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser()); mCache.put(key, entry); Bitmap lowResIcon = generateLowResIcon(entry.icon); ContentValues values = newContentValues(entry.icon, lowResIcon, entry.title.toString(), app.getApplicationInfo().packageName); addIconToDB(values, app.getComponentName(), info, userSerial); }
b.
protected CacheEntry cacheLocked( @NonNull ComponentName componentName, @NonNull Provider<LauncherActivityInfo> infoProvider, UserHandle user, boolean usePackageIcon, boolean useLowResIcon) { ..... /* begin */ if (componentName != null && componentName.getPackageName() != null && componentName.getPackageName().equals("com.android.chrome")) { entry.icon = IconUtil.getBitmap(mContext, R.drawable.icon_chrome); } /* end */ return entry; }
c.
private CacheEntry getEntryForPackageLocked(String packageName, UserHandle user,
boolean useLowResIcon) {
......
/* begin */
if("com.android.chrome".equals(packageName)){
icon = IconUtil.getBitmap(mContext,R.drawable.icon_chrome);
}
/* end */
Bitmap lowResIcon = generateLowResIcon(icon);
entry.title = appInfo.loadLabel(mPackageManager);
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
entry.icon = useLowResIcon ? lowResIcon : icon;
entry.isLowResIcon = useLowResIcon;
......
還有一個getBitmap()方法:
public static Bitmap getBitmap(Context context, int res) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_4444;
return BitmapFactory.decodeResource(context.getResources(), res, options);
}
這樣就大功告成啦!