项目用到异步加载头像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 这个图片异步加载并缓存的 ...
随机推荐
- mysqlsla 分析mysql慢查询日志
发现有一个工具mysqlsla,分析查询日志比 mysqldumpslow分析的会更清晰明了! 安装mysqlsla: 下载mysqlsla-2.03.tar.gz [root@yoon export ...
- WPF-控件-ListView
<Window x:Class="DataTemplate2.MainWindow" xmlns="http://schemas.microsoft.com/win ...
- glog使用
How To Use Google Logging Library Glog 的基本使用方法在google code上有介绍:How To Use Google Logging Library ;最好 ...
- Luence简单实现1
初步认识Luence,简单按照官方文档做了个例子,大牛绕开,仅供小白路过参考.如有错误,欢迎指正批评. 建一个简单工程,并且加入这几个小奶瓶,如下图: 注:版本不同,可能对jdk的需求是不同的,这个需 ...
- ASP.Net MVC4 登录
一月之前,工作室师兄给我们介绍博客,然后试着去申请了一个,一直不晓得更新什么,直到今天,才有了一点小想法.最近在做一个小网站,然后就在登录那一块犯愁了,止步不前. 以前对登录一直没有什么概念,以为登录 ...
- jquery(1.3.2)<--json-->spring(3.0)
发现spring 3已经对ajax支持的很好了,前端可以只使用html+jquery,后端 只使用spring再加上一种orm,两者之间用json交换数据就可以了,现在是放弃 jsp,struts这些 ...
- 剑指offer--面试题6
题目:由前序.中序遍历序列重建二叉树 虽然思路能想到,但是实际写却无从下手...下面重现作者代码,多多实践... #include<exception> //首先定义二叉树节点 struc ...
- uva 534
floyd算法 数据量比较小 就简单了~ /************************************************************************* > ...
- spoj 1108
要求输出一个牌的顺序 使每隔1.2.......n翻牌后出现1 2 3 4 5 6 7 8 9 .... n 将牌想象成n个空格 正向推 空n个位置放n 循环 需优化 #include <io ...
- UVA 10000 Longest Paths (SPFA算法,模板题)
题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...