Android 应用icon适配

前言

上架Google Play最低SDK版本为8.0,在升级==targetSdkVersion = 26==然后在兼容测试的时候发现通知图标变成灰色不显示图标,启动图标多了一个外边框,在调查后开启图标适配填坑。

为什么要适配应用图标

在当前Android环境下,应用图标功能是极其混乱的。截取了谷歌亲儿子Nexus 5的系统菜单,
应用图标可以是方形、圆形、圆角矩形、或者其他不规则图形。

《Android 应用icon适配》 Nexus 5

在Android 8.0 (API level 26) 安卓通过自适应图标(Adaptive icons)意图让应用图标的形状都保持一致

8.0系统的应用图标适配

Guide

《Android 应用icon适配》 image

官方Guide中把应用图标被分为了两层:前景图片和背景图片。
我们在设计应用图标的时候,需要将前景和背景分离,前景用来展示应用图标的Logo,背景用来衬托应用图标的Logo。需要注意的是,背景层在设计的时候只允许定义颜色和纹理,背景的形状由系统决定。

如何适配

通过Android Studio新建项目其实已经给了我们一个例子!

《Android 应用icon适配》 sample

我们可以看到在Android Studio 3x中建立的项目多了一个++mipmap-anydpi-v26++文件夹,这个文件就是自适应图标,在manifest文件中也多了一个android:roundIcon设置。

 android:icon="@mipmap/ic_launcher"
 android:label="@string/app_name"
 android:roundIcon="@mipmap/ic_launcher_round"

那么我们得让UI给我们切两套图?然后给5个应用图标尺寸?其实Google提供Image Asset工具我们让UI提供前景图片和背景图片然后用Image Asset就能生成所有图标。

Image Asset工具的使用

Image Asset Studio 可以帮助您生成以下图标类型:

  • 启动器图标
  • 操作栏和标签图标
  • 通知图标
启动器图标

我先在iconfont上随便找一个图标举个例子

1.打开Image Asset工具

《Android 应用icon适配》 WX20190103-172516.png

2.选择你的icon

《Android 应用icon适配》 WX20190103-172701.png

3.生成结果

《Android 应用icon适配》 ic_launcher_round.png
《Android 应用icon适配》 ic_launcher.png

通知图标

通知图标操作方式一样,注意把icon Type换成Notification icons
通知5.0变更

  • 使用 setColor() 在您的图标图像后面的圆形中设置重点色彩。
  • 更新或移除使用色彩的资源。系统在操作图标和主要通知图标中忽略所有非阿尔法通道。您应假设这些图标仅支持阿尔法通道。系统用白色绘制通知图标,用深灰色绘制操作图标。

*图片需要无透明通道,简单点来说就是没有其他的颜色。

通知例子

  private void sendNotification(String title, String body, PendingIntent pendingIntent) {
        final String CHANNEL_ID = "channel_id";
        final String CHANNEL_NAME = "channel_name";

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
        notificationBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE)
                .setSmallIcon(R.drawable.ic_start_name)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round))
                .setColor(Color.parseColor("#9F9FA0"))
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }
        notificationManager.notify(0, notificationBuilder.build());
    }

总结

通过Android Studio的Image Asset工具会自动帮我们生成适配系统的应用图标,以后写Demo也能自己做一个漂亮的图标了。

    原文作者:liliLearn
    原文地址: https://www.jianshu.com/p/f2af1ae9d94a
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞