So I have been working on this app for a couple weeks and I started to build the icon. I have Android Studio 3.0.1 and they seem to have changed the way Image Assets are made, now they have adaptive icons. I made an icon with transparent background for my app. Before, I would just change the shape to "none" and there would be no background generated. But now that is not an option unless I go to "legacy" which is useless. The background color does not seem to support transparency. Even if in the ic_launcher.xml
I set the background to a transparent color but the icon still appears with a black background.
Here is my ic_lancher.xml
<?xml version="1.0" encoding="utf-8"?> <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> <background android:drawable="@color/transparent"/> <foreground android:drawable="@mipmap/ic_launcher_foreground"/> </adaptive-icon>
And ic_launcher_round.xml is the same. The @color/transparent
bit is a this:
<color name="transparent">#00000000</color>
Guide to Make Icon with Transparent Background by CanvaChoose a canvas of your choice. Step 2: Go to "Uploads" and click on the "Upload Media" button. Choose the icon you want to edit. Step 3: To remove the background of your icon, click on the "Effects" button and click on the "Background Remover" option.
You must only use the android:roundIcon attribute if you require a different icon asset for circular masks, if for example the branding of your logo relies on a circular shape.
An adaptive icon, or AdaptiveIconDrawable , can display differently depending on individual device capabilities and user theming. Adaptive icons are primarily used by the launcher on the homescreen, but can also be used in shortcuts, the Settings app, sharing dialogs, and the overview screen.
Just go to android > app > src > main > res and delete all the drawable-dpi folders (except for the plain drawable folder), and the mipmap-any folder. Those folders contain the files for the adaptive icon, and without them Android just uses the regular icon.
TL;DR Adaptive Icons cannot have transparent Background
According to the source code of Android 8.0 framework, the <background>
layer is inherently opaque; the framework fills the background with opaque black, as mentioned in the question.
The framework has a class AdaptiveIconDrawable
, which draws adaptive launcher icons. https://developer.android.com/reference/android/graphics/drawable/AdaptiveIconDrawable.html
The element <adaptive-icon>
creates an instance of it:
This class can also be created via XML inflation using
<adaptive-icon>
tag in addition to dynamic creation.
The source code of method draw
in AdaptiveIconDrawable.java
is:
@Override public void draw(Canvas canvas) { if (mLayersBitmap == null) { return; } if (mLayersShader == null) { mCanvas.setBitmap(mLayersBitmap); mCanvas.drawColor(Color.BLACK); for (int i = 0; i < mLayerState.N_CHILDREN; i++) { if (mLayerState.mChildren[i] == null) { continue; } final Drawable dr = mLayerState.mChildren[i].mDrawable; if (dr != null) { dr.draw(mCanvas); } } mLayersShader = new BitmapShader(mLayersBitmap, TileMode.CLAMP, TileMode.CLAMP); mPaint.setShader(mLayersShader); } if (mMaskBitmap != null) { Rect bounds = getBounds(); canvas.drawBitmap(mMaskBitmap, bounds.left, bounds.top, mPaint); } }
In short, a Canvas instance receives a bitmap to draw into, and initially it fills the bitmap with black color. Then the foreground drawable and the background drawable do the job:
mCanvas.setBitmap(mLayersBitmap); // reset mCanvas.drawColor(Color.BLACK); // fill for (int i = 0; i < mLayerState.N_CHILDREN; i++) { // two layers ... final Drawable dr = mLayerState.mChildren[i].mDrawable; ... dr.draw(mCanvas); // draw ... }
The Color.BLACK is opaque:
0xff000000
The method drawColor fills the bitmap with it, using SRC_OVER mode:
Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.
So, the background will be always opaque, even if you set transparent color to the background, as in the screenshot below (from my answer to a similar question).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With