ImageLoader的工做原理:在显示图片的时,它会先在内存中查找,若是没有就去本地查找,若是尚未,就开一个新的线程去下载这张图片,下载成功会把图片同时缓存在内存和本地。html
从三者的协做关系上看,他们有点像厨房规定、厨师、客户我的口味之间的关系。ImageLoaderConfiguration就像是厨房里面的规定,每个厨师要怎么着装,要怎么保持厨房的干净,这是针对每个厨师都适用的规定,并且不容许个性化改变。ImageLoader就像是具体作菜的厨师,负责具体菜谱的制做。DisplayImageOptions就像每一个客户的偏好,根据客户是重口味仍是清淡,每个imageLoader根据DisplayImageOptions的要求具体执行。android
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using.// See the sample project how to use ImageLoader correctly.File cacheDir =StorageUtils.getCacheDirectory(context); ImageLoaderConfiguration config =newImageLoaderConfiguration.Builder(context) .memoryCacheExtraOptions(480, 800) // default = device screen dimensions .diskCacheExtraOptions(480, 800, null) .taskExecutor(...) .taskExecutorForCachedImages(...) .threadPoolSize(3) // default .threadPriority(Thread.NORM_PRIORITY-2) // default .tasksProcessingOrder(QueueProcessingType.FIFO) // default .denyCacheImageMultipleSizesInMemory() .memoryCache(newLruMemoryCache(2*1024*1024)) .memoryCacheSize(2*1024*1024) .memoryCacheSizePercentage(13) // default .diskCache(newUnlimitedDiskCache(cacheDir)) // default .diskCacheSize(50*1024*1024) .diskCacheFileCount(100) .diskCacheFileNameGenerator(newHashCodeFileNameGenerator()) // default .imageDownloader(newBaseImageDownloader(context)) // default .imageDecoder(newBaseImageDecoder()) // default .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default .writeDebugLogs() .build();
每个ImageLoader.displayImage(...)均可以使用DisplayImageOptions,具体配置代码以下:git
// DON'T COPY THIS CODE TO YOUR PROJECT! This is just example of ALL options using. // See the sample project how to use ImageLoader correctly. DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.drawable.ic_stub) // resource or drawable .showImageForEmptyUri(R.drawable.ic_empty) // resource or drawable .showImageOnFail(R.drawable.ic_error) // resource or drawable .resetViewBeforeLoading(false) // default .delayBeforeLoading(1000) .cacheInMemory(false) // default .cacheOnDisk(false) // default .preProcessor(...) .postProcessor(...) .extraForDownloader(...) .considerExifParams(false) // default .imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2) // default .bitmapConfig(Bitmap.Config.ARGB_8888) // default .decodingOptions(...) .displayer(new SimpleBitmapDisplayer()) // default .handler(new Handler()) // default .build();
ImageLoader imageLoader = ImageLoader.getInstance(); // Get singleton instance // Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view // which implements ImageAware interface) imageLoader.displayImage(imageUri, imageView);
// Load image, decode it to Bitmap and return Bitmap to callback imageLoader.loadImage(imageUri, new SimpleImageLoadingListener() { @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { // Do whatever you want with Bitmap } });
protected void onDestroy() { //回收该页面缓存在内存的图片 imageLoader.clearMemoryCache(); super.onDestroy(); }
Fresco 是一个强大的图片加载组件。它是Facebook开源的图片加载库github
Fresco 中设计有一个叫作 image pipeline 的模块。它负责从网络,从本地文件系统,本地资源加载图片。为了最大限度节省空间和CPU时间,它含有3级缓存设计(2级内存,1级文件)。缓存
Fresco 中设计有一个叫作 Drawees 模块,方便地显示loading图,当图片再也不显示在屏幕上时,及时地释放内存和空间占用。性能优化
Fresco 支持 Android2.3(API level 9) 及其以上系统。服务器
为了下载网络图片,请确保在 AndroidManifest.xml
中有如下权限:网络
<uses-permission android:name="android.permission.INTERNET"/>
在 Application 初始化时,在应用调用 setContentView()
以前,进行初始化:多线程
Fresco.initialize(context);
在xml布局文件中, 加入命名空间:框架
<!-- 其余元素 --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:fresco="http://schemas.android.com/apk/res-auto"> 加入SimpleDraweeView: <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_image_view" android:layout_width="20dp" android:layout_height="20dp" fresco:placeholderImage="@drawable/my_drawable" />
开始加载图片
Uri uri = Uri.parse("https://raw.githubusercontent.com/facebook/fresco/gh-pages/static/fresco-logo.png"); SimpleDraweeView draweeView = (SimpleDraweeView) findViewById(R.id.my_image_view); draweeView.setImageURI(uri);
本篇主要介绍了第三方图片加载库的原理和使用方法,特别是ImageLoader的介绍,由于图片显示在APP中是很常见的应用场景,何况ImageLoader已经封装好了一些类和方法。咱们能够直接拿来用了。而不用重复去写了。若是本身去写一个的话,这方面的程序仍是比较麻烦的,要考虑多线程缓存,内存溢出等不少方面,再说这么多程序都在用,说明稳定性仍是可靠的,相似这种工具类能用稳定成熟的,咱们做为一名开发人员,专一度仍是在应用业务开发上。