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;
    }

结束
此方法中只是简单的在Launcher中实现自定义应用图标的功能,有啥不足希望大家指出,共同进步。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值