自从Android 5.0推出后,通知中的大图标有颜色背景:
Notification.Builder.setColor(int)
)。那么如何设置大图标的颜色呢?它是否是实际图像的一部分?如果是,圆形半径应该是多少?Notification.Builder.setColor(int)
)。那么如何设置大图标的颜色呢?它是否是实际图像的一部分?如果是,圆形半径应该是多少?是的,大图标的颜色是实际图片的一部分。Lollipop上大图标的尺寸为40x40dp,光学视图填充整个图像。因此,您应该创建一个带有20dp半径圆形的40x40dp资产。 您可以按以下方式设置通知的大图标:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.notification_small_icon)
.setLargeIcon(notificationLargeIconBitmap)
.setContentTitle("Notification")
.setContentText("Content text")
.setColor(context.getResources().getColor(R.color.accent_color));
如果您需要使用drawable资源来获取大图标,可以像这样获取 Bitmap
实例:
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
context.getResources(),
R.drawable.notification_large_icon);
如果你想让你的通知在之前的安卓版本(kitkat 及以下)中以优美的方式显示,那么你需要拥有一个尺寸为64x64dp 的方形大图标。
setColor()
函数来对小图标进行着色。fun Bitmap.tint(color: Int): Bitmap =
Bitmap.createBitmap(this.width, this.height, Bitmap.Config.ARGB_8888).also { outBmp ->
Canvas(outBmp).drawBitmap(
this, 0f, 0f,
Paint().apply {
this.colorFilter = PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN)
}
)
}
NotificationCompat.Builder(context)
.setColor(yourColor)
.setLargeIcon(largeBitmap.tint(yourColor))
.setSmallIcon(R.drawable.small_icon)
一个大图标应该始终有一个背景(即头像)。它还用作穿戴设备上通知的背景。它显示在不同的背景颜色上,因此应该是一张非透明的图片。