1.首先网络权限
2.导包
3.配置新的Manifest 加入 android:name="com.example.week3_test.MyApplication"的权限
4.关于MyApplication,暂时复制即可
private File file;
@Override
public void onCreate() {
file = new File("path");
// 初始化imageloader
initImageloader();
}
/**
*
*/
private void initImageloader() {
int maxMemory = ((int) Runtime.getRuntime().maxMemory()) / 1024 / 1024;
System.out.println("最大堆内存" + maxMemory);
// 获得 imageLoader实例
ImageLoader imageLoader = ImageLoader.getInstance();
// 全局配置imageLoader的属性
ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(
this)
.
// 最大缓存数
diskCacheSize(100)
.
// 指定加载文件的磁盘缓存路径
// 指定内存缓存策略,此处是通过使用的频率,限制内存的使用
// ) UsingFreqLimitedCache (最少被用到的对象会被删除)
// 3) UsingAgeLimitedCache (最早被添加的对象会被删除)
// 4) LargestLimitedCache (空间占用最大的对象会被删除)
//FIFOLimitedCache //(根据先进先出的原则上删除多余对象)
memoryCache(new FIFOLimitedMemoryCache(5 * 1024 * 1024));
// 初始化imageLoader
imageLoader.init(builder.build());
}
5.在使用时
<pre name="code" class="java">ImageLoader.getInstance().displayImage(str_arr[position], viewHodler.imageView);
第一个参数就是地址,第二个参数是imageView
下边的不懂
// 加载图片时的属性,比如,加载失败显示的图片,是否把图片缓存的内存中.....
DisplayImageOptions options = new DisplayImageOptions.Builder()
// 图片路径为空的默认显示
.showImageForEmptyUri(R.drawable.ic_launcher)
// 图片的显示格式
.bitmapConfig(Bitmap.Config.RGB_565)
.showImageOnFail(R.drawable.icon_head)
// 图片加载中,显示的默认图片
.showImageOnLoading(R.drawable.bar1)
.showImageForEmptyUri(R.drawable.icon_head).build();