0
我希望Notification
图标是一个数字,取决于条件可以是1到1000之间的任意数字。动态更改通知图标
有没有办法来动态地做到这一点(如生成从String
的Icon
和setSmallIcon()
使用它),而无需手动创建所有这些数字为图像文件,并动态调用它们?
我希望Notification
图标是一个数字,取决于条件可以是1到1000之间的任意数字。动态更改通知图标
有没有办法来动态地做到这一点(如生成从String
的Icon
和setSmallIcon()
使用它),而无需手动创建所有这些数字为图像文件,并动态调用它们?
不幸的是没有办法(我知道的),要做到这一点低于API级别23
在API级别23+:
你可以使用Canvas.drawText()
创建从Bitmap
您String
。
例如:
public Bitmap createBitmapFromString(String string) {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(50); // size is in pixels
Rect textBounds = new Rect();
paint.getTextBounds(string, 0, string.length(), textBounds);
Bitmap bitmap = Bitmap.createBitmap(textBounds.width(), textBounds.height(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(string, -textBounds.left,
textBounds.height() - textBounds.bottom, paint);
return bitmap;
}
之后,你可以使用这个Bitmap
与Icon.createWithBitmap()
创建Icon
。
再经过此Icon
到setSmallIcon()
(在API级23的溶液中加入此方法)。
(setSmallIcon(Icon icon)
也被加入API等级23)
你可能要考虑的徽章,而不是 –
不可能在API级别23 – earthw0rmjim
@GabeSechan可以请你给出一个具体的例子吗?试图寻找绑定,但我得到了很多htis,我看着的那些似乎并不是我需要的。我正在尝试创建类似于[this]的内容(https://lh4.ggpht.com/9PZYdDwGmFliEq1OKrM_PU1NId5q1uYleGittsMyNHwMn6JQd2hvu-cTrqsS00phew=h310-rw) –