Java 如何使Android应用程序的图标背景透明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50960640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:54:11  来源:igfitidea点击:

How to make the icon background of an Android app transparent?

javaandroidxmlimageandroid-studio

提问by apsommer

A PNG image is to be used as the icon foreground, and the background should be completely transparent.

PNG 图像将用作图标前景,背景应完全透明。

Using Android Studio > New Image Asset > Configure Image Asset dialogin the Asset Studio, virtually very combination of options for the background has been tried, including removing the file entirely from the path and deleting the background tags in ic_launcher.xmland ic_launcher_round.xml.

使用Android Studio > New Image Asset > Configure Image Asset dialog在资产工作室为背景选项实际上很组合都试过,包括完全从路径删除文件和删除后台标签ic_launcher.xmlic_launcher_round.xml

Removing the file from the path in this wizard and removing the tags from the XML files almost does the trick, but a faint black outline remains as the background (~alpha=20%). All other options for the Background Layer leave either a stark white or black background behind the foreground image when viewing the icon on the device's launcher.

从该向导中的路径中删除文件并从 XML 文件中删除标签几乎可以解决问题,但背景仍保留一个微弱的黑色轮廓 (~alpha=20%)。查看设备启动器上的图标时,背景层的所有其他选项都会在前景图像后面留下纯白色或黑色背景。

Many apps manage to have a foreground only on their icons, so it is possible. What am I doing wrong here?

许多应用程序设法仅在其图标上有一个前景,因此这是可能的。我在这里做错了什么?

Update: Thank you for the quick comments! I tried both the solutions provided as answers, same result. Screen capture is uploaded as suggested.

更新:感谢您的快速评论!我尝试了作为答案提供的两种解决方案,结果相同。按照建议上传屏幕截图。

Updating just the Legacy icon in the wizard is not sufficient, as that only changes the Legacy icon format. The newer "Adaptive" icons are required as well. Even if the background is set to a completely transparent square png, Android Studio changes it to white. If the background file is removed entirely, the wizard allows completion, but then Android Studio uses a slightly opaque black background, as shown. With respect to the uploaded image, there should be nothing behind the foreground, completely gray to in this case without any visible circle.

仅更新向导中的 Legacy 图标是不够的,因为这只会更改 Legacy 图标格式。还需要较新的“自适应”图标。即使背景设置为完全透明的方形 png,Android Studio 也会将其更改为白色。如果背景文件被完全删除,向导允许完成,但 Android Studio 会使用稍微不透明的黑色背景,如图所示。对于上传的图像,前景后面应该没有任何东西,在这种情况下完全灰色到没有任何可见的圆圈。

Screen capture from device running API 25

来自运行 API 25 的设备的屏幕截图

采纳答案by Amir Dora

Method 1:

方法一:

if you have a png, just copy it and paste it in drawable, and refer to image name from manifest.

如果您有 png,只需将其复制并粘贴到可绘制对象中,然后从清单中引用图像名称。

<application
        android:allowBackup="true"
        android:icon="@mipmap/new_image"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/new_image"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        ....
        </application>

Method 2:

方法二:

Goto file > new > image asset, make sure you select a png image (with transparent background) & set background shape to none.

转到文件 > 新建 > 图像资产,确保选择一个 png 图像(具有透明背景)并将背景形状设置为无。

enter image description here

在此处输入图片说明

回答by Mitesh Sawant

First you have a background image to get transparent icon without any background image you can't get your icon transparent .

首先你有一个背景图片来获得透明图标,没有任何背景图片你不能让你的图标透明。

And if you look to get image transparent you simply use some photo editor.

如果您希望图像透明,您只需使用一些照片编辑器。

回答by Paul

Get a view of the imageButton

获取 imageButton 的视图

View v = findViewById(R.id.myImageBtn);

get the icon from the app for example Chrome

从应用程序中获取图标,例如 Chrome

pkgnam="com.android.chrome";
Intent intent1 = ctx.getPackageManager().getLaunchIntentForPackage(pgknam);
Drawable myIcon = getPackageManager().getActivityIcon(intent1);

set the background icon the same as the foreground icon

将背景图标设置为与前景图标相同

v.setForeground(myIcon);
v.setBackground(myIcon);