Android .setColor for Large Icon

时间:2014-11-18 13:28:26

标签: java android notifications icons android-5.0-lollipop

我想询问是否有办法从.setColor设置.setLargeIcon的通知颜色? 因为我一使用.setSmallIcon.setLargeIcon我的颜色就用于小图标。我想用LargeIcon表示我的个人通知图标,以及用小图标触发通知的应用程序图标。

示例:

Bitmap maintenanceIcon = BitmapFactory.decodeResource(getResources(),R.drawable.maintenance);
            Intent replacePumpIntent = new Intent(this, FoodListActivity.class);
            PendingIntent replacePumpPendingIntent =  PendingIntent.getActivity(this,0,replacePumpIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder maintenanceBuilder = new NotificationCompat.Builder(this)


                    .setLargeIcon(maintenanceIcon)
                    .setSmallIcon(R.drawable.app)
                    .setContentTitle("Maintenance: ")
                    .setColor(getResources().getColor(R.color.alertMaintenance))
                    .setContentText(Html.fromHtml(getString(R.string.alert_maintenance_message)))

                    .setLights(Color.YELLOW, 500 , 500)
                    .setVibrate(new long[] { 100, 250, 100, 250, 100, 250 })
                    .setPriority(0x00000001)
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(Html.fromHtml(getString(R.string.alert_maintenance_message))))
                    .addAction(R.drawable.ic_arrow_right_black, getString(R.string.alert_maintenance_button_1),replacePumpPendingIntent );

            NotificationManager maintenanceNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            maintenanceNotificationManager.notify(3, maintenanceBuilder.build());

3 个答案:

答案 0 :(得分:2)

这将帮助您从通知图标中删除灰色

 Notification notification = new NotificationCompat.Builder(context)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentText("Simple description of something meaningful")
    .setContentTitle("Yo check this out")
    .setColor(context.getResources()
            .getColor(R.color.brand_color))
    .build();

 NotificationManager manager  = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

 manager.notify(SINGLE_NOTIFICATION_ID, notification);

 brand_color is defined as #FF0066CC.

Source

答案 1 :(得分:1)

根据这个:https://stackoverflow.com/a/27023679/327011

同时拥有这两个图标时无法更改大图标背景。大图标不应该是透明的。

答案 2 :(得分:0)

您可以为大图标着色,然后设置为 NotificationBuilder。使用此函数为位图着色:

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(maintenanceIcon.tint(yourColor))
        .setSmallIcon(R.drawable.app)

在这种情况下,大小图标将被着色为所需的颜色