ListView中加载大量图片处理问题优化,效率还行

找到组织 发布于 2013/11/07 18:07
阅读 3K+
收藏 3

今天下午写了一个小程序主要是处理listView下面对于大量图片的加载问题生成指定的ThumbNails:


import java.io.File;


import android.content.Context;

import android.graphics.Bitmap;

import android.os.Handler;

import android.os.Message;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;

import android.widget.TextView;


import com.example.thumbnails.R;

import com.thumbnails.ImageLoader.ImageCallBack;


public class ListAdapter extends BaseAdapter {


private File[] lists;

private LayoutInflater inflater;

private Context context;

private int COUNT= 8;

private ThumbNailsThread[] threads= new ThumbNailsThread[8];

private ImageLoader ImageLoader = new ImageLoader();

 

public ListAdapter(File[] lists, Context context) {


this.lists= lists;

this.context= context;

inflater= LayoutInflater.from(context);

}


@Override

public int getCount() {


return lists.length;

}


@Override

public Object getItem(int position) {

return null;

}


@Override

public long getItemId(int position) {



return position;

}


@Override

public View getView(int position, View convertView, ViewGroup parent) {



ContentHolder holder= null;

if(convertView!= null) {


holder= (ContentHolder) convertView.getTag();

}else {


convertView= inflater.inflate(R.layout.item, null);

holder= new ContentHolder();

holder.icon= (ImageView) convertView.findViewById(R.id.thumbnail);

holder.name= (TextView) convertView.findViewById(R.id.item_name);

holder.size= (TextView) convertView.findViewById(R.id.item_size);

convertView.setTag(holder);

}

setImage(holder,position);

// setBitmap(holder,position);

holder.name.setText(lists[position].getName());

holder.size.setText((lists[position].length()/ 1000/ 1000.0)+ "M");

return convertView;

}


private void setImage(final ContentHolder holder, int position) {


ImageLoader.loadDrawable(lists[position].getAbsolutePath(), 40, 40, new ImageCallBack() {


@Override

public void imageLoaded(Bitmap drawable) {


holder.icon.setImageBitmap(drawable);

}

}, new Handler(){


@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

Bitmap bitmap= (Bitmap) msg.obj;

holder.icon.setImageBitmap(bitmap);

}

});

}


private void setBitmap(final ContentHolder holder, int position) {


Handler handler= new Handler(){


@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

Bitmap bitmap= (Bitmap) msg.obj;

holder.icon.setImageBitmap(bitmap);

}

};

// if(position< 8) {


ThumbNailsThread thread= new ThumbNailsThread(handler, lists[position].getAbsolutePath(), 40, 40);

threads[0]= thread;

thread.start();

// }else {

//

// int index= position% 8;

// threads[index]

// }

}


class ContentHolder {


TextView name;

TextView size;

ImageView icon;

}

}

package com.thumbnails;


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


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;


public class ImageLoader {
// 为了加快速度,在内存中开启缓存(主要应用于重复图片较多时,或者同一个图片要多次被访问,比如在ListView时来回滚动)
public Map<String, SoftReference<Bitmap>> imageCache = new HashMap<String, SoftReference<Bitmap>>();
private ExecutorService executorService = Executors.newFixedThreadPool(20); // 固定五个线程来执行任务
private ImageCallBack callBack;
public Bitmap loadDrawable(final String imageUrl, final int width, final int height, final ImageCallBack callBack, final Handler handler) {
// 如果缓存过就从缓存中取出数据
if (imageCache.containsKey(imageUrl)) {
SoftReference<Bitmap> softReference = imageCache.get(imageUrl);
if (softReference.get() != null) {
return softReference.get();
}
}
// 缓存中没有图像,则从网络上取出数据,并将取出的数据缓存到内存中
executorService.submit(new Thread() {
public void run() {
try {

final Bitmap drawable = loadImageFromUrl(imageUrl,width, height);
imageCache.put(imageUrl, new SoftReference<Bitmap>(drawable));
Message msg= new Message();
msg.obj= drawable;
handler.sendMessage(msg);
} catch (Exception e) {

throw new RuntimeException(e);
}
}
});
return null;
}


// 从网络上取数据方法
protected Drawable loadImageFromUrl(String imageUrl, boolean flag) {
try {
// 测试时,模拟网络延时,实际时这行代码不能有
SystemClock.sleep(2000);


// return Drawable.createFromStream(new URL(imageUrl).openStream(),
// "image.png");

return Drawable.createFromPath(imageUrl);
} catch (Exception e) {
throw new RuntimeException(e);
}
}


protected Bitmap loadImageFromUrl(String path, int width, int height) {
Bitmap bitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// 获取这个图片的宽和高,注意此处的bitmap为null
bitmap = BitmapFactory.decodeFile(path, options);
options.inJustDecodeBounds = false; // 设为 false
// 计算缩放比
int h = options.outHeight;
int w = options.outWidth;
int beWidth = w / width;
int beHeight = h / height;
int be = 1;
if (beWidth < beHeight) {
be = beWidth;
} else {
be = beHeight;
}
if (be <= 0) {
be = 1;
}
options.inSampleSize = be;
// 重新读入图片,读取缩放后的bitmap,注意这次要把options.inJustDecodeBounds 设为 false
bitmap = BitmapFactory.decodeFile(path, options);
// 利用ThumbnailUtils来创建缩略图,这里要指定要缩放哪个Bitmap对象
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
return bitmap;
}

public interface ImageCallBack {

public void imageLoaded(final Bitmap drawable);
}
}

主要是通过多线程来处理,其中有参考过别人的东西,谢谢啊。大家有什么好的建议欢迎啊。

加载中
返回顶部
顶部