Listview中加载大量缩略图(优化)

在Android 2.2版本中,新增了一个ThumbnailUtils工具类来是实现缩略图,此工具类的功能是强大的,使用是简单,它提供了一个常量和三个方法。利用这些常数和方法,可以轻松快捷的实现图片和视频的缩略图功能。

如果加载的缩略图不多,可以直接使用:

//得到原图片
Bitmapbitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xx);
或者
Bitmapbitmap= BitmapFactory.decodeFile(path//图片的路径, null);

 //得到缩略图
bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100,100);//100表示图片缩略后的宽和高  
image.setImageBitmap(bitmap);  

如果加载的图片数量过多就不能单一使用了,否则会卡的要命。

在这里,我采用多线程的方式来做优化处理:
先创建一个工具类
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 { 
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); 


然后在适配器中定义private ImageLoader ImageLoader = new ImageLoader();

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

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

@Override

public void imageLoaded(Bitmap drawable) {

holder.image.setImageBitmap(drawable);

}

}, new Handler(){

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

Bitmap bitmap= (Bitmap) msg.obj;

holder.image.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.image.setImageBitmap(bitmap);

}

};

在适配器getview中显示缩略图setImage(holder,position);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值