透明的自适应图标背景 [英] Transparent adaptive icon background
问题描述
所以我已经在这个应用程序上工作了几个星期,并且我开始构建图标.我有Android Studio 3.0.1,并且它们似乎已经改变了制作图像资产的方式,现在它们具有自适应图标了.我为我的应用制作了一个带有透明背景的图标.在此之前,我只是将形状更改为"none",并且不会生成任何背景.但是,除非我没有用遗产",否则现在这不是一个选择.背景颜色似乎不支持透明性.即使在 ic_launcher.xml
中,我将背景设置为透明颜色,但图标仍显示为黑色背景.
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.
这是我的 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
位是this:
And ic_launcher_round.xml is the same. The @color/transparent
bit is a this:
<color name="transparent">#00000000</color>
推荐答案
TL; DR 自适应图标不能具有透明背景
TL;DR Adaptive Icons cannot have transparent Background
根据Android 8.0框架的源代码,< 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.
该框架有一个 AdaptiveIconDrawable
类,该类绘制了自适应启动器图标. https://developer.android.com/reference/android/graphics/drawable/AdaptiveIconDrawable.html
元素< adaptive-icon>
创建它的一个实例:
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:
除了动态创建之外,还可以使用
< adaptive-icon>
标签通过XML填充来创建此类.
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);
}
}
简而言之, Canvas 实例会收到要绘制的位图,最初它用黑色填充位图.然后前景可绘制对象和背景可绘制对象完成这项工作:
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
...
}
Color.BLACK 是不透明的:
0xff000000
0xff000000
方法 drawColor 填充位图使用 SRC_OVER 模式:
The method drawColor fills the bitmap with it, using SRC_OVER mode:
使用srcover porterduff模式以指定的颜色填充整个画布的位图(仅限于当前剪辑).
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).
这篇关于透明的自适应图标背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!