当前位置: 移动技术网 > 移动技术>移动开发>Android > android 实现类似微信缓存和即时更新好友头像示例

android 实现类似微信缓存和即时更新好友头像示例

2019年07月24日  | 移动技术网移动技术  | 我要评论

引言

使用微信时我们会发现,首次进入微信的好友列表时,会加载好友头像,但是再次进入时,就不用重新加载了,而且其他页面都不用重新加载,说明微信的好友头像是缓存在本地的,然后好友修改头像后,又会及时的更新,这个功能是如何实现的呢,我们来分析一下

分析

关于头像缓存的实现

头像是网络图片,而且数据量较大,如果用我们常用的sharedpreferences将头像以bitmap的形式存储,势必会造成oom,这个方法是行不通的,我们存储的只能是图片的地址,但是如果只存储地址的话,要转化成图片,还是要通过网络请求重新加载,达不到我们要求的效果,所以我们需要在磁盘中单独开辟一块空间,将头像以bitmap的形式进行存储,如何实现呢?其实关于网络图片的缓存,有很多开源的第三方框架,比较可靠好用的如xutils,glide,volley,universal-image-loader,picasso,fresco等等。

下面我们以常用的xutils为例首先对bitmaputils的实例化,对于磁盘缓存路径,磁盘缓存空间大小,内存缓存的空间大小,内存缓存百分比可以自定义,也可以使用默认配置,代码如下:

 /**
 * @param context 上下文
 */
public bitmaputils(context context) {
  this(context, null);
}

/**
 * @param context 上下文
 * @param diskcachepath 磁盘高速缓存路径
 */
public bitmaputils(context context, string diskcachepath) {
  if (context == null) {
    throw new illegalargumentexception("context may not be null");
  }

  this.context = context.getapplicationcontext();
  globalconfig = bitmapglobalconfig.getinstance(this.context, diskcachepath);
  defaultdisplayconfig = new bitmapdisplayconfig();
}

/**
 * 
 * @param context 上下文
 * @param diskcachepath 磁盘高速缓存路径
 * @param memorycachesize 内存缓存空间大小
 */
public bitmaputils(context context, string diskcachepath, int memorycachesize) {
  this(context, diskcachepath);
  globalconfig.setmemorycachesize(memorycachesize);
}

/**
 * 
 * @param context 上下文
 * @param diskcachepath 磁盘高速缓存路径
 * @param memorycachesize 内存缓存空间大小
 * @param diskcachesize 磁盘高速缓存空间大小
 */
public bitmaputils(context context, string diskcachepath, int memorycachesize, int diskcachesize) {
  this(context, diskcachepath);
  globalconfig.setmemorycachesize(memorycachesize);
  globalconfig.setdiskcachesize(diskcachesize);
}

/**
 * 
 * @param context 上下文
 * @param diskcachepath 磁盘高速缓存路径
 * @param memorycachepercent 内存缓存百分比
 */
public bitmaputils(context context, string diskcachepath, float memorycachepercent) {
  this(context, diskcachepath);
  globalconfig.setmemcachesizepercent(memorycachepercent);
}
   /**
 * 
 * @param context 上下文
 * @param diskcachepath 磁盘高速缓存路径
 * @param memorycachepercent 内存缓存百分比
 * @param diskcachesize 磁盘缓存空间大小
 */
public bitmaputils(context context, string diskcachepath, float memorycachepercent, int diskcachesize) {
  this(context, diskcachepath);
  globalconfig.setmemcachesizepercent(memorycachepercent);
  globalconfig.setdiskcachesize(diskcachesize);
}

一般情况下,我们只需要使用默认配置就可以了,即

bitmaputils bitmap = new bitmaputils(context);

然后对图片的缓存和显示

 /**
 * 根据图片路径,显示到具体的view上
 * @param container 要把图片显示到的view
 * @param uri 图片路径
 */
public <t extends view> void display(t container, string uri) {
  display(container, uri, null, null);
}

/**
 * 根据图片路径,显示到具体的view上
 * @param container 要把图片显示到的view
 * @param uri 图片路径
 * @param displayconfig
 */
public <t extends view> void display(t container, string uri, bitmapdisplayconfig displayconfig) {
  display(container, uri, displayconfig, null);
}

/**
 * 根据图片路径,显示到具体的view上
 * @param container 要把图片显示到的view
 * @param uri 图片路径
 * @param callback 加载过程回调各种状态
 */
public <t extends view> void display(t container, string uri, bitmaploadcallback<t> callback) {
  display(container, uri, null, callback);
}  
 
 /**
 * 根据图片路径,显示到具体的view上
 * @param container 要把图片显示到的view
 * @param uri 图片路径
 * @param displayconfig 位图显示配置
 * @param callback
 */
public <t extends view> void display(t container, string uri, bitmapdisplayconfig displayconfig, bitmaploadcallback<t> callback) {
  if (container == null) {
    return;
  }

  if (callback == null) {
    callback = new defaultbitmaploadcallback<t>();
  }

  if (displayconfig == null || displayconfig == defaultdisplayconfig) {
    displayconfig = defaultdisplayconfig.clonenew();
  }
  // optimize max 
  bitmapsize size = displayconfig.getbitmapmaxsize();sizedisplayconfig.setbitmapmaxsize(bitmapcommonutils.optimizemaxsizebyview(container, size.getwidth(), size.getheight()));

  container.clearanimation();

  if (textutils.isempty(uri)) {
    callback.onloadfailed(container, uri, displayconfig.getloadfaileddrawable());
    return;
  }

  // start loading
  callback.onpreload(container, uri, displayconfig);

  // find bitmap from mem cache.
  bitmap bitmap = globalconfig.getbitmapcache().getbitmapfrommemcache(uri, displayconfig);

  if (bitmap != null) {
    callback.onloadstarted(container, uri, displayconfig);
    callback.onloadcompleted(
        container,
        uri,
        bitmap,
        displayconfig,
        bitmaploadfrom.memory_cache);
  } else if (!bitmaploadtaskexist(container, uri, callback)) {

    final bitmaploadtask<t> loadtask = new bitmaploadtask<t>(container, uri, displayconfig, callback);

    // get executor
    priorityexecutor executor = globalconfig.getbitmaploadexecutor();
    file diskcachefile = this.getbitmapfilefromdiskcache(uri);
    boolean diskcacheexist = diskcachefile != null && diskcachefile.exists();
    if (diskcacheexist && executor.isbusy()) {
      executor = globalconfig.getdiskcacheexecutor();
    }
    // set loading image
    drawable loadingdrawable = displayconfig.getloadingdrawable();
    callback.setdrawable(container, new asyncdrawable<t>(loadingdrawable, loadtask));

    loadtask.setpriority(displayconfig.getpriority());
    loadtask.executeonexecutor(executor);
  }
}

从这段代码中我们可以看到,当要加载某张图片时,会根据图片地址进行查找是否有对应的bitmap缓存图片,如果有就直接引用缓存,如果没有就加载并缓存,所以我们对图片的缓存只需要实现以上方法就可以了,而且只要设置相同的缓存路径,就可以实现一个页面缓存后,其他页面有相同图片也可以调用。那么缓存之后,好友更新头像,又是怎么做到即时更新的呢?

缓存后如何实现即时更新头像

根据查阅的资料,可以归结为以下几种实现方式:

1.在服务器返回用户数组时多加一个字段头像最后一次修改时间或者修改过几次等标志符,与缓存进行比较,是否有变化

2.利用图片的checksum来实现,如果check到这个数字有变化,就会自动去更新

3.利用socket监听,当好友头像更新时候首先会告诉服务器,服务器将变化通知推送到所有好友,好友监听收到通知后自动更新
第一种方法和第二种方法本质是一致的,通过请求服务器的数据与本地缓存进行对比,是由客户端处理的,第三种方法的话你换一次头像就要服务器去提醒你的所有好友一遍,服务器压力会不会比较大

仔细去研究一下微信,就会发现,当好友头像修改后,如果你停留在某个页面,进入的这个页面是之前进入过的,还没有销毁,头像是不会改变的,你需要打开一个新的页面或者重新进入微信,才会更新头像,由此看出,微信并不是用的第三种方式,而是采用了前两种方式的实现原理,只有在创建一个activity或fragment时,调用接口,读取服务器数据时才会更新头像

总结

通过以上的分析,我们基本捋清了思路,要实现类似微信的缓存和更新还有头像,先是在磁盘开辟一个空间,用于读写头像的bitmap,然后创建页面时,读取服务器数据和本地缓存进行比较,如果有变化就进行更新

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网