美文网首页
android 12 launcher icon背景透明

android 12 launcher icon背景透明

作者: gale_小米 | 来源:发表于2022-08-18 11:53 被阅读0次

    客户需求要求launcher上面的应用icon为背景透明的,Android sdk26后应用icon使用的是adaptive-icon图标;

    Android 11 上没有做严格的验证,直接在解析 background 属性时continue就可以了;

    Index: frameworks/base/graphics/java/android/graphics/drawable/AdaptiveIconDrawable.java
    ===================================================================  处理ICON图标透明
    --- frameworks/base/graphics/java/android/graphics/drawable/AdaptiveIconDrawable.java   (revision 21510)
    +++ frameworks/base/graphics/java/android/graphics/drawable/AdaptiveIconDrawable.java   (working copy)
    @@ -358,7 +358,8 @@
             }
             if (mLayersShader == null) {
                 mCanvas.setBitmap(mLayersBitmap);
    -            mCanvas.drawColor(Color.BLACK);
    +            // mCanvas.drawColor(Color.BLACK);
    +            mCanvas.drawColor(Color.TRANSPARENT);
                 for (int i = 0; i < mLayerState.N_CHILDREN; i++) {
                     if (mLayerState.mChildren[i] == null) {
                         continue;
    @@ -482,7 +483,8 @@
                 String tagName = parser.getName();
                 if (tagName.equals("background")) {
                     childIndex = BACKGROUND_ID;
    +                continue;
                } else  if (tagName.equals("foreground")) {
                     childIndex = FOREGROUND_ID;
                 } else {
                     continue;
    Index: packages/apps/Launcher3/src/com/android/launcher3/folder/PreviewBackground.java
    ===================================================================   处理文件夹图标透明
    --- packages/apps/Launcher3/src/com/android/launcher3/folder/PreviewBackground.java (revision 21510)
    +++ packages/apps/Launcher3/src/com/android/launcher3/folder/PreviewBackground.java (working copy)
    @@ -221,7 +221,8 @@
         }
     
         public int getBgColor() {
    -        int alpha = (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
    +        // int alpha = (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
    +        int alpha = 0;
             return setColorAlphaBound(mBgColor, alpha);
         }
     
    @@ -420,7 +421,8 @@
         }
     
         public int getBackgroundAlpha() {
    -        return (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
    +        // return (int) Math.min(MAX_BG_OPACITY, BG_OPACITY * mColorMultiplier);
    +        return 0;
         }
     
         public float getStrokeWidth() {
    
    

    12上做非空验证,会报空指针NullPointerException

    2022-08-13 15:41:19.899 7604-7604/com.android.launcher3 W/UnsafeUtil: platform method missing - proto runtime falling back to safer methods: java.lang.NoSuchMethodException: sun.misc.Unsafe.copyMemory [class java.lang.Object, long, class java.lang.Object, long, long]
    2022-08-13 15:41:19.907 7604-7623/com.android.launcher3 E/LoaderTask: Desktop items loading interrupted
        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.ColorDrawable.setColor(int)' on a null object reference
            at com.android.launcher3.icons.BaseIconFactory.normalizeAndWrapToAdaptiveIcon(BaseIconFactory.java:312)
            at com.android.launcher3.icons.BaseIconFactory.createBadgedIconBitmap(BaseIconFactory.java:217)
            at com.android.launcher3.icons.BaseIconFactory.createBadgedIconBitmap(BaseIconFactory.java:192)
            at com.android.launcher3.icons.BaseIconFactory.createBadgedIconBitmap(BaseIconFactory.java:185)
            at com.android.launcher3.icons.BaseIconFactory.createBadgedIconBitmap(BaseIconFactory.java:180)
            at com.android.launcher3.icons.LauncherActivityCachingLogic.loadIcon(LauncherActivityCachingLogic.java:60)
            at com.android.launcher3.icons.LauncherActivityCachingLogic.loadIcon(LauncherActivityCachingLogic.java:31)
            at com.android.launcher3.icons.cache.BaseIconCache.cacheLocked(BaseIconCache.java:327)
            at com.android.launcher3.icons.IconCache.getTitleAndIcon(IconCache.java:300)
            at com.android.launcher3.icons.IconCache.getTitleAndIcon(IconCache.java:194)
            at com.android.launcher3.model.LoaderCursor.getAppShortcutInfo(LoaderCursor.java:291)
            at com.android.launcher3.model.LoaderTask.loadWorkspace(LoaderTask.java:538)
            at com.android.launcher3.model.LoaderTask.loadWorkspace(LoaderTask.java:313)
            at com.android.launcher3.model.LoaderTask.run(LoaderTask.java:197)
            at android.os.Handler.handleCallback(Handler.java:938)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loopOnce(Looper.java:201)
            at android.os.Looper.loop(Looper.java:288)
            at android.os.HandlerThread.run(HandlerThread.java:67)
    

    所以不在解析的时候过滤,而是在draw()中做下过滤不绘制background;

    Index: java/android/graphics/drawable/AdaptiveIconDrawable.java
         @Override
    @@ -358,9 +359,10 @@
             }
             if (mLayersShader == null) {
                 mCanvas.setBitmap(mLayersBitmap);
    -            mCanvas.drawColor(Color.BLACK);
    +            //mCanvas.drawColor(Color.BLACK);
    +            mCanvas.drawColor(Color.TRANSPARENT);
                 for (int i = 0; i < mLayerState.N_CHILDREN; i++) {
    -                if (mLayerState.mChildren[i] == null) {
    +                if (mLayerState.mChildren[i] == null || i== BACKGROUND_ID) {
                         continue;
                     }
                     final Drawable dr = mLayerState.mChildren[i].mDrawable;
    

    相关文章

      网友评论

          本文标题:android 12 launcher icon背景透明

          本文链接:https://www.haomeiwen.com/subject/bbvkgrtx.html