2010-08-27 48 views
10

我想在狀態欄中提示一個顯示圖標的通知 - 目前爲止還不錯,但實際上我希望此圖標爲3個字符的字符串。將字符串轉換爲可繪製的

所以我的問題是:有沒有辦法將我的字符串轉換爲Drawable在狀態欄中顯示爲圖標?

編輯:我最近發現了一個應用程序,它有類似的功能 - 電池指示燈

顯示當前電池電量在狀態欄通知圖標 - 我不知道它是否真的使用不同的100張圖像

Screenshot

+3

只是爲了防止任何人感興趣:我發現上面提到的應用程序真的每個值使用一個圖像 – DonGru 2010-08-30 14:46:24

+0

這正是我所需要的。除了在電池狀態下創建100 png(針對每個dpi值)之外,您是否找到了解決方案? – vault 2013-02-02 18:57:58

回答

3

短:不,你不能。

長:通知圖標需要R.drawable.something,並且您無法在運行時創建它。

0

你看過API演示>應用程序>通知>狀態欄?

如果您的字符串選項數量有限(如表情符號),您可以爲每個字符串創建繪圖。

+0

我想避免這種情況,因爲它具有相當多的可能性:) – DonGru 2010-08-28 09:29:24

2

您可以製作您自己的自定義繪圖,就像textview小部件一樣工作,除了繪製而不是視圖。 textview類只是包含文本的drawable的容器。

+0

您能否提供至少一份可以做到這一點的代碼草案? – vault 2013-02-02 18:51:16

+2

@vault我沒有示例,但是您想要做的是擴展Drawable並重寫draw(Canvas),然後從draw中調用canvas.drawText()。 – schwiz 2013-02-04 16:02:15

4
public Drawable getDrawable(String bitmapUrl) { 
     try { 
     URL url = new URL(bitmapUrl); 
     Drawable d =new BitmapDrawable(BitmapFactory.decodeStream(url.openConnection().getInputStream())); 
     return d; 
     } 
     catch(Exception ex) {return null;} 
    } 
+0

.setSmallIcon(int)以int爲輸入如何在那裏使用drawable? – 2014-05-08 18:16:58

1

我已經使用了一種解決方法,它適用於我。

首先,我轉換的字符串,位圖,然後將其轉換爲可繪製的,這裏是代碼:

byte [] encodeByte=Base64.decode(":",Base64.DEFAULT); 
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);  
Drawable d = new BitmapDrawable(bitmap); 

希望它能幫助!

+0

.setSmallIcon(int)以int爲輸入如何在那裏使用drawable? – 2014-05-08 17:56:28

+0

您只能在.setLargIcon方法上使用它,該方法將drawable作爲參數,smallIcon只接受資源。 – Gilson 2014-05-13 14:17:21

+0

我試過使用set setLargIcon但是圖標永遠不可見。大圖標何時顯示? – 2014-05-14 06:27:20

-1
try { 

    InputStream inputStream = new URL(Your imageWebAddress).openStream(); 

    drawable = Drawable.createFromStream(inputStream, null); 
    inputStream.close(); 
    } 
    catch (MalformedURLException ex) { } 
    catch (IOException ex) { } 
    layout.setBackground(drawable); 
0

(我知道這並不能完全回答OP的問題,但是標題讓我在這裏,因爲它是相當普遍的。)

了一下週圍擺弄之後,我想出了這個解決方案。這是相當混亂,可能可以改善,但它的作品。

在其當前形式中,該函數接受傳遞的字符串的第一個字母以及該字符串的唯一ID。該ID僅用於生成背景顏色並記住它,因此如果您要使用穩定的顏色,則可以將其刪除。

我這樣做了爲沒有保存圖像的聯繫人生成默認圖像,但它應該很容易適應。它也恰好返回InputStream而不是Drawable,但您可以在繪製後返回bitmap,或使用Drawable.createFromStream()

private static InputStream returnDefaultContact(Context context, String name, long id) { 
    Paint textPaint = new Paint(); 
    textPaint.setColor(Color.WHITE); 
    textPaint.setTextAlign(Paint.Align.CENTER); 
    textPaint.setTextSize(110); 

    int color = PreferenceManager.getDefaultSharedPreferences(context).getInt("contact_by_id_" + id, 0); 

    if (color == 0) { 
     int colorValue1 = (int)((56 + Math.random() * 200)); 
     int colorValue2 = (int)((56 + Math.random() * 200)); 
     int colorValue3 = (int)((56 + Math.random() * 200)); 

     color = Color.rgb(colorValue1, colorValue2, colorValue3); 

     PreferenceManager.getDefaultSharedPreferences(context).edit().putInt("contact_by_id_" + id, color).apply(); 
    } 

    Paint backgroundPaint = new Paint(); 
    backgroundPaint.setColor(color); 

    Bitmap bitmap = Bitmap.createBitmap(120, 120, Bitmap.Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 

    canvas.drawCircle(canvas.getWidth()/2, canvas.getHeight()/2, canvas.getHeight()/2, backgroundPaint); 

    int xPos = (canvas.getWidth()/2); 
    int yPos = (int) ((canvas.getHeight()/2) - ((textPaint.descent() + textPaint.ascent())/2)) ; 

    canvas.drawText(name.substring(0, 1), xPos, yPos, textPaint); 

    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
    byte[] imageInByte = stream.toByteArray(); 

    return new ByteArrayInputStream(imageInByte); 
}