ImageLoader加载图片

分享一个用于加载很多图片的类,虽然代码一看就懂,但是很多细节还是很值得学习的。
主要涉及到LruCache,线程池,图片压缩,单例模式,handler等用法
public class ImageLoader {
private LruCache<String, Bitmap> mMermoryCachre;
private ExecutorService mEService;
private static ImageLoader mLoader;
private ImageLoader(){
int maxSize=(int) Runtime.getRuntime().maxMemory()/4;
mMermoryCachre=new LruCache<String, Bitmap>(maxSize){
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes()*value.getHeight();
}
};
getThreadPool();
};
public static ImageLoader getInstance(){
synchronized (ImageLoader.class) {
if(mLoadernull){
mLoader=new ImageLoader();
}
return mLoader;
}
}
private void getThreadPool(){
mEService=Executors.newFixedThreadPool(2);
}
public Bitmap loadImage(final String path,final int width,final int height,final OnCallBackListener listener){
Bitmap bitmap=getFormCache(path);
final Handler handle=new Handler(){
@Override
public void handleMessage(Message msg) {
Bitmap bitmap=(Bitmap) msg.obj;
if(listener!=null){
listener.setOnCallBackListener(bitmap,path);
}
super.handleMessage(msg);
}
};
if(bitmap
null){
mEService.execute(new Thread(){
@Override
public void run() {
Bitmap bitmap=getThumbImage(path, width, height);
Message msg=new Message();
msg.obj=bitmap;
handle.sendMessage(msg);
addtoCache(path,bitmap);
}
});
return null;
}else{
return bitmap;
}

}
private Bitmap getFormCache(String path) {
	return mMermoryCachre.get(path);
}
public void cancleTask(){
	if(mEService!=null){
		mEService.shutdown();
	}
}
private void addtoCache(String path,Bitmap bitmap){
	if(bitmap!=null){			
		mMermoryCachre.put(path, bitmap);
	}
}
private Bitmap getThumbImage(String path,int width,int height){
	BitmapFactory.Options option=new BitmapFactory.Options();
	option.inJustDecodeBounds=true;
	BitmapFactory.decodeFile(path, option);
	option.inSampleSize=getScale(option,width,height);
	option.inJustDecodeBounds=false;
	return BitmapFactory.decodeFile(path, option);
	
}
private int getScale(Options options, int viewWidth, int viewHeight) {
	int inSampleSize = 1;  
    if(viewWidth == 0 || viewWidth == 0){  
        return inSampleSize;  
    }  
    int bitmapWidth = options.outWidth;  
    int bitmapHeight = options.outHeight;  
    if(bitmapWidth > viewWidth || bitmapHeight > viewWidth){  
        int widthScale = Math.round((float) bitmapWidth / (float) viewWidth);  
        int heightScale = Math.round((float) bitmapHeight / (float) viewWidth);  
        inSampleSize = widthScale < heightScale ? widthScale : heightScale;  
    }  
    return inSampleSize;  
}  

public interface OnCallBackListener{
	public void setOnCallBackListener(Bitmap bitmap,String url);
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值