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的更多相关文章

  1. UniversalImageLoader(异步加载大量图片)

    UniversalImageLoader是用于加载图片的一个开源项目,UniversalImageLoader是实现异步加载大量图片的源码和例子,包括缓存.硬盘缓存.容错机制等技术.在其项目介绍中是这 ...

  2. cocos2dx lua中异步加载网络图片,可用于显示微信头像

    最近在做一个棋牌项目,脚本语言用的lua,登录需要使用微信登录,用户头像用微信账户的头像,微信接口返回的头像是一个url,那么遇到的一个问题就是如何在lua中异步加载这个头像,先在引擎源码里找了下可能 ...

  3. Python 爬虫练习项目——异步加载爬取

    项目代码 from bs4 import BeautifulSoup import requests url_prefix = 'https://knewone.com/discover?page=' ...

  4. 关于ios异步加载图片的几个开源项目

    一.HjCache  原文:http://www.markj.net/hjcache-iphone-image-cache/ 获取 HJCache: HJCache is up on github h ...

  5. 解决ListView滑动时卡的问题,实现异步加载图片解决

    ListView是最为常见的空间之一,现在的应用的呈现形式大多数都需要用到ListView来呈现,以列表的方式最直观最便于操作. 那么在使用的过程中大家一定使用adapter适配器来匹配这个ListV ...

  6. Android新浪微博客户端(七)——ListView中的图片异步加载、缓存

    原文出自:方杰|http://fangjie.info/?p=193转载请注明出处 最终效果演示:http://fangjie.sinaapp.com/?page_id=54 该项目代码已经放到git ...

  7. Android之ListView异步加载图片且仅显示可见子项中的图片

    折腾了好多天,遇到 N 多让人崩溃无语的问题,不过今天终于有些收获了,这是实验的第一版,有些混乱,下一步进行改造细分,先把代码记录在这儿吧. 网上查了很多资料,发现都千篇一律,抄来抄去,很多细节和完整 ...

  8. 【Android】纯代码创建页面布局(含异步加载图片)

    开发环境:macOS 10.12 + Android Studio 2.2,MinSDK Android 5.1 先看看总体效果 本示例是基于Fragment进行的,直接上代码: [界面结构] 在 F ...

  9. Android-Universal-Image-Loader 图片异步加载类库的使用

    在博客中看到一篇利用组件进行图片异步加载的文章在此作记录 原文:http://blog.csdn.net/vipzjyno1/article/details/23206387 这个图片异步加载并缓存的 ...

随机推荐

  1. 1095. Cars on Campus (30)

    Zhejiang University has 6 campuses and a lot of gates. From each gate we can collect the in/out time ...

  2. 1093. Count PAT's (25)

    The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and ...

  3. 1014. Waiting in Line (30)

    Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...

  4. 拥抱ARM妹纸第二季 之 第二次 约会需要浪漫,这么大灯泡怎么弄?

    终于轮到俺的小穆出场啦.有请能让太阳也为之暗淡的小穆闪亮登场-,鼓掌吧,欢呼吧!-- ♪♪ We can burn brighter Than the sun ~~~ ♪♪ “谢谢---“ 唱的太棒啦 ...

  5. Oracle中的注释

    注释用于对程序代码的解释说明,它能够增强程序的可读性,是程序易于理解. 单行注释: 用“--”,后面跟上注释的内容 Declare Num_sal number; --声明一个数字类型的变量 Var_ ...

  6. Eclipse中查看JDK类库的源代码

    在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...

  7. python之super()函数

    python之super()函数 python的构造器奇特, 使用魔方. 构造器内对基类对象的初始化同样也很奇特, 奇特到没有半点优雅! 在构造器中使用super(class, instance)返回 ...

  8. .NET中class和struct的区别

    1.引言 提起class和struct,我们首先的感觉是语法几乎相同,待遇却天壤之别.历史将接力棒由面向过程编程传到面向对象编程,class和struct也背负着各自的命运前行.在我认为,struct ...

  9. java集合类(四)About Set

    接上篇:java集合类(三)About Iterator & Vector(Stack) 之前,在比较java常见集合类的时候,就了解到一点有关Set的特性.实现类及其要求等,读者可以去温习下 ...

  10. 【工具】NS2安装记录

    献给同样为了NS2抓破了头皮的同志们. 1, Get Started: http://www.isi.edu/nsnam/ns/ns-build.html#allinone Build by piec ...