//创建Application类在配置文件中写name属性、要有写的权限
package com.bwie.imageloaer;
import android.app.Application;
import android.os.Environment;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import java.io.File;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
String path = Environment.getExternalStorageDirectory()+"/1507D";
File cacheDir = new File(path);
//希望有缓存,指明缓存的路径
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
//指明内存缓存要缓存多少*多少像素图片 480 * 800
.memoryCacheExtraOptions(480,800)
//指明线程优先级
.threadPriority(100)
//配置多少个线程在后台加载图片
.threadPoolSize(3)
//指明图片sdcard缓存,缓存到什么地方
.diskCache(new UnlimitedDiskCache(cacheDir))
//限定一下内存缓存的大小 在内存缓存当中缓存2MB的大小图片
.memoryCacheSize(2 * 1024 * 1024)
//在sdcard缓存多少MB的图片
.diskCacheSize(50 * 1024 * 1024)
//指明图片文件名(MD5) 为了避免有重复的图片
.diskCacheFileNameGenerator(new Md5FileNameGenerator())
.build();
ImageLoader.getInstance().init(config);
}
}
//baseAdapter中配置ImageLoad信息
package com.bwie.imageloader;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
public class MyBaseAdapter extends BaseAdapter {
private Context context;
private List list;
private DisplayImageOptions options;
public MyBaseAdapter(Context context,List list){
this.context = context;
this.list = list;
options = new DisplayImageOptions.Builder()
.cacheInMemory(true)
.cacheOnDisk(true)
.showImageForEmptyUri(R.mipmap.ic_empty)
.showImageOnFail(R.mipmap.ic_error)
.showImageOnLoading(R.mipmap.loading)
.build();
}
@Override
public int getCount() {
return list != null ? list.size() : 0;
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = convertView.inflate(context,R.layout.item,null);
viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.ivIcon);
viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
//设置文本信息以及图片加载
viewHolder.tvContent.setText(list.get(position).getTitle());
//加载图片
ImageLoader.getInstance().displayImage(list.get(position).getImg(),viewHolder.ivIcon,options);
return convertView;
}
class ViewHolder{
ImageView ivIcon;
TextView tvContent;
}
}