Android 8.1 去掉 Launcher3 默认给 icon 增加的白边

3,326 阅读1分钟

前言

如果你还不知道 icon 白边是什么的话,请移步这篇 Android应用图标微技巧,8.0系统中应用图标的适配 看一下

为了避免 app 图标适配的工作,手里有源码就是爽,我们可以修改源码把这个白边去掉,如果你没有源码就老老实实按照

上面博客的方法进行适配吧。

在这里插入图片描述
修改前效果

在这里插入图片描述

修改后效果

思路

从上文的博客介绍可以得知只要 app 的 AndroidManifest.xml 中 targetSdkVersion 的值 >= 26,如果你没进行适配,Launcher3

就会给你的 app 图标增加一个默认的白色遮罩。 targetSdkVersion 的值 < 26, 则使用原来的图标。

好了,那么在 Launcher3 中必定有判断 targetSdkVersion 的代码,26 对应的变量为 Build.VERSION_CODES.O

通过全局搜索会有很多相关的,过滤我们需要的

packages\apps\Launcher3\src\com\android\launcher3\graphics\LauncherIcons.java

修改方法如下

可以看到 进行了双重判断,当前编译系统的 SDK 版本是否是 23 和 iconapp 的 targetSdk 版本是否大于 23

将 if 部分直接注释,默认走 else 部分,不添加白边

/**
     * Returns a bitmap suitable for the all apps view. The icon is badged for {@param user}.
     * The bitmap is also visually normalized with other icons.
     */
    public static Bitmap createBadgedIconBitmap(
            Drawable icon, UserHandle user, Context context, int iconAppTargetSdk) {

        IconNormalizer normalizer;
        float scale = 1f;
        if (!FeatureFlags.LAUNCHER3_DISABLE_ICON_NORMALIZATION) {
            normalizer = IconNormalizer.getInstance(context);
            
            //annotation for don't add white mask outshape when targetsdk >= 26
            /*if (Utilities.ATLEAST_OREO && iconAppTargetSdk >= Build.VERSION_CODES.O) {
                boolean[] outShape = new boolean[1];
                AdaptiveIconDrawable dr = (AdaptiveIconDrawable)
                        context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper).mutate();
                dr.setBounds(0, 0, 1, 1);
                scale = normalizer.getScale(icon, null, dr.getIconMask(), outShape);
                if (FeatureFlags.LEGACY_ICON_TREATMENT &&
                        !outShape[0]){
                    Drawable wrappedIcon = wrapToAdaptiveIconDrawable(context, icon, scale);
                    if (wrappedIcon != icon) {
                        icon = wrappedIcon;
                        scale = normalizer.getScale(icon, null, null, null);
                    }
                }
            } else {
                scale = normalizer.getScale(icon, null, null, null);
            }*/
            scale = normalizer.getScale(icon, null, null, null);
        }
        Bitmap bitmap = createIconBitmap(icon, context, scale);
        if (FeatureFlags.ADAPTIVE_ICON_SHADOW && Utilities.ATLEAST_OREO &&
                icon instanceof AdaptiveIconDrawable) {
            bitmap = ShadowGenerator.getInstance(context).recreateIcon(bitmap);
        }
        return badgeIconForUser(bitmap, user, context);
    }