Convert String to Drawable

2020-07-06 03:24发布

I want to raise a notification showing an icon in the status bar - so far so good, but actually I would like this icon to be a 3 character String.

So my question is: Is there a way to convert my String into a Drawable to display it as Icon in the status bar?

EDIT: I recently found an app which does something similar - Battery Indicator

It shows the current battery level as notification icon in the status bar - I wonder if it really uses different 100 images

Screenshot

标签: android
8条回答
该账号已被封号
2楼-- · 2020-07-06 04:05

Have you looked at the API Demos > App > Notifications > Status Bar?

If you have limited number of String options (like Smileys) you can create drawables for each of those Strings.

查看更多
仙女界的扛把子
3楼-- · 2020-07-06 04:08

No you can not, I thought you could use the same method as here: Combine image and text to drawable, but you can't, as the notification takes a drawable id, not a drawable object.

查看更多
一纸荒年 Trace。
4楼-- · 2020-07-06 04:17

Short: No, you can't.

Long: The notification needs a R.drawable.something for the icon and you can't create it on runtime.

查看更多
叛逆
5楼-- · 2020-07-06 04:17

you can make your own custom drawable that would work just like the textview widget except it is a drawable instead of a view. The textview class is just a container for the drawable that contains the text.

查看更多
欢心
6楼-- · 2020-07-06 04:21

I have used a workaround and it worked properly for me.

First i convert the string to bitmap and then convert it to a drawable, here is the code:

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

Hope it helps!

查看更多
欢心
7楼-- · 2020-07-06 04:27
   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;}
    }
查看更多
登录 后发表回答