Android介面 NotificationManager使用Bitmap做表徵圖_Android

來源:互聯網
上載者:User
今天看到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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.