Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent adaptive icon background

Tags:

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> 
like image 912
JoseRivas1998 Avatar asked Dec 08 '17 22:12

JoseRivas1998


People also ask

How do you make an icon background transparent?

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.

Is android roundIcon required?

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.

What is android adaptive icon?

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.

How do I get rid of adaptive icon?

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.


1 Answers

TL;DR Adaptive Icons cannot have transparent Background

Transparent background will be opaque black.

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).

NoAdaptive and Adaptive

like image 103
qtmfld Avatar answered Sep 20 '22 18:09

qtmfld