欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android界面 NotificationManager使用Bitmap做图标

程序员文章站 2023-12-04 11:07:28
今天看到eoe问答里面有这“[android 界面]notificationmanager 如何使用bitmap做图标”这样一个问题,在论坛搜索也没有好的案例 特写一个简单...
今天看到eoe问答里面有这“[android 界面]notificationmanager 如何使用bitmap做图标”这样一个问题,在论坛搜索也没有好的案例

特写一个简单的demo供大家参考
今天发布的是notificationmanager 使用bitmap做图标
关键code
复制代码 代码如下:

public void notification(int flag)
{
notification notification = new notification();
//设置statusbar显示的icon
notification.icon = r.drawable.icon;
//设置statusbar显示的文字信息
// mynoti.tickertext= new_msg ;
notification.flags = notification.flag_auto_cancel;
//设置notification发生时同时发出默认声音
notification.defaults = notification.default_sound;
remoteviews contentview = new remoteviews(getpackagename(), r.layout.custom_notification);
bitmap bitmap=null;
if(flag==0)
{
bitmap=drawabletobitmap(this.getresources().getdrawable(r.drawable.icon));
}else
{
//此处是关键地方,可以从网络或是sdcard上获取图片,转成bitmap就可以
bitmap=drawabletobitmap(this.getresources().getdrawable(r.drawable.alert_dialog_icon));
}
contentview.setimageviewbitmap(r.id.notification_icon, bitmap);
contentview.settextviewtext(r.id.app_name, "custom notification");
notification.contentview = contentview;
intent intent = new intent(this, mainactivity.class);
pendingintent contentintent = pendingintent.getactivity(this, 0, intent,
pendingintent.flag_update_current);
notification.contentintent = contentintent;
//显示notification
random random = new random(new date().gettime());
mnotificationmanager.notify(random.nextint(1000000), notification);
}
//转化drawabletobitmap
public static bitmap drawabletobitmap(drawable drawable)
{
bitmap bitmap = bitmap.createbitmap(drawable.getintrinsicwidth(), drawable.getintrinsicheight(), drawable.getopacity() != pixelformat.opaque ? bitmap.config.argb_8888 : bitmap.config.rgb_565);
canvas canvas = new canvas(bitmap);
drawable.setbounds(0, 0, drawable.getintrinsicwidth(), drawable.getintrinsicheight());
drawable.draw(canvas);
return bitmap;
}

源码下载:notificationicon.rar