项目用到异步加载头像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 这个图片异步加载并缓存的 ...
随机推荐
- JavaScript中使用console调试程序的坑
上DEMO a = {key1: [1, 2], 'key2': {'key4': '11'}, 'key3': [1, 2]} console.info(1,a) a.key2.key4 = '22 ...
- WPF学习笔记1——XAML之1
参考文献: http://msdn.microsoft.com/zh-cn/library/ms752059(v=vs.110).aspx <Pro WPF 4.5 in C# > 一.X ...
- 【转】 申请对齐某种结构体大小的buffer
在大多数情况下,编译器和C库透明地帮你处理对齐问题.POSIX 标明了通过malloc( ), calloc( ), 和 realloc( ) 返回的地址对于任何的C类型来说都是对齐的.在Linux中 ...
- hadoop相关问题
发现一篇不错的文章,转一下.http://www.cnblogs.com/xuekyo/p/3386610.html HDFS导论(转) 1.流式数据访问 HDFS的构建思想是这样的:一次写入,多 ...
- 暂停更新Blog
今天非常不好意思的是老魏又要一次的暂停文章跟新了,原因是有些有问题老魏需要从新的梳理,加上这几天工作又开始忙碌起来了,所以这一阵子估计很难有有时间更新了. 不过老魏会抽一下时间更新文章的,不可能像2月 ...
- NET
NET狂官方面试题-数据库篇答案 题目:http://www.cnblogs.com/dunitian/p/6028838.html 汇总:http://www.cnblogs.com/dunit ...
- CSS去除Chrome浏览器的控件默认样式
html的input输入框在Chrome浏览器里是有默认样式的,当它获得焦点时,即使你没有为它设置:focus时的样式,Chrome浏览器还是会给它加上蓝色的边框,今天百度找到有个方法可以去除该默认样 ...
- ORACLE EXPDP命令使用详细【转】
本文转自:http://blog.csdn.net/zftang/article/details/6387325 ORACLE EXPDP命令使用详细 相关参数以及导出示例: 1. DIRECTORY ...
- angular-file-upload API angular文件上传插件
官方例子 : http://nervgh.github.io/pages/angular-file-upload/examples/simple/ ===Directives=== nvFileSel ...
- java多线程为什么要用while而不是if
对于java多线程的wait()方法,我们在jdk1.6的说明文档里可以看到这样一段话 从上面的截图,我们可以看出,在使用wait方法时,需要使用while循环来判断条件十分满足,而不是if,那么我们 ...