public class MusicAdapter extends BaseAdapter{
private Context context;
private List<Music> musics;
private LayoutInflater inflater;
//声明图片下载任务集合
private List<ImageLoadTask> tasks=new ArrayList<ImageLoadTask>();
//声明用于轮循任务队列的工作线程
private Thread workThread;
private boolean isLoop=true;
private ListView listView;
//声明用于实现内存缓存的map
private Map<String, SoftReference<Bitmap>> cache = new HashMap<String, SoftReference<Bitmap>>();
//声明handler 显示图片
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case HANDLER_LOAD_IMAGE_SUCCESS: //图片下载成功
ImageLoadTask task = (ImageLoadTask) msg.obj;
Bitmap bitmap = task.bitmap;
//通过listView.findViewWithTag()方法获取相应的imageView
ImageView imageView=(ImageView) listView.findViewWithTag(task.path);
if(imageView != null){
if(bitmap!=null){
imageView.setImageBitmap(bitmap);
}else{
imageView.setImageResource(R.drawable.ic_launcher);
}
}
break;
}
}
};
public static final int HANDLER_LOAD_IMAGE_SUCCESS=1;
public MusicAdapter(Context context, List<Music> musics, ListView listView) {
this.context = context;
this.listView = listView;
this.musics = musics;
this.inflater = LayoutInflater.from(context);
//初始化并且启动workThread
workThread = new Thread(){
public void run() {
//不断轮循任务集合 一旦有任务 则获取然后执行下载操作
while(isLoop){
if(!tasks.isEmpty()){ //有任务
ImageLoadTask task=tasks.remove(0);
String path = task.path;
//下载图片
Bitmap bitmap = loadBitmap(path);
task.bitmap = bitmap;
//给imageView 设置 Bitmap
Message msg = new Message();
msg.what = HANDLER_LOAD_IMAGE_SUCCESS;
msg.obj = task;
handler.sendMessage(msg);
}else{ //没有任务 工作线程等待
try {
synchronized (workThread) {
workThread.wait();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
};
workThread.start();
}
/**
* 通过path 下载图片
* @param path
* @return
*/
public Bitmap loadBitmap(String path){
try {
InputStream is = HttpUtils.get(path);
Bitmap b = BitmapUtils.loadBitmap(is, 50, 50);
//下载成功 把b存入内存缓存中
cache.put(path, new SoftReference<Bitmap>(b));
//存入文件缓存目录中
String filename = path.substring(path.lastIndexOf("/")+1);
// f --> /data/data/cn.tedu.music/cache/xxxxx.jpg
File f = new File(context.getCacheDir(), filename);
BitmapUtils.save(b, f.getAbsolutePath());
return b;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public int getCount() {
return musics.size();
}
@Override
public Music getItem(int position) {
return musics.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null){
convertView = inflater.inflate(R.layout.item_lv_music, null);
holder = new ViewHolder();
holder.ivPic = (ImageView) convertView.findViewById(R.id.ivPic);
holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
holder.tvSinger = (TextView) convertView.findViewById(R.id.tvSinger);
convertView.setTag(holder);
}
holder=(ViewHolder) convertView.getTag();
//给holder中控件赋值
Music m = getItem(position);
holder.tvName.setText(m.getTitle());
holder.tvSinger.setText(m.getArtist_name());
//设置图片 从缓存中读取图片
SoftReference<Bitmap> ref=cache.get(m.getPic_small());
if(ref!=null){
Log.i("info", "从内存中获取的图片...");
Bitmap bitmap=ref.get();
if(bitmap!=null){ //图片还没有被销毁
holder.ivPic.setImageBitmap(bitmap);
return convertView;
}
}
//内存缓存中没有图片 则去文件缓存中读取
String path = m.getPic_small();
String filename = path.substring(path.lastIndexOf("/")+1);
File f = new File(context.getCacheDir(), filename);
Bitmap bitmap=BitmapUtils.loadBitmap(f.getAbsolutePath());
if(bitmap!=null){
Log.i("info", "从文件中获取的图片....");
holder.ivPic.setImageBitmap(bitmap);
//向内存缓存中再存一次
cache.put(m.getPic_small(), new SoftReference<Bitmap>(bitmap));
return convertView;
}
// 向任务集合中添加一个图片下载任务
//给imageView设置一个tag值 用于handler中通过tag值找到imageView
holder.ivPic.setTag(m.getPic_small());
ImageLoadTask task = new ImageLoadTask();
task.path = m.getPic_small();
tasks.add(task);
//唤醒工作线程 赶紧起来干活
synchronized (workThread) {
workThread.notify();
}
return convertView;
}
/**
* 停止线程
*/
public void stopThread(){
isLoop = false;
synchronized (workThread) {
//唤醒工作线程
workThread.notify();
}
}
class ViewHolder{
ImageView ivPic;
TextView tvName;
TextView tvSinger;
}
/**
* 封装一个图片下载任务
*/
class ImageLoadTask {
String path; //图片路径
Bitmap bitmap; //通过路径下载成功的bitmap
}
}
自写的ImageLoader用于缓存图片
最新推荐文章于 2024-03-28 09:47:26 发布