权限
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1、注册全局的 Universal Image Loader,在Application里面注册。
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
long size = Runtime.getRuntime().maxMemory();
int mysize = (int) (size / 8);
File cacheDir;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// String SDurl = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = Environment.getExternalStorageDirectory();
//自定义文件名称
String fileName = "xxxx";
cacheDir = new File(dir, fileName);
} else {
cacheDir = StorageUtils.getCacheDirectory(this); // 自定义缓存文件夹
}
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.threadPoolSize(5)//线程池
.tasksProcessingOrder(QueueProcessingType.FIFO)
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new UsingFreqLimitedMemoryCache(mysize))
.memoryCacheSize(mysize)
.threadPriority(Thread.NORM_PRIORITY - 2) // default ,指定线程优先级
.diskCache(new UnlimitedDiskCache(cacheDir)) // default , 指定硬盘缓存的地址
.diskCacheFileCount(100) // 指定硬盘缓存的文件个数
.build();
ImageLoader.getInstance().init(config);
}
}
2、在清单文件(AndroidManifest)中,application的name改成我们自定义的MyApplication。
<application
android:name=".application.MyApplication"
android:allowBackup="true"
android:icon="@mipmap/icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
3、写一个 Universal Image Loader的工具类,一般写成单例。
public class DownLoadTool {
private Context context;
public DisplayImageOptions options;
public DisplayImageOptions options1;
public ImageLoader imageLoader;
private static DownLoadTool downLoadTool;
public static DownLoadTool getInstance(Context context) {
if (downLoadTool == null) {
downLoadTool = new DownLoadTool(context);
}
return downLoadTool;
}
private DownLoadTool(Context context) {
this.context = context;
initTools();
}
private void initTools() {
/**
* ImageLoader
*/
imageLoader = ImageLoader.getInstance();
//一般的参数
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.imagedefault_small) // 图片正在加载时显示的图片资源ID
.showImageForEmptyUri(R.mipmap.imagedefault_small) // URI为空时显示的图片资源ID
.showImageOnFail(R.mipmap.imagedefault_small) // 图片加载失败时显示的图片资源ID
.resetViewBeforeLoading(true)
.cacheInMemory(true) // default , 是否缓存在内存中, 默认不缓存
.cacheOnDisk(true) // default , 是否缓存在硬盘 , 默认不缓存
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default , 指定图片缩放的方式,ListView/GridView/Gallery推荐使用此默认值
.bitmapConfig(Bitmap.Config.RGB_565) // default , 指定图片的质量,默认是 ARGB_8888
.displayer(new FadeInBitmapDisplayer(100))
.build();
//圆形图片的参数
options1 = new DisplayImageOptions.Builder()
.showImageOnLoading(R.mipmap.imagedefault_small) // 图片正在加载时显示的图片资源ID
.showImageForEmptyUri(R.mipmap.imagedefault_small) // URI为空时显示的图片资源ID
.showImageOnFail(R.mipmap.imagedefault_small) // 图片加载失败时显示的图片资源ID
.resetViewBeforeLoading(true)
.cacheInMemory(true) // default , 是否缓存在内存中, 默认不缓存
.cacheOnDisk(true) // default , 是否缓存在硬盘 , 默认不缓存
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default , 指定图片缩放的方式,ListView/GridView/Gallery推荐使用此默认值
.bitmapConfig(Bitmap.Config.RGB_565) // default , 指定图片的质量,默认是 ARGB_8888
.displayer(new RoundedBitmapDisplayer(45))//设置圆角
.build();
}
}
4、使用这个工具类
DownLoadTool downLoadTool = DownLoadTool.getInstance(context);
//url图片的地址,iv要显示的ImageView
downLoadTool.imageLoader.displayImage(url, iv, downLoadTool.options);
downLoadTool.imageLoader.displayImage(url, iv, downLoadTool.options1);//圆形图片