自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
实现自适应图标
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如果开发者仅提供一个图标素材资源,自适应图标的形状在设备内会保持一致,但在设备之间会有所差异。此外,图标支持两个图层(前景和背景),可用于实现动画效果,从而为用户提供愉悦的视觉体验。
设备实现人员会提供一个设备遮罩,该遮罩将决定设备上所有图标的形状。该图标将在任何使用启动器图标(例如,启动器、概览、设置和分享表单)的系统界面上使用。
示例和源代码
代码示例:
platform/development/samples/AdaptiveIconSample/
开发者文档:
源代码:
platform/frameworks/base/graphics/java/android/graphics/drawable/AdaptiveIconDrawable.java
实现
如需更改平台上图标的形状,请替换 framework/base/core/res/res/values/config.xml
中的一个字符串,如下所示:
<!-- Specifies the path that is used by AdaptiveIconDrawable class to crop launcher icons. -->
<string name="config_icon_mask" translatable="false">"M50,0L100,0 100,100 0,100 0,0z"</string>
该字符串的格式和语法均遵循路径定义的 W3-SVG 标准。Android 矢量可绘制对象也支持 PathData 的这种格式。
这个路径应该是上凸的,且应考虑视图边界范围内的安全区 (66/71 = 91%)。此要求在其中一项 CTS 测试中强制执行。
如果您决定使用圆形作为平台遮罩,请确保也要替换 config_useRoundIcon = true。否则,请将此配置值设置为 false 或不指定此配置值。
自适应图标 API
AdaptiveIconDrawable
类的 API 如下所示:
package android.graphics.drawable;
public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback {
method public Drawable getBackground();
method public Drawable getForeground();
method public Path getIconMask();
method public Region getSafeZone();
method public float getExtraInsetFraction();
method public int getOpacity();
method public void invalidateDrawable(Drawable);
method public void scheduleDrawable(Drawable, Runnable, long);
method public void setAlpha(int);
method public void setColorFilter(ColorFilter);
method public void setOpacity(int);
method public void unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable);
}
public class Icon extends Parceleable {
method public Bitmap createWithAdaptiveBitmap();
}
参考实现
无需执行任何操作,即可在任何系统界面上呈现静态自适应图标。当 PackageManager 返回可绘制对象时,只需将它绑定到 ImageView 即可。这就是图标在 Android O 之前版本的平台上呈现的方式。
关于如何呈现动态动画效果,Launcher3 (platform/packages/apps/Launcher3) 中会提供相应的参考实现来说明如何在 O-MR1 中实现这种效果。
验证
如需验证实现,请在替换其需要的遮罩后,查看图标是否可以在 Launcher3、设置、概览和设置中正确呈现。此外,您还可以在图形 CTS TestCase 中运行 AdaptiveIconDrawableTest.java 和 AdaptiveIconMaskTest.java 来测试实现。
可以在以下位置找到推荐的手动测试用例:platform/development/samples/AdaptiveIconSample/。
已知问题
已知问题包括:
- 图标模糊(取决于遮罩路径的定义方式)。
- 快捷方式图标被放大(如果应用开发者未使用
Icon.createWithAdaptiveBitmap()
方法或未正确使用此方法)。为使此方法正常发挥作用,如果图标是以位图形式传递的,则图标的四边应各保留 25% 的内边距。
这些问题可以通过以下方式解决:
- 应在 [0,100] x [0,100] 坐标系中定义遮罩。
- 确保用于自适应图标(启动器图标、快捷方式)的图片在四侧均保留了充足的内边距 (25%)。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Implement adaptive icons\n\nAdaptive Icons maintain a consistent shape intra-device but vary from device to\ndevice with only one icon asset provided by the developer. Additionally, icons\nsupport two layers (foreground and background) that can be used for motion to\nprovide visual delight to users.\n\n\nDevice implementers provide a device mask that will decide the shape of all icons on a\ndevice. This icon will be used on any system UI surfaces that use Launcher Icons\n(e.g., launcher, overview, settings and share sheet).\n\nExamples and source\n-------------------\n\n\nCode examples:\n\n- `platform/development/samples/AdaptiveIconSample/`\n\n\nDeveloper documentation:\n\n- [Adaptive icons](https://developer.android.com/guide/practices/ui_guidelines/icon_design_adaptive)\n- [AdaptiveIconDrawable](https://developer.android.com/reference/android/graphics/drawable/AdaptiveIconDrawable.html)\n- [createWithAdaptiveBitmap](https://developer.android.com/reference/android/graphics/drawable/Icon.html#createWithAdaptiveBitmap(android.graphics.Bitmap))\n\n\nSource code:\n\n- `platform/frameworks/base/graphics/java/android/graphics/drawable/AdaptiveIconDrawable.java`\n\nImplementation\n--------------\n\n\nTo change the shape of the icon on a platform, overlay one string in\n`framework/base/core/res/res/values/config.xml`, as follows: \n\n```ecl\n\u003c!-- Specifies the path that is used by AdaptiveIconDrawable class to crop launcher icons. --\u003e\n \u003cstring name=\"config_icon_mask\" translatable=\"false\"\u003e\"M50,0L100,0 100,100 0,100 0,0z\"\u003c/string\u003e\n```\n\n\nThe format and syntax of the string follow the [W3, SVG standard for path\ndefinition](https://www.w3.org/TR/SVG/paths.html). This format for PathData is what Android vector drawables\nsupport as well.\n\n\nThis path should be convex and should respect the safezone (66/71 = 91%) within\nthe view bounds. This is enforced in one of the CTS tests.\n\n\nIf you decide to use a circle as the platform mask, make sure to also overlay\nconfig_useRoundIcon = true. If not, set this config value false or do not\nspecify this config value.\n\nAdaptive Icon API\n-----------------\n\n\nThe API for the `AdaptiveIconDrawable` class is shown below: \n\n```gdscript\npackage android.graphics.drawable;\n public class AdaptiveIconDrawable extends Drawable implements Drawable.Callback {\n method public Drawable getBackground();\n method public Drawable getForeground();\n method public Path getIconMask();\n method public Region getSafeZone();\n method public float getExtraInsetFraction();\n method public int getOpacity();\n method public void invalidateDrawable(Drawable);\n method public void scheduleDrawable(Drawable, Runnable, long);\n method public void setAlpha(int);\n method public void setColorFilter(ColorFilter);\n method public void setOpacity(int);\n method public void unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable);\n }\n``` \n\n```gdscript\npublic class Icon extends Parceleable {\n method public Bitmap createWithAdaptiveBitmap();\n }\n```\n\nReference implementation\n------------------------\n\n\nNothing needs to be done to render the static adaptive icons on any of the\nSystem UI surfaces. When PackageManager returns a drawable, simply bind that to\nan ImageView. This is how icons are already rendered in Pre-O platforms.\n\n\nRegarding rendering dynamic motion effect, Launcher3\n(platform/packages/apps/Launcher3) will have a reference implementation showing\nhow to achieve the effect in O-MR1.\n\nValidation\n----------\n\n\nTo validate the implementation, after overriding the mask of their liking, see\nif icons are rendered correctly in Launcher3, Settings, Overview and Settings.\nYou may also run AdaptiveIconDrawableTest.java and AdaptiveIconMaskTest.java\ninside graphics CTS TestCase to test the implementation.\n\n\nA recommended manual test case can be found at:\nplatform/development/samples/AdaptiveIconSample/.\n\nKnown issues\n------------\n\n\nKnown issues include the following:\n\n- Blurry icons, depending on how the mask path is defined.\n- Zoomed-in shortcut icons if app developers do not use the `Icon.createWithAdaptiveBitmap()` method, or do not use this method properly. For this method to function properly, the passed in Bitmap should be padded 25% on all four sides.\n\n\nThese issues can be addressed as follows:\n\n- The mask should be defined in \\[0, 100\\] x \\[0, 100\\] coordinate system.\n- Make sure that images used for adaptive icons (launcher icons , shortcuts) have sufficient padding (25%) on all four sides."]]