note_35:Image Asset和mipmap-anydpi-v26

Image Asset和mipmap-anydpi-v26


因为build.gradle里面写的targetSdkVersion低于26,而且也只是在安卓8.1以下的设备跑过,所以根本没有发现应用图标有问题。直到前几天,忽然发现apk装到机器上之后根本没有显示mipmap-*dpi文件夹下的ic_launcher.png,而是显示了系统自带的那个绿底白色的android机器人图标。

因为后来升级了Android Studio,所以在manifest.xml中发现多了个ic_launcher.xmlic_launcher_round.xml的文件,这两个文件放在mipmap-anydpi-v26的文件夹下。

note_35:Image Asset和mipmap-anydpi-v26

原本以为ic_launcher_backgroundic_launcher_foreground应该是个图片,没想到是个<vector></vector>xml文件。而这个ic_launcher_foreground预览的时候可以看到就是白色的机器人。

note_35:Image Asset和mipmap-anydpi-v26



方法1:删掉mipmap-anydpi-v26

StackOverflow上有一个推荐的回答,就是把mipmap-anydpi-v26这个文件夹删去,因为删去之后系统没机会找ic_launcher.xml。对于安卓8.1来说,系统会优先读mipmap-anydpi-v26里的ic_launcher来匹配图标,而不是像8.1以下的那样直接读取mipmap-*dpi。然而,我的图标还是没有显示出来。于是找到了博客说可以new一个Image Asset来改图标。


方法2:用Image Asset来制作adaptive icon

首先,确保build.gradle里面的buildSdk是26或以上的,否则没办法操作Image Asset里的Resize。

mipmap-anydpi-v26 -> New -> Image Asset

note_35:Image Asset和mipmap-anydpi-v26

打开之后是这样的
note_35:Image Asset和mipmap-anydpi-v26

Foreground Layer就是图标的图案(白色机器人),Background Layer是背景(绿色底)。

Name就是生成的这个icon的名字,如果不改名的话,那么用Image Asset制作出来的icon就会替代原来放在mipmap-*dpi里的所有ic_launcheric_launcher_round。(当然会按照像素匹配)

Source Asset里面的Path就是自定义的icon,因为系统只生成一张icon,然后再把这些icon制作出不同的dpi类型的icon,所以Path里面要选择*dpi最高的那张图片,否则出来的icon一转成xhdpi之后就会变得特别模糊。

当选择了一张图片之后,
note_35:Image Asset和mipmap-anydpi-v26

右上角的Preview里面有很多个dpi可以预览。现在很明显可以看出这个图片过大,所以需要在左下角Resize进行操作。

note_35:Image Asset和mipmap-anydpi-v26

ic_launcher_foreground至少把图案放在那个安全区的圆圈里面。接下来就是ic_launcher_background。

note_35:Image Asset和mipmap-anydpi-v26
如果不是要用图片叠加的话,那么Asset Type可以直接选Color而不选Image。
note_35:Image Asset和mipmap-anydpi-v26
然后Next,再保存就行了。

这时重新打开ic_launcher.xml就会变成这样
note_35:Image Asset和mipmap-anydpi-v26
重新打开ic_launcher.png,原来的绿底白机器人会被替换掉
note_35:Image Asset和mipmap-anydpi-v26