开发者社区> ctrip_xzh> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

android使用LruCache对listview加载图片时候优化处理

简介: <p style="line-height:19px; margin:10px auto; color:rgb(75,75,75); font-family:Verdana,Geneva,Arial,Helvetica,sans-serif; font-size:13px"> 注意:LruCache是有版本限制的,低版本的sdk需要在libs文件夹添加相应的support-4v文件。<b
+关注继续查看

注意:LruCache是有版本限制的,低版本的sdk需要在libs文件夹添加相应的support-4v文件。
本文改造的大部分是参考http://www.iteye.com/topic/1118828,感谢。
不废话直接上工程代码,内有关键注释,项目就不上传了,自己对照着上面网址改呗。


首先是Application文件,负责创建图片存储文件夹:

复制代码
public class MyApp extends Application{
    @Override
    public void onCreate() {
        super.onCreate();
        File f = new File(Environment.getExternalStorageDirectory()+"/TestSyncListView/pic/");
        if (!f.exists()) {
            f.mkdirs();
        }
    }
}
复制代码


图像读取工具类:

?
public class SyncImageLoaderUtil {
    private Object lock = new Object(); 
       
    private boolean mAllowLoad = true
   
    private boolean firstLoad = true
   
    private int mStartLoadLimit = 0
   
    private int mStopLoadLimit = 0
   
    final Handler handler = new Handler(); 
   
//    private HashMap<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>(); 
   
    private LruCache<String,Bitmap> mMemoryCache;
     
    RunInOtherThread runInOutherThread; 
   
    public SyncImageLoaderUtil(Context context) { 
        super(); 
         
        int memClass = ((ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
        int cacheSize = 1024 *1024 *memClass / 8;
        mMemoryCache = new LruCache<String, Bitmap>(cacheSize){
            @Override
            protected int sizeOf(String key, Bitmap value) {
                // TODO Auto-generated method stub
                return value.getRowBytes();
            }
        };
         
        runInOutherThread = new RunInOtherThread(); 
        runInOutherThread.start(); 
    
   
    public interface OnImageLoadListener { 
        public void onImageLoad(Integer t, Drawable drawable); 
   
        public void onError(Integer t); 
    
   
    public void setLoadLimit(int startLoadLimit,int stopLoadLimit) { 
        if (startLoadLimit > stopLoadLimit) { 
//          LogUtil.i("test", startLoadLimit+"--错误---"+stopLoadLimit);
            return
        
        mStartLoadLimit = startLoadLimit; 
        mStopLoadLimit = stopLoadLimit; 
    
   
    public void restore() { 
        mAllowLoad = true
        firstLoad = true
    
   
    public void lock() { 
        mAllowLoad = false
        firstLoad = false
    
   
    public void unlock() { 
        mAllowLoad = true
        synchronized (lock) { 
            lock.notifyAll(); 
        
    
   
    public void loadImage(Integer t, String imageUrl, 
            OnImageLoadListener listener) { 
        final OnImageLoadListener mListener = listener; 
        final String mImageUrl = imageUrl; 
        final Integer mt = t; 
           
        runInOutherThread.getHandler().post(new Runnable() { 
   
            @Override 
            public void run() { 
                if (!mAllowLoad) { 
                    synchronized (lock) { 
                        try 
                            lock.wait(); 
                        }catch (InterruptedException e) { 
                            // TODO Auto-generated catch block 
                            e.printStackTrace(); 
                        
                    
                
                   
                if (mAllowLoad && firstLoad) { 
                    loadImage(mImageUrl, mt, mListener); 
                
   
//                LogUtil.e("test", "原始开始:"+mStartLoadLimit+"原始当前位置:"+mt+"原始结束:"+mStopLoadLimit);
                if (mAllowLoad && mt <= mStopLoadLimit && mt >= mStartLoadLimit) { 
//                  LogUtil.e("test", "开始:"+mStartLoadLimit+"当前位置:"+mt+"结束:"+mStopLoadLimit);
                    loadImage(mImageUrl, mt, mListener);
                
            
   
        }); 
    
       
    private void loadImage(final String mImageUrl, final Integer mt, 
            final OnImageLoadListener mListener) { 
   
        if (mImageUrl!=null && mMemoryCache.get(mImageUrl)!=null) { 
//            SoftReference<Drawable> softReference = imageCache.get(mImageUrl); 
            final Drawable d = new BitmapDrawable(mMemoryCache.get(mImageUrl)); 
//            LogUtil.d("ppp", "drawable:"+d);
            if (d != null) { 
                handler.post(new Runnable() { 
                    @Override 
                    public void run() { 
                        if (mAllowLoad) { 
                            mListener.onImageLoad(mt, d); 
                        
                    
                }); 
                return
            
        
        try 
            final Drawable d = loadImageFromUrl(mImageUrl); 
            if (d != null) { 
                mMemoryCache.put(mImageUrl, ((BitmapDrawable)d).getBitmap());
            
            handler.post(new Runnable() { 
                @Override 
                public void run() { 
                    if (mAllowLoad) { 
                        mListener.onImageLoad(mt, d); 
                    
                
            }); 
        }catch (IOException e) { 
            handler.post(new Runnable() { 
                @Override 
                public void run() { 
                    mListener.onError(mt); 
                
            }); 
            e.printStackTrace(); 
        
    
   
    public static Drawable loadImageFromUrl(String url) throws IOException { 
        //DebugUtil.debug(url); 
        if (Environment.getExternalStorageState().equals( 
                Environment.MEDIA_MOUNTED)) { 
            File f = new File(Environment.getExternalStorageDirectory() 
                    +"/Weiyu/pic/" + MD5Util.getMD5(url.getBytes())); 
            if (f.exists()) { 
                FileInputStream fis = new FileInputStream(f); 
                Drawable d = Drawable.createFromStream(fis, "src"); 
                return d; 
            
            URL m = new URL(url); 
            InputStream i = (InputStream) m.getContent(); 
            DataInputStream in = new DataInputStream(i); 
            FileOutputStream out = new FileOutputStream(f); 
            byte[] buffer = new byte[1024]; 
            int byteread = 0
            while ((byteread = in.read(buffer)) != -1) { 
                out.write(buffer,0, byteread); 
            
            
            in.close(); 
            out.close();
            return loadImageFromUrl(url); 
        }else 
            URL m = new URL(url); 
            InputStream i = (InputStream) m.getContent(); 
            Drawable d = Drawable.createFromStream(i, "src"); 
            return d; 
        
   
    
}

  
线程辅助类:

?
public class RunInOtherThread {
    private static final String LOG_TAG = "RunInOtherThread"
     
    private LooperThread localThread = new LooperThread(); 
       
    private boolean isRunning = true
   
    public Handler getHandler(){ 
        return localThread.getHandler(); 
    
       
    private class LooperThreadextends Thread { 
        private Handler mHandler; 
   
        public void run() { 
            Looper.prepare(); 
            mHandler = new Handler() { 
                public void handleMessage(Message msg) { 
                    onReceiveMessage(msg.what); 
                
            }; 
            Looper.loop(); 
        
           
        Handler getHandler(){ 
            return mHandler; 
        
      
    
       
    public void start(){ 
        localThread.start(); 
    
       
    public void quit(){ 
        localThread.getHandler().getLooper().quit(); 
    
       
    public void sendMessage(int what){ 
        getHandler().sendEmptyMessage(what); 
    
       
    public Thread getThread(){ 
        return localThread; 
    
       
    public void onReceiveMessage(int what){};
}

  
使用类:

?
// 实例化工具类
SyncImageLoaderUtil syncImageLoader = new SyncImageLoaderUtil(mContext);
 
syncImageLoader.loadImage(position, model.mPic, imageLoadListener);//应用接口:参数一是加载图片的位置;参数二是加载的ImageView;参数三是回调接口
 
// map保存的键是位置,值是listview对应位置的布局
HashMap map = new HashMap();
map.put(position, convertView);
 
SyncImageLoaderUtil.OnImageLoadListener imageLoadListener = new SyncImageLoaderUtil.OnImageLoadListener() {
 
        @Override
        public void onImageLoad(Integer t, Drawable drawable) {
            View view = (View) map.get(t);
            if (view != null) {
                ImageView iv = (ImageView) view.findViewById(R.id.image);
                iv.setBackgroundDrawable(drawable);
            }
        }
 
        @Override
        public void onError(Integer t) {
                        // 图片加载失败
                       // 取得listview对应的位置的行的内容布局
                    MusicModel model = (MusicModel) getItem(t);
            View view = mListView.findViewWithTag(model);
            if (view != null) {
                ImageView iv = (ImageView) view.findViewById(R.id.image);
                iv.setBackgroundResource(R.drawable.img_pic);
            }
        }
 
    };
 
 
// 实现类而且需要实现OnScrollListener接口
public void loadImage() {
                // 不要在这里使用listview的getFirstVisiblePosition方法,位置不准
        if (end >= getCount()) {
            end = getCount() - 1;
        }
        syncImageLoader.setLoadLimit(start, end);
        syncImageLoader.unlock();
    }
 
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        // TODO Auto-generated method stub
        if (lodingView) {
            switch (scrollState) {
            case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
                syncImageLoader.lock();
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
 
                loadImage();
                break;
            case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
                syncImageLoader.lock();
                break;
            default:
                break;
            }
        }
    }
 
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem,
            int visibleItemCount,int totalItemCount) {
        // 在这里取得的位置较准确,不过也会出现特殊的奇疤机型是不行的
                // start与end是定义的变量
        start = firstVisibleItem;
        end = firstVisibleItem + visibleItemCount;
        if (firstVisibleItem != 0) {
                        // lodingView是控制变量,用来控制第一次进来视图加载读取图片
            lodingView = true;
        }else {
            lodingView = false;
            loadImage();
        }
    }

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
Android ListView优化之局部刷新(更新)(非notifyDataSetChanged)
Android ListView优化之局部刷新(更新)(非notifyDataSetChanged)
0 0
Android零基础入门第43节:ListView优化和列表首尾使用
原文:Android零基础入门第43节:ListView优化和列表首尾使用    前面连续几期都在学习ListView的各种使用方法,如果细心的同学可能会发现其运行效率是有待提高的,那么本期就来一起学习有哪些方法技巧来优化ListView的效率。
1201 0
Android--Listview优化
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/52797308 Androi...
704 0
Android官方开发文档Training系列课程中文版:布局性能优化之ListView的优化
原文地址:http://android.xsoftlab.net/training/improving-layouts/smooth-scrolling.html 想要让ListView滑动流畅的关键所在是减轻主线程的负担。
551 0
Android实训案例(五)——四大组件之一ContentProvider的使用,通讯录的实现以及ListView的优化
<div class="markdown_views"> <h1 id="android实训案例五四大组件之一contentprovider的使用通讯录的实现">Android实训案例(五)——四大组件之一ContentProvider的使用,通讯录的实现</h1> <hr> <blockquote> <p>Android四大组件是啥这里就不用多说了,看图吧,他们之间通过
1059 0
[Android]ListView的Adapter.getView()方法中延迟加载图片的优化
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html   举个例子吧,以好友列表为例 ListView中每个Item表示一个好友,每个好友中都有一个头像,需要从服务端加载到本地,然后显示在item中。
637 0
Android Studio进行APP设计开发之矢量图及XML文件转换
Android Studio进行APP设计开发之矢量图及XML文件转换
0 0
+关注
文章
问答
文章排行榜
最热
最新
相关电子书
更多
Android组件化实现
立即下载
蚂蚁聚宝Android秒级编译—— Freeline
立即下载
Android插件化:从入门到放弃
立即下载