Android 弱引用二级缓存实现

package com.example.gp11_day10_news.adapter;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.example.gp11_day10_news.R;
import com.example.gp11_day10_news.asynctask.DownImageTask;
import com.example.gp11_day10_news.entity.News;
import com.example.gp11_day10_news.tool.Urls;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsAdapter extends BaseAdapter {

    private Context context;
    private List<News> datas;

    /*private Object obj = new Object();//强引用---在任何情况下都不会被回收 //SoftReference----当内存不足时会回收软引用的对象 String ss = "hello"; SoftReference<String> soft = new SoftReference<String>(ss); soft.get();//hello 得到软引用指向的对象*/

    private ExecutorService es = Executors.newFixedThreadPool(2);

    //使用一级缓存,也就是内存来对图片进行缓存
    //map集合的键是图片的地址,值是图片的软引用
    //把第一次下载的图片存入map,当再次下载时先从缓存中判断是否有该图片
    private Map<String,SoftReference<Bitmap>> imageCatche = new HashMap<String,SoftReference<Bitmap>>();



    public NewsAdapter(Context context, List<News> datas) {
        super();
        this.context = context;
        this.datas = datas;
    }

    @Override
    public int getCount() {
        int count = 0;
        if(datas!=null)
            count = datas.size();
        return count;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, final ViewGroup parent) {
        if(convertView==null)
        {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
            ViewHolder holder =new ViewHolder();
            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);
            holder.text_subject = (TextView) convertView.findViewById(R.id.text_subject);
            holder.text_summary = (TextView) convertView.findViewById(R.id.text_summary);
            holder.text_changed = (TextView) convertView.findViewById(R.id.text_changed);

            convertView.setTag(holder);
        }
        ViewHolder holder = (ViewHolder) convertView.getTag();

        News news = datas.get(position);
        holder.text_subject.setText(news.getSubject());
        holder.text_summary.setText(news.getSummary());
        holder.text_changed.setText(news.getChanged());

        //得到图片路径
        String imageUrl = "http://litchiapi.jstv.com"+news.getCover();
        holder.imageView.setTag(imageUrl);//防止图片错位
        holder.imageView.setImageResource(R.drawable.ic_launcher);

        //先取缓存中查找是否有需要下载的图片
        SoftReference<Bitmap> soft = imageCatche.get(imageUrl);
        if(soft!=null)
        {
            Bitmap bitmap = soft.get();
            if(bitmap!=null)
                holder.imageView.setImageBitmap(bitmap);
            else
                //使用异步任务下载图片
                downImage(imageUrl,parent);
        }
        else
            //使用异步任务下载图片
            downImage(imageUrl,parent);


        //使用异步任务下载图片
// new DownImageTask(new DownImageTask.DownBack() {
// 
// @Override
// public void response(String url, Bitmap bitmap) {
// 
// ImageView imageView = (ImageView) parent.findViewWithTag(url);
// if(imageView!=null)
// imageView.setImageBitmap(bitmap);
// }
// } ).execute(imageUrl);

        return convertView;
    }

    //下载图片的函数
    public void downImage(final String imageUrl,final ViewGroup parent)
    {
        new DownImageTask(new DownImageTask.DownBack() {

            @Override
            public void response(String url, Bitmap bitmap) {

                ImageView imageView = (ImageView) parent.findViewWithTag(url);
                if (imageView != null)
                    imageView.setImageBitmap(bitmap);
                //把图片放入缓存
                imageCatche.put(url, new SoftReference<Bitmap>(bitmap));
            }
        }).executeOnExecutor(es, imageUrl);
    }

    class ViewHolder
    {
        ImageView imageView;
        TextView text_subject,text_summary,text_changed;
    }

}




你可能感兴趣的:(二级缓存)