项目用到异步加载头像LasyList
package com.leo.proforjob; import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler; import java.util.Collections;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; public class ImageLoader { private MemoryCache memoryCache = new MemoryCache();
private Map<ImageProcessingCallback, Long> callbacks = Collections.synchronizedMap(new WeakHashMap<ImageProcessingCallback, Long>());
private ExecutorService executorService;
private Handler handler = new Handler();// handler to display images in UI thread private ImageLoader() { } private static class SingletonHolder {
public static final ImageLoader instance = new ImageLoader();
} public static ImageLoader getInstance() {
return SingletonHolder.instance;
} public void init(Context context) {
executorService = Executors.newFixedThreadPool(5);
} public void displayImage(Long id, ImageProcessingCallback imageProcessingCallback) {
imageProcessingCallback.onImagePreProcessing();
callbacks.put(imageProcessingCallback, id);
Drawable drawable = memoryCache.get(id);
if (drawable != null) {
imageProcessingCallback.onImageProcessing(drawable);
}else {
queuePhoto(id, imageProcessingCallback);
}
} private void queuePhoto(Long id, ImageProcessingCallback imageProcessingCallback) {
PhotoToLoad p = new PhotoToLoad(id, imageProcessingCallback);
executorService.submit(new PhotosLoader(p));
} // Task for the queue
private class PhotoToLoad {
public Object id;
public ImageProcessingCallback imageProcessingCallback; public PhotoToLoad(Object u, ImageProcessingCallback i) {
id = u;
imageProcessingCallback = i;
}
} class PhotosLoader implements Runnable {
PhotoToLoad photoToLoad; PhotosLoader(PhotoToLoad photoToLoad) {
this.photoToLoad = photoToLoad;
} @Override
public void run() {
try {
if (viewReused(photoToLoad))
return;
//Drawable drawable = getBitmap(photoToLoad.url);
//memoryCache.put(photoToLoad.id, drawable);
if (viewReused(photoToLoad))
return;
// BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
//handler.post(bd);
} catch (Throwable th) {
th.printStackTrace();
}
}
} boolean viewReused(PhotoToLoad photoToLoad) {
Long tag = callbacks.get(photoToLoad.imageProcessingCallback);
if (tag == null || !tag.equals(photoToLoad.id))
return true;
return false;
} // Used to display bitmap in the UI thread
class BitmapDisplayer implements Runnable {
Drawable bitmap;
PhotoToLoad photoToLoad; public BitmapDisplayer(Drawable b, PhotoToLoad p) {
bitmap = b;
photoToLoad = p;
} public void run() {
if (viewReused(photoToLoad))
return;
if (bitmap != null) {
photoToLoad.imageProcessingCallback.onImageProcessing(bitmap);
}
}
} public void clearCache() {
memoryCache.clear();
} }
package com.leo.proforjob; import android.graphics.drawable.Drawable; /**
* Created by leo on 16/8/1.
*/
public interface ImageProcessingCallback {
void onImagePreProcessing(); void onImageProcessing(Drawable drawable);
}
package com.leo.proforjob; import android.graphics.drawable.Drawable;
import android.util.Log; import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map; /**
* Created by leo on 16/8/1.
*/
public class MemoryCache { private static final String TAG = "MemoryCache";
private Map<Object, Drawable> cache= Collections.synchronizedMap(
new LinkedHashMap<Object, Drawable>(10,1.5f,true));//Last argument true for LRU ordering
private long size=0;//current allocated size
private long limit=1000000;//max memory in bytes public MemoryCache(){
//use 25% of available heap size
setLimit(Runtime.getRuntime().maxMemory()/4);
} public void setLimit(long new_limit){
limit=new_limit;
Log.i(TAG, "MemoryCache will use up to "+limit/1024./1024.+"MB");
} public Drawable get(Long id){
try{
if(!cache.containsKey(id))
return null;
//NullPointerException sometimes happen here http://code.google.com/p/osmdroid/issues/detail?id=78
return cache.get(id);
}catch(NullPointerException ex){
ex.printStackTrace();
return null;
}
} public void put(Object id, Drawable bitmap){
try{
if(cache.containsKey(id))
size-=getSizeInBytes(cache.get(id));
cache.put(id, bitmap);
size+=getSizeInBytes(bitmap);
checkSize();
}catch(Throwable th){
th.printStackTrace();
}
} private void checkSize() {
if(size>limit){
Iterator<Map.Entry<Object, Drawable>> iter=cache.entrySet().iterator();//least recently accessed item will be the first one iterated
while(iter.hasNext()){
Map.Entry<Object, Drawable> entry=iter.next();
size-=getSizeInBytes(entry.getValue());
iter.remove();
if(size<=limit)
break;
}
}
} public void clear() {
if (cache !=null) {
cache.clear();
}
size=0;
} long getSizeInBytes(Drawable drawable) {
if(drawable==null)
return 0;
return drawable.getIntrinsicWidth() * drawable.getIntrinsicHeight();
} }
ImageLoader.getInstance().displayImage(id, callback);
项目用到异步加载头像LasyList的更多相关文章
- UniversalImageLoader(异步加载大量图片)
UniversalImageLoader是用于加载图片的一个开源项目,UniversalImageLoader是实现异步加载大量图片的源码和例子,包括缓存.硬盘缓存.容错机制等技术.在其项目介绍中是这 ...
- cocos2dx lua中异步加载网络图片,可用于显示微信头像
最近在做一个棋牌项目,脚本语言用的lua,登录需要使用微信登录,用户头像用微信账户的头像,微信接口返回的头像是一个url,那么遇到的一个问题就是如何在lua中异步加载这个头像,先在引擎源码里找了下可能 ...
- Python 爬虫练习项目——异步加载爬取
项目代码 from bs4 import BeautifulSoup import requests url_prefix = 'https://knewone.com/discover?page=' ...
- 关于ios异步加载图片的几个开源项目
一.HjCache 原文:http://www.markj.net/hjcache-iphone-image-cache/ 获取 HJCache: HJCache is up on github h ...
- 解决ListView滑动时卡的问题,实现异步加载图片解决
ListView是最为常见的空间之一,现在的应用的呈现形式大多数都需要用到ListView来呈现,以列表的方式最直观最便于操作. 那么在使用的过程中大家一定使用adapter适配器来匹配这个ListV ...
- Android新浪微博客户端(七)——ListView中的图片异步加载、缓存
原文出自:方杰|http://fangjie.info/?p=193转载请注明出处 最终效果演示:http://fangjie.sinaapp.com/?page_id=54 该项目代码已经放到git ...
- Android之ListView异步加载图片且仅显示可见子项中的图片
折腾了好多天,遇到 N 多让人崩溃无语的问题,不过今天终于有些收获了,这是实验的第一版,有些混乱,下一步进行改造细分,先把代码记录在这儿吧. 网上查了很多资料,发现都千篇一律,抄来抄去,很多细节和完整 ...
- 【Android】纯代码创建页面布局(含异步加载图片)
开发环境:macOS 10.12 + Android Studio 2.2,MinSDK Android 5.1 先看看总体效果 本示例是基于Fragment进行的,直接上代码: [界面结构] 在 F ...
- Android-Universal-Image-Loader 图片异步加载类库的使用
在博客中看到一篇利用组件进行图片异步加载的文章在此作记录 原文:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载并缓存的 ...
随机推荐
- Ajax入门小例子
大牛文章:http://www.cnblogs.com/guduoduo/p/3681296.html ---Ajax基础学习 http:/ ...
- Redis集群明细文档
Redis目前版本是没有提供集群功能的,如果要实现多台Redis同时提供服务只能通过客户端自身去实现(Memchached也是客户端实现分布式).目前根据文档已经看到Redis正在开发集群功能,其中一 ...
- WCF-Configuration
Host-Configuration <?xml version="1.0"?> <configuration> <configSections> ...
- JavaScript 代码片段
1.无题 if (i && i.charAt(i.length - 1) == "/") { i = i.substr(0, i.length - 1) } 2.无 ...
- cocos2dx中的实现地图卷动的两种方式
在游戏当中,实现地图卷动是最基本的功能,具体的实现的方法,大致有两类: 方法一:加载两张图片,轮流显示, 优点: 1.无论是地图上下卷动,还是左右卷动都可以 2.支持各种图片,(png,jpg...) ...
- MVC与WebForm的一些区别
MVC与WebForm的一些区别 它们都是ASP.NET WEB开发的两种方式 .但是他们也是有一些不同.做个小结. 1.MVC是没有服务器端控件这么一说的,也就是没有viewstate,也就不会产生 ...
- ProgressDialog弹出时的底色变暗(转)
背景:在做一个进度条时,不想让它的背景变暗,以免影响其他区域的正常显示. 在网上搜索时,看到的方法多数是: 方法一 :在代码中 可以这么设置 Window mWindow = getWindow(); ...
- Enterprise Library 6——Using the Logging Application Block
原文参考 http://msdn.microsoft.com/en-us/library/dn440731(v=pandp.60).aspx 一.简介 .更重要的是用于审计.这种日志可以跟踪用户的行为 ...
- [搜片神器]winform程序自己如何更新自己的方法代码
DHT抓取程序开源地址:https://github.com/h31h31/H31DHTDEMO 数据处理程序开源地址:https://github.com/h31h31/H31DHTMgr 国外测试 ...
- RabbitMQ学习(1):安装
1.安装 Erlang,官网:https://www.erlang.org/ 2.安装RabbitMQ服务器,rabbitMQ server,官网http://www.rabbitmq.com/ 注: ...