-
.denyCacheImageMultipleSizesInMemory()
-
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
-
.memoryCacheSize(2 * 1024 * 1024)
-
.memoryCacheSizePercentage(13) // default
-
.diskCache(new UnlimitedDiscCache(cacheDir)) // default
-
.diskCacheSize(50 * 1024 * 1024)
-
.diskCacheFileCount(100)
-
.diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
-
.imageDownloader(new BaseImageDownloader(context)) // default
-
.imageDecoder(new BaseImageDecoder()) // default
-
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
-
.writeDebugLogs()
-
.build();
上面的这些就是所有的选项配置,我们在项目中不需要每一个都自己设置,一般使用createDefault()创建的ImageLoaderConfiguration就能使用,然后调用ImageLoader的init()方法将ImageLoaderConfiguration参数传递进去,ImageLoader使用单例模式。
配置Android Manifest文件
[html] view plain copy
-
<manifest>
-
<uses-permission android:name=“android.permission.INTERNET” />
-
-
<uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE” />
-
…
-
<application android:name=“MyApplication”>
-
…
-
</application>
-
</manifest>
接下来我们就可以来加载图片了,首先我们定义好Activity的布局文件
[html] view plain copy
- <?xml version\="1.0" encoding\="utf-8"?>
-
<FrameLayout xmlns:android=“http://schemas.android.com/apk/res/android”
-
android:layout_width=“fill_parent”
-
android:layout_height=“fill_parent”>
-
<ImageView
-
android:layout_gravity=“center”
-
android:id=“@+id/image”
-
android:src=“@drawable/ic_empty”
-
android:layout_width=“wrap_content”
-
android:layout_height=“wrap_content” />
-
</FrameLayout>
里面只有一个ImageView,很简单,接下来我们就去加载图片,我们会发现ImageLader提供了几个图片加载的方法,主要是这几个displayImage(), loadImage(),loadImageSync(),loadImageSync()方法是同步的,android4.0有个特性,网络操作不能在主线程,所以loadImageSync()方法我们就不去使用
.
loadimage()加载图片
我们先使用ImageLoader的loadImage()方法来加载网络图片
[java] view plain copy