因此,我已经在这个应用程序上工作了几个星期,并开始构建图标。我有 Android Studio 3.0.1,它们似乎改变了图像 Assets 的制作方式,现在它们具有自适应图标。我为我的应用制作了一个透明背景的图标。以前,我只是将形状更改为“无”,并且不会生成任何背景。但现在这不是一个选择,除非我去“遗产”,这是无用的。背景色好像不支持透明。即使在 ic_launcher.xml
中我将背景设置为透明色,但图标仍然显示为黑色背景。
这是我的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>
和ic_launcher_round.xml是一样的。 @color/transparent
位是这样的:
<color name="transparent">#00000000</color>
最佳答案
TL;DR 自适应图标不能有透明背景
透明背景将是不透明的黑色。
根据Android 8.0框架源码,<background>
图层本质上是不透明的;如问题中所述,框架用不透明的黑色填充背景。
框架有一个类 AdaptiveIconDrawable
,绘制自适应启动器图标。
https://developer.android.com/reference/android/graphics/drawable/AdaptiveIconDrawable.html
元素 <adaptive-icon>
创建它的一个实例:
This class can also be created via XML inflation using
<adaptive-icon>
tag in addition to dynamic creation.
The source code方法draw
在AdaptiveIconDrawable.java
是:
@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);
}
}
简而言之,一个Canvas实例接收要绘制的位图,最初它用黑色填充位图。然后前景可绘制对象和背景可绘制对象完成工作:
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
...
}
Color.BLACK是不透明的:
0xff000000
方法drawColor用它填充位图,使用 SRC_OVER模式:
Fill the entire canvas' bitmap (restricted to the current clip) with the specified color, using srcover porterduff mode.
因此,背景将始终是不透明的,即使您将背景设置为透明色,如下面的屏幕截图(来自 my answer 的类似问题)。
关于android - 透明自适应图标背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47722958/