项目中遇到一个问题,Android 8.0 系统上 APP 的 icon 显示的是默认的机器人的 icon,这是什么回事?原来 Android 8.0(API 级别 26)引入了自适应启动器图标,能够在不一样设备模型中显示各类形状。下面看下官方酷炫动态图:android
图1. 自适应图标支持各类设备之间不一样的掩码。markdown
能够经过定义 2 层来控制自适应启动器图标的外观,包括背景和前景。您必须提供图标图层做为可绘图,图标轮廓周围不能有蒙版或背景阴影。app
图2. 自适应图标使用 2 个图层和 1 个蒙版进行定义。动画
在 Android 7.1(API级别25)及更早版本中,启动器图标大小为 48 x 48 dp。必须使用如下准则来调整图标图层的大小:spa
我验证了,不是这些尺寸也是能够的,但咱们仍是严格按照这个准则来吧。code
图3. 自适应图标支持各类视觉效果。orm
注意: 若是您没有使用必要的图层更新启动器图标,则该图标与系统 UI 显示的其余图标看起来不一致,而且不支持视觉效果。xml
咱们首先建立一个 Sample 项目,如图: ip
比以往多一个 res/mipmap-anydpi-v26 文件,打开,有背景和前景。utf-8
ic_launcher_background.xml
<?xml version="1.0" encoding="utf-8"?> <vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="108dp" android:height="108dp" android:viewportHeight="108" android:viewportWidth="108"> <path android:fillColor="#26A69A" android:pathData="M0,0h108v108h-108z" /> <path android:fillColor="#00000000" android:pathData="M9,0L9,108" android:strokeColor="#33FFFFFF" android:strokeWidth="0.8" /> <!--省略部分代码--> </vector> 复制代码
ic_launcher_foreground.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:aapt="http://schemas.android.com/aapt" android:width="108dp" android:height="108dp" android:viewportHeight="108" android:viewportWidth="108"> <path android:fillType="evenOdd" android:pathData="M32,64C32,64 38.39,52.99 44.13,50.95C51.37,48.37 70.14,49.57 70.14,49.57L108.26,87.69L108,109.01L75.97,107.97L32,64Z" android:strokeColor="#00000000" android:strokeWidth="1"> <aapt:attr name="android:fillColor"> <gradient android:endX="78.5885" android:endY="90.9159" android:startX="48.7653" android:startY="61.0927" android:type="linear"> <item android:color="#44000000" android:offset="0.0" /> <item android:color="#00000000" android:offset="1.0" /> </gradient> </aapt:attr> </path> <!--省略部分代码--> </vector> 复制代码
它们都是 vector,<foreground>
和<background>
是支持android:drawable
属性,我直接换成 ic_launcher_background.png 和 ic_launcher_foreground.png,<foreground>
和<background>
也支持@color/资源名
。
<?xml version="1.0" encoding="utf-8"?> <adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android"> <!--<background android:drawable="@color/colorAccent" />--> <background android:drawable="@drawable/ic_launcher_background" /> <foreground android:drawable="@drawable/ic_launcher_foreground" /> </adaptive-icon> 复制代码
而后清单使用android:icon
属性以指定可绘制资源,还可使用该android:roundIcon
属性定义图标可绘制资源。
<application … android:icon="@mipmap/ic_launcher" android:roundIcon="@mipmap/ic_launcher_round" …> </application> 复制代码
若是要将常规自适应启动器图标应用于快捷方式的相同蒙版和视觉效果,使用如下:
<adaptive-icon>
元素。createWithAdaptiveBitmap()
建立方法时调用该 方法。大功告成,Android 8.0 上能自适应,如下是默认的图标。
注意:Android Studio 3.0 如下的编译器没法找到 adaptive-icon 标签,这点未验证。
公众号「吴小龙同窗」回复:AdaptiveIconsSample,得到完整 Sample 代码。
个人公众号:吴小龙同窗,欢迎交流~