Android notification动态设置网络图片icon

本文介绍了如何在Android中实现在发送Notification时动态加载服务器上的图片作为图标。通过自定义Notification布局,使用BitmapFactory.decodeStream从网络流中解析Bitmap,并将其设置到自定义布局的ImageView上,最终显示在Notification中。
摘要由CSDN通过智能技术生成

公司项目有个需求,需要在发送Notification的时候动态加载服务器的图片,经研究实现。大概思路如下

自定义Notification的布局文件,这样能够很方便设置View的属性。

首先加载网络图片,使用BitmapFactory.decodeStream解析出Bitmap,然后设置到自定义布局文件中的ImageView上。


自定义Notification布局如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/image"  
  8.         android:layout_width="45dip"  
  9.         android:layout_height="45dip"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_marginBottom="8.0dip"  
  12.         android:layout_marginLeft="8.0dip"  
  13.         android:layout_marginRight="10dp"  
  14.         android:layout_marginTop="8.0dip" />  
  15.   
  16.     <TextView  
  17.         android:id="@+id/title"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_marginTop="8.0dip"  
  21.         android:layout_toRightOf="@id/image"  
  22.         android:textSize="16.0dip" />  
  23.   
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值