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已經封裝好了一些類和方法。咱們能夠直接拿來用了。而不用重複去寫了。若是本身去寫一個的話,這方面的程序仍是比較麻煩的,要考慮多線程緩存,內存溢出等不少方面,再說這麼多程序都在用,說明穩定性仍是可靠的,相似這種工具類能用穩定成熟的,咱們做爲一名開發人員,專一度仍是在應用業務開發上。