2016-09-07 126 views
0

我希望Notification圖標是一個數字,取決於條件可以是1到1000之間的任意數字。動態更改通知圖標

有沒有辦法來動態地做到這一點(如生成從StringIconsetSmallIcon()使用它),而無需手動創建所有這些數字爲圖像文件,並動態調用它們?

+0

你可能要考慮的徽章,而不是 –

+0

不可能在API級別23 – earthw0rmjim

+0

@GabeSechan可以請你給出一個具體的例子嗎?試圖尋找綁定,但我得到了很多htis,我看着的那些似乎並不是我需要的。我正在嘗試創建類似於[this]的內容(https://lh4.ggpht.com/9PZYdDwGmFliEq1OKrM_PU1NId5q1uYleGittsMyNHwMn6JQd2hvu-cTrqsS00phew=h310-rw) –

回答

1

不幸的是沒有辦法(我知道的),要做到這一點低於API級別23

在API級別23+

你可以使用Canvas.drawText()創建從BitmapString

例如:

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; 
} 

之後,你可以使用這個BitmapIcon.createWithBitmap()創建Icon

再經過此IconsetSmallIcon()(在API級23的溶液中加入此方法)。

setSmallIcon(Icon icon)也被加入API等級23)