通知 - 在 Lollipop Android 之前的版本上设置大图标
Notification - set large icon on pre Lollipop Android version
我已经在开发带有雪崩消息通知的应用程序,但我在早于 Lollipop Android 版本驱动的设备上遇到过通知图标尺寸过大的问题。
在 Lollipop 和 Marshmallow 设备上它看起来不错:
但是当我在 Android 4.4 模拟器上打开一个应用程序时,我发现了这个:
图片没有缩放、调整,只是在中间被裁剪了。它看起来肯定不好。
NOTE: To make it clear, that this large notification icons is
downloaded from server together with message date and content. It
looks like this:
我的解决方案
在雪崩通知中工作了几个小时后 class 我添加了两种方法,运行 只有检测到 Android pre-lollipop 版本:
- 第一个将图标形状从矩形更改为正方形并添加白色背景
- 第二个将图标尺寸更改为 96x96
更改后,我在同一个 Kitkat 模拟器 (Moto X 2013) 上的通知图标看起来好多了:
问题
你能告诉我是否有更简单的方法来处理这个问题?如果不是,除了 96x96,我还需要支持哪些图标尺寸?
提前致谢
如果我理解正确,您希望通知图标在棒棒糖设备和前棒棒糖设备上看起来都不错,根据我的经验,我为确保在两个 SDK 中都好看所做的是:
对于通知大图标,我使用这些尺寸的资源:
96x96(高清像素)
64x64(MDPI)
128x128 (xhdpi)
192x192(xxhdpi)
256x256 (xxxhdpi)
我使用这段代码来配置通知样式:
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle(title);
notiStyle.bigText(msg);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(msg)
.setColor(coloresOnboarding.getColor())
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
.setSmallIcon(R.mipmap.ic_launchersmall)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcherlarge))
.setStyle(notiStyle);
Intent resultIntent = new Intent(context, HeadAppMain.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
int mId=1;
mBuilder.setAutoCancel(true);
希望对您有所帮助。
我已经在开发带有雪崩消息通知的应用程序,但我在早于 Lollipop Android 版本驱动的设备上遇到过通知图标尺寸过大的问题。
在 Lollipop 和 Marshmallow 设备上它看起来不错:
但是当我在 Android 4.4 模拟器上打开一个应用程序时,我发现了这个:
图片没有缩放、调整,只是在中间被裁剪了。它看起来肯定不好。
NOTE: To make it clear, that this large notification icons is downloaded from server together with message date and content. It looks like this:
我的解决方案
在雪崩通知中工作了几个小时后 class 我添加了两种方法,运行 只有检测到 Android pre-lollipop 版本:
- 第一个将图标形状从矩形更改为正方形并添加白色背景
- 第二个将图标尺寸更改为 96x96
更改后,我在同一个 Kitkat 模拟器 (Moto X 2013) 上的通知图标看起来好多了:
问题
你能告诉我是否有更简单的方法来处理这个问题?如果不是,除了 96x96,我还需要支持哪些图标尺寸?
提前致谢
如果我理解正确,您希望通知图标在棒棒糖设备和前棒棒糖设备上看起来都不错,根据我的经验,我为确保在两个 SDK 中都好看所做的是:
对于通知大图标,我使用这些尺寸的资源: 96x96(高清像素) 64x64(MDPI) 128x128 (xhdpi) 192x192(xxhdpi) 256x256 (xxxhdpi)
我使用这段代码来配置通知样式:
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle(); notiStyle.setBigContentTitle(title); notiStyle.bigText(msg); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context) .setContentTitle(title) .setContentText(msg) .setColor(coloresOnboarding.getColor()) .setCategory(NotificationCompat.CATEGORY_MESSAGE) .setPriority(NotificationCompat.PRIORITY_HIGH) .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000}) .setSmallIcon(R.mipmap.ic_launchersmall) .setAutoCancel(true) .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcherlarge)) .setStyle(notiStyle); Intent resultIntent = new Intent(context, HeadAppMain.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); mBuilder.setContentIntent(pendingIntent); NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); int mId=1; mBuilder.setAutoCancel(true);
希望对您有所帮助。