android Launcher3中定制第三方apk圖標,實現類似主題功能

最近遇到在Launcher中對于指定的app需使用使用指定的圖標,實現類似于主題的功能。要實現這樣的功能就要弄清楚Launcher是如何加載app圖標的。

首先定位到com.android.launcher3.IconCache.java這個類的cacheLocked()方法

private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
            HashMap<Object, CharSequence> labelCache, UserHandleCompat user, boolean usePackageIcon) {
        CacheKey cacheKey = new CacheKey(componentName, user);
        CacheEntry entry = mCache.get(cacheKey);
        if (entry == null) {
            entry = new CacheEntry();
        .......
                entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
                //用于初始化應用圖標
                entry.icon = Utilities.createIconBitmap(
                        info.getBadgedIcon(mIconDpi), mContext);
            } else {
        ......
        return entry;
    }

首先通過通過info.getBadgedIcon(mIconDpi)來獲取應用原始launcher圖標的Drawable,mIconDpi為圖圖片資源id,然后通過Utilities.createIconBitmap(Drawable icon, Context context)方法來對圖標進行簡單的處理(要修改所有Launcher圖標的背景等,可以在此方法中修改)。

其中info.getBadgedIcon(mIconDpi)就是決定應用圖標的方法。該方法最終會進入com.Android.launcher3.compat.LauncherActivityInfoCompatV16.Java類的getBadgedIcon()–>getIcon();通過Resources來獲取圖標的。 我采用的方法,首先自定義一個類,LauncherIconTheme.java,該類的內容如下

package com.android.launcher3;

import com.android.launcher3.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;

public final  class LauncherIconTheme {
    //google郵箱
    private static int GMAIL = R.drawable.ic_launcher_gmail;
    //google地圖
    private static int GOOGLE_MAPS = R.drawable.ic_launcher_googlemaps;
    //自定義的應用資源id
    ...................

    private static String TAG = "LauncherIconTheme";

        //根據包名、類名獲取Bitmap
    public static Bitmap  getIconBitmap(Context context , String packageName , String className) {
        Resources resources = context.getResources();
        int iconId = getIconId(packageName, className);
        if (iconId != -1){
            return BitmapFactory.decodeResource(resources, iconId);
        }
        return null;
    }
        //根據包名、類名獲取Drawable   要用到的就是這個方法
    public static Drawable getIconDrawable(Context context , String packageName , String className) {
        Resources resources = context.getResources();
        int iconId = getIconId(packageName, className);
        if ( iconId != -1) {
            return resources.getDrawable(iconId);
        }
        return null;
    }
        //根據包名、類名獲取資源定義的圖標資源id
    private static int getIconId(String packageName , String className){
        if ( "com.google.android.gm".equals(packageName)
                && "com.google.android.gm.ConversationListActivityGmail".equals(className)) {
            return GMAIL;

        }else if ("com.google.android.apps.maps".equals(packageName)
                && "com.google.android.maps.MapsActivity".equals(className)) {
            return GOOGLE_MAPS;

        }else if ....................
            .................................
        }else{
            return -1;          
        }
    }   
}

然后在IconCache的cacheLocked方法中

 /**
     * Retrieves the entry from the cache. If the entry is not present, it creates a new entry.
     * This method is not thread safe, it must be called from a synchronized method.
     */
    private CacheEntry cacheLocked(ComponentName componentName, LauncherActivityInfoCompat info,
            HashMap<Object, CharSequence> labelCache, UserHandleCompat user, boolean usePackageIcon) {
        CacheKey cacheKey = new CacheKey(componentName, user);
        CacheEntry entry = mCache.get(cacheKey);
        if (entry == null) {
           .................................

                entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, user);
                //實現修改圖標的邏輯
                Drawable themeDrawable = LauncherIconTheme
                        .getIconDrawable(mContext,
                                componentName.getPackageName(), componentName.getClassName());
                if ( themeDrawable != null ) {
                    entry.icon = Utilities.createIconBitmap(
                            themeDrawable, mContext);
                }else{
                    entry.icon = Utilities.createIconBitmap(
                            info.getBadgedIcon(mIconDpi), mContext);                    
                }
            } else {
             ...........................................
        return entry;
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容