Android Studio图像资产启动器图标透明背景色 [英] Android Studio Image Asset Launcher Icon Transparent Background Color

查看:1076
本文介绍了Android Studio图像资产启动器图标透明背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能会认为此问题与 ,具体取决于其包名称 com.google.android.launcher 。背景颜色可以是透明的,如Update#1的屏幕截图所示。



在Nexus 5模拟器(Android 8.1)中



默认启动器应用是应用为封闭源:




  • com.google.android.launcher Google Now Launcher

  • com.google.android.apps.nexuslauncher 像素启动器



相反, AOSP应用是开源的。 Android中的大多数启动器应用程序都基于以下启动器应用程序的源代码:




  • com.android.launcher


  • com.android.launcher2


    • Launcher2 (原始软件包名称)


  • com.android.launcher3


    • Launcher3 (适用于Android 5.0 Lollipop或更高版本)




Launcher3源代码



在git分支中 oreo -Launch LauncherIcons.java 具有方法 wrapToAdaptiveIconDrawable ,该方法将旧版启动器图标包装在自适应启动器图标中。

  / ** 
*如果平台运行的是O,但应用程序未提供AdaptiveIconDrawable,则
*缩小旧图标并将其设置为前景。将颜色可绘制背景用作
*创建AdaptiveIconDrawable的背景。
* /
static Drawable wrapToAdaptiveIconDrawable(Context context,Drawable drawable,float scale){
if(!(FeatureFlags.LEGACY_ICON_TREATMENT&& Utilities.isAtLeastO())){
返回可绘制;
}
试试{
if(!(AdaptiveIconDrawable的可绘制实例)){
AdaptiveIconDrawable iconWrapper =(AdaptiveIconDrawable)
context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper)。 mutate();
FixedScaleDrawable fsd =(((FixedScaleDrawable)iconWrapper.getForeground());
fsd.setDrawable(drawable);
fsd.setScale(scale);
return(Drawable)iconWrapper;
}
} catch(Exception e){
return drawable;
}
可退回款项;
}

标志 FeatureFlags.LEGACY_ICON_TREATMENT FeatureFlags.java

  //启用后,图标不支持{@link AdaptiveIconDrawable }将被包装在此类中。 
public static final boolean LEGACY_ICON_TREATMENT = true;

因此,旧版启动器图标的背景颜色取决于此标志,在某些情况下它可能是不透明的启动应用程序,例如 Pixel Launcher



背景色



如果标志设置为 true ,使用 R.drawable.adaptive_icon_drawable_wrapper 和现有的旧版图标创建一个新的自适应启动器图标成为其前景层。根据 @ color / legacy_icon_background + / oreo-release / res / drawable-v26 / adaptive_icon_drawable_wrapper.xml rel = noreferrer>资源XML文件

 < adaptive-icon xmlns:android = http://schemas.android.com/apk/res/android> 
< background android:drawable = @ color / legacy_icon_background />
<前景>
< com.android.launcher3.graphics.FixedScaleDrawable />
< / foreground>
< / adaptive-icon>

中定义了颜色 legacy_icon_background href = https://android.googlesource.com/platform/packages/apps/Launcher3/+/oreo-release/res/values/colors.xml#50 rel = noreferrer> colors.xml

 < color name = legacy_icon_background>#FFFFFF< / color> 

因此,背景颜色变为白色。


You might be thinking this question is a duplicate of this one. But since then, Android Studio has been updated and the solution given there is not working anymore.

I am trying to set my app logo using image asset in android studio. This is because if I put my app logo directly in drawable or mipmap, then it causes many problems like: If it's size is big then app crash occurs, if device running on oreo then logo will not showed and default ic_launcher is showing etc.

While trying to set my app's logo using image asset, I am facing a problem: I can't keep app logo's background transparent.

I have a png logo made in photoshop and I want to set this as my app logo and I don't want any background but android studio image asset doesn't provide any option to remove background. I tried following solutions from google:

this and this

but none of these worked for me.

Tried solutions:

  1. By setting shape to none
  2. by Deleting ic_launcher_background file from it's default location
  3. Tried in different devices

None of these works...please help me. Any help will be appreciated.

解决方案

Android 8.0 Oreo (API level 26) introduced adaptive launcher icons, which consist of two layers: a foreground and a background. The material design guidelines state that the background layer must be opaque in Android O and beyond, as you can see at the bottom of the following citation. So, the adaptive launcher icon for Android 8.0 or higher must have opaque background color at least, if targetSdkVersion of your app is 26 or higher.

https://material.io/guidelines/style/icons.html#icons-icons-for-android

Icons for Android

Android O and beyond

Android O icons represent your app on a device's Home and All Apps screens. The following guidelines describe how icons can receive unique visual treatments, animations, and behaviors.

...

Layer specs

Icons consist of two layers: a foreground and a background. Each layer can animate and receive treatments independently from the other layer.

Foreground (scrolling parallax)

  • 108 x 108 dp
  • 72dp masked section
  • Transparency recommended (optional)

Background (subtler parallax)

  • 108 X 108 dp
  • 72dp masked section
  • Must be opaque


Workaround for 7.1 or lower

Although launcher icons for 8.0 or higher must have opaque background color, the other legacy launcher icons for 7.1 or lower can revert to transparent background color, if you can omit round launcher icons from your app.

  1. First, create Launcher Icons (Adaptive and Legacy). It will create adaptive launcher icons and legacy launcher icons. All of them have opaque background color, at first.
  2. After that, create Launcher Icons (Legacy only). This will overwrite only the existing legacy launcher icons, as you can see in the second screenshot below. If you set the shape to none, they will have transparent background color.
  3. Delete folder res/mipmap/ic_laucher_round in the project window.
  4. Open AndroidManifest.xml and remove attribute android:roundIcon="@mipmap/ic_launcher_round" from the application element.

In the left-side pane above, the following XML files define adaptive launcher icons for Android 8.0 or higher.

  • mipmap-anydpi-v26/ic_launcher.xml
  • mipmap-anydpi-v26/ic_launcher_round.xml

As seen in the right-side pane, they refer to the following drawable XML files.

  • drawable/ic_launcher_background.xml
  • drawable-v24/ic_launcher_foreground.xml


Update #1:

In Android 8.0 or higher, the background color of launcher icons can be transparent, as shown in the Android 8.1 (Nexus 5X) screenshots. The sample app "NoAdaptive" is without any resource for the adaptive launcher icons in folder mipmap-anydpi-v26, and the other app "Adaptive" has the resource.


Update #2:

Although the background color of launcher icon can be transparent in Android 8.0 or higher, it depends on user’s launcher app. Some launcher apps will convert your legacy icon to opaque adaptive icon.

In Nexus 5X device (Android 8.1)

The default launcher app is Google Now Launcher, according to its package name com.google.android.launcher. The background color can be transparent, as in the screenshots of Update #1.

In Nexus 5 emulator (Android 8.1)

The default launcher app is Pixel Launcher, according to its package name com.google.android.apps.nexuslauncher. The background color can be transparent in Recents screen, as in the screenshots below:

  • opaque in Home screen
  • transparent in Recents screen
  • opaque in All Apps screen

Opaque white background in some launcher apps

These GMS apps are closed-source:

  • com.google.android.launcher Google Now Launcher
  • com.google.android.apps.nexuslauncher Pixel Launcher

In contrast, AOSP apps are open-source. Most launcher apps in Android are based on the source code of the following launcher apps:

  • com.android.launcher
  • com.android.launcher2
    • Launcher2 (original package name)
  • com.android.launcher3
    • Launcher3 (for Android 5.0 Lollipop or higher)

Launcher3 source code

In the git branch oreo-release of Launcher3, LauncherIcons.java has the method wrapToAdaptiveIconDrawable that wraps legacy launcher icon in adaptive launcher icon.

/**
 * If the platform is running O but the app is not providing AdaptiveIconDrawable, then
 * shrink the legacy icon and set it as foreground. Use color drawable as background to
 * create AdaptiveIconDrawable.
 */
static Drawable wrapToAdaptiveIconDrawable(Context context, Drawable drawable, float scale) {
    if (!(FeatureFlags.LEGACY_ICON_TREATMENT && Utilities.isAtLeastO())) {
        return drawable;
    }
    try {
        if (!(drawable instanceof AdaptiveIconDrawable)) {
            AdaptiveIconDrawable iconWrapper = (AdaptiveIconDrawable)
                    context.getDrawable(R.drawable.adaptive_icon_drawable_wrapper).mutate();
            FixedScaleDrawable fsd = ((FixedScaleDrawable) iconWrapper.getForeground());
            fsd.setDrawable(drawable);
            fsd.setScale(scale);
            return (Drawable) iconWrapper;
        }
    } catch (Exception e) {
        return drawable;
    }
    return drawable;
}

The flag FeatureFlags.LEGACY_ICON_TREATMENT is defined in FeatureFlags.java:

// When enabled, icons not supporting {@link AdaptiveIconDrawable} will be wrapped in this class.
public static final boolean LEGACY_ICON_TREATMENT = true;

So, the background color of legacy launcher icon depends on this flag, and it can be opaque in some launcher apps such as Pixel Launcher.

The background color

If the flag is set to true, a new adaptive launcher icon is created with R.drawable.adaptive_icon_drawable_wrapper, and the existing legacy icon becomes its foreground layer. The background layer is a drawable: @color/legacy_icon_background, according to the resource XML file:

<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@color/legacy_icon_background"/>
    <foreground>
        <com.android.launcher3.graphics.FixedScaleDrawable />
    </foreground>
</adaptive-icon>

The color legacy_icon_background is defined in colors.xml

<color name="legacy_icon_background">#FFFFFF</color>

So, the background color becomes white.

这篇关于Android Studio图像资产启动器图标透明背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆