Glide Picasso Fresco UIL 圖片框架 MD


Markdown版本筆記 我的GitHub首頁 我的博客 我的微信 我的郵箱
MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina.com

Glide Picasso Fresco UIL 圖片框架 MD


目錄

Glide Google推薦 24k

Glide 官方文檔
Glide 英文教程
Glide 中文教程

簡介

我們要曉得:Glide是Google推薦的圖片加載庫,專注於流暢的滾動

Glide 是 Google 一位員工的大作,他完全是基於 Picasso 的,沿襲了 Picasso 的簡潔風格,但是在此做了大量優化與改進。

Glide 默認的 Bitmap 格式是 RGB_565 格式,而 Picasso 默認的是 ARGB_8888 格式,相比而言,這個內存開銷要小一半。

在磁盤緩存方面,Picasso 只會緩存原始尺寸的圖片,而 Glide 緩存的是多種規格,也就意味着 Glide 會根據你 ImageView 的大小來緩存相應大小的圖片尺寸,比如你 ImageView 大小是 200*200,原圖是 400*400 ,而使用 Glide 就會緩存 200*200 規格的圖,而 Picasso 只會緩存 400*400 規格的。這個改進就會導致 Glide 比 Picasso 加載的速度要快,畢竟少了每次裁剪重新渲染的過程。

最重要的一個特性是 Glide 支持加載 Gif 動態圖,而 Picasso 不支持該特性。

除此之外,還有很多其他配置選項的增加。

總體來說,Glide 是在 Picasso 基礎之上進行的二次開發,各個方面做了不少改進,不過這也導致他的包比 Picasso 大不少,不過也就不到 500k,Picasso 是100多k,方法數也比 Picasso 多不少,不過畢竟級別還是蠻小的,影響不是很大。

GitHub 上的介紹

An image loading and caching library for Android focused on smooth scrolling

Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface.

Glide supports fetching, decoding, and displaying video stills, images, and animated GIFs. Glide includes a flexible API that allows developers to plug in to almost any network stack. By default Glide uses a custom HttpUrlConnection based stack, but also includes utility libraries plug in to Google's Volley project or Square's OkHttp library instead.

Glide's primary首要的 focus is on making scrolling any kind of a list of images as smooth and fast as possible, but Glide is also effective for almost any case where you need to fetch, resize, and display a remote image.

基本使用

添加依賴:

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

加載圖片:

Glide.with(context)
    .load("http://inthecheesefactory.com/uploads/source/glidepicasso/cover.jpg")
    .into(ivImg);

Glide的 with 方法不光接受 Context,還接受 Activity 和 Fragment。
同時將 Activity/Fragment 作為 with() 參數的好處是:圖片加載會和 Activity/Fragment 的生命周期保持一致,比如在 Paused 狀態暫停加載,在 Resumed 的時候又自動重新加載。所以我建議傳參的時候傳遞 Activity 和 Fragment 給Glide,而不是 Context。
當然,如果使用 Application 作為上下文,Glide 請求將不受 Activity/Fragment 生命周期控制。

總結

  • 優點:加載速度極快,框架體積小,四五百KB
  • 缺點:因為機制的選擇不同,速度快了,但是圖片質量低了,RGB565
  • 特點:根據ImageView大小來進行緩存,也就是說一張圖片可能根據展示情況來緩存不同尺寸的幾份

可以 load 的資源類型

Glide基本可以load任何可以拿到的媒體資源,如:

load(String string)
load SD卡資源:load("file://"+ Environment.getExternalStorageDirectory().getPath()+"/test.jpg") 
load assets資源:load("file:///android_asset/f003.gif") 
load raw資源:load("Android.resource://com.frank.glide/raw/raw_1")
          或 load("android.resource://com.frank.glide/raw/"+R.raw.raw_1) 
load drawable資源:load("android.resource://com.frank.glide/drawable/news")
               或 load("android.resource://com.frank.glide/drawable/"+R.drawable.news) 
load ContentProvider資源:load("content://media/external/images/media/139469") 
load http資源:load("http://img.my.csdn.net/uploads/201508/05/1438760757_3588.jpg") 
load https資源:load("https://img.alicdn.com/tps/TB1uyhoMpXXXXcLXVXXXXXXXXXX-476-538.jpg_240x5000q50.jpg_.webp") 

當然,load不限於String類型,還可以:

  • load(File file) file:The File containing the image。這個文件可能不存在於你的設備中,然而你可以用任何文件路徑,去指定一個圖片路徑。
  • load(Integer resourceId) resourceId:the id of the resource containing the image。可以用R.drawable或R.mipmap
  • load(Uri uri) uri:The Uri representing the image. Must be of a type handled by UriLoader
  • load(byte[] model) model:the data to load.
  • loadFromMediaStore(Uri uri) uri:The uri representing the media.
  • load(T model) model:The model the load.
  • load(URL url):deprecated

load的資源也可以是本地視頻,但如果是一個網絡 URL 的視頻,它是不工作的!

從資源 id 轉換成 Uri 的方法:

public static Uri resourceIdToUri(Context context, int resourceId) {
    return Uri.parse("android.resource://" + context.getPackageName() + "/" + resourceId);
}

比 Picasso 強大的地方

  • Glide 可以加載 GIF 動態圖,而 Picasso 不能。同時因為 Glide 和 Activity/Fragment 的生命周期是一致的,因此 gif 的動畫也會自動的隨着 Activity/Fragment 的狀態暫停、重放。Glide 的緩存在 gif 這里也是一樣,調整大小然后緩存。
  • Glide 可以將任何的本地視頻解碼成一張靜態圖片。
  • 可以配置圖片顯示的動畫,而 Picasso 只有一種動畫:fading in。
  • 可以使用 thumbnail() 產生一個你所加載圖片的 thumbnail。

其實還有一些特性,不過不是非常重要,比如將圖像轉換成字節數組等。

Picasso 16K

Picasso是Square公司開源的一個Android平台上的圖片加載框架,簡單易用,一句話搞定項目中的圖片加載,好用到令人發指。相比而言,這個也算是一個出來時間比較長的框架了。

使用示例:

Picasso.with(this)
    .load("url")
    .placeholder(R.mipmap.ic_default)
    .into(imageView);

添加依賴

compile 'com.squareup.picasso:picasso:2.5.2'

A powerful image downloading and caching library for Android

總結

  • 優點:圖片質量高
  • 缺點:加載速度一般
  • 特點:只緩存一個全尺寸的圖片,根據需求的大小在壓縮轉換

Fresco 15K

FaceBook出品,支持Android 2.3 (Gingerbread)及以上

尼瑪,他竟然有專門的 中文文檔

Fresco 是 Facebook 出品的新一代的圖片加載庫,我們知道 Android 應用程序可用的內存有限,經常會因為圖片加載導致 OOM,雖然我們有各種手段去優化,盡量減少出現 OOM 的可能性,但是永遠沒法避免,尤其某些低端手機 OOM 更是嚴重。而 Facebook 就另辟蹊徑,既然沒法在 Java 層處理,我們就在更底層的 Native 堆做手腳。於是 Fresco 將圖片放到一個特別的內存區域叫 Ashmem 區,就是屬於 Native 堆,圖片將不再占用 App 的內存,Java 層對此無能為力,這里是屬於 C++ 的地盤,所以能大大的減少 OOM。

添加依賴

compile 'com.facebook.fresco:fresco:1.2.0'

//**************下面的依賴需要根據需求添加******************//
// 在 API < 14 上的機器支持 WebP 時,需要添加
compile 'com.facebook.fresco:animated-base-support:1.2.0'

// 支持 GIF 動圖,需要添加
compile 'com.facebook.fresco:animated-gif:1.2.0'

// 支持 WebP (靜態圖+動圖),需要添加
compile 'com.facebook.fresco:animated-webp:1.2.0'
compile 'com.facebook.fresco:webpsupport:1.2.0'

// 僅支持 WebP 靜態圖,需要添加
compile 'com.facebook.fresco:webpsupport:1.2.0'

GitHub上的介紹:

Fresco is a powerful system for displaying images in Android applications.

Fresco takes care of image loading and display, so you don't have to. It will load images from the network, local storage, or local resources, and display a placeholder占位符 until the image has arrived. It has two levels of cache; one in memory and another in internal storage.

In Android 4.x and lower, Fresco puts images in a special region of Android memory. This lets your application run faster - and suffer the dreaded OutOfMemoryError much less often.

Fresco also supports:

  • streaming of progressive JPEGs
  • display of animated GIFs and WebPs
  • extensive廣闊的 customization of image loading and display
  • and much more!

Fresco 支持許多URI格式,但 Fresco 不支持 相對路徑的URI。所有的 URI 都必須是絕對路徑,並且帶上該 URI 的 scheme。如下:

類型 SCHEME 示例
遠程圖片 http:// HttpURLConnection或者參考使用其他網絡加載方案
本地文件 file:// FileInputStream
ContentProvider content:// ContentResolver
asset目錄下的資源 asset:// AssetManager
res目錄下的資源 res:// Resources.openRawResource
Uri中指定圖片數據 data:mime/type;base64 數據類型必須符合rfc2397規定(僅支持UTF-8)

總結

  • 優點:支持圖像漸進式呈現,大公司出品,后期維護有保障
  • 缺點:框架體積較大,3M左右會增大apk的大小;操作方式不是特別簡單,有一定學習成本
  • 特點:有兩級內存一級文件的緩存機制,並且有自己特別的內存區域來處理緩存,避免oom

Universal-Image-Loader 16K

jar包下載

UIL可以算是老牌最火的圖片加載庫了,使用過這個框架的項目可以說多到教你做人,我第一次把第三方開源圖片加載框架加入項目中的就是這個了,當時感覺瞬間逼格上漲,媽媽再也不用擔心出現OOM和ListView圖片錯亂了。

Powerful and flexible library for loading, caching and displaying images on Android.

尼瑪,雖然這哥們很久基本都不更新了,而且還不提供Gradle的支持,但目前依然是星星最多的一個圖片處理庫

加載策略

  • 每一個圖片的加載和顯示任務都運行在獨立的線程中,除非這個圖片緩存在【內存】中,這種情況下圖片會立即顯示。
  • 如果需要的圖片緩存在【本地】,他們會開啟一個獨立的線程隊列。
  • 如果在緩存中沒有正確的圖片,任務線程會從線程池中獲取,因此,快速顯示緩存圖片時不會有明顯的障礙。

控制OOM

  • 減少線程池中線程的個數,在ImageLoaderConfiguration中的.threadPoolSize中配置,推薦配置1-5
  • 在DisplayImageOptions選項中配置bitmapConfig為Bitmap.Config.RGB_565,因為默認是ARGB_8888, 使用RGB_565會比使用ARGB_8888少消耗2倍的內存
  • 在ImageLoaderConfiguration中配置圖片的內存緩存為memoryCache(new WeakMemoryCache()) 或者不使用內存緩存
  • 在DisplayImageOptions選項中設置.imageScaleType(ImageScaleType.IN_SAMPLE_INT)或者imageScaleType(ImageScaleType.EXACTLY)

我們在使用該框架的時候盡量的使用displayImage()方法去加載圖片,loadImage()是將圖片對象回調到ImageLoadingListener接口的onLoadingComplete()方法中,需要我們手動去設置到ImageView上面,displayImage()方法中,對ImageView對象使用的是WeakReferences,方便垃圾回收器回收ImageView對象,如果我們要加載固定大小的圖片的時候,使用loadImage()方法需要傳遞一個ImageSize對象,而displayImage()方法會根據ImageView對象的測量值,或者android:layout_widthandroid:layout_height設定的值,或者android:maxWidth 和/或 android:maxHeight設定的值來裁剪圖片。

總結

  • 優點:豐富的配置選項
  • 缺點:有不維護的趨勢,可能被當前圖片框架替代
  • 特點:三級緩存的策略

小結

本人四個庫都使用了一遍,對比后發現Fresco確實強大,加載大圖 Fresco 最強,有的圖 Glide 和 Picasso 加載不出來,換上 Fresco 妥妥的,不過 Fresco 比較龐大,推薦在主要都是圖片的 app 中使用,一般的 app 使用 Glide 或 Picasso 就夠了!

Glide 和 Picasso 都是非常完美的庫,Glide 加載圖像以及磁盤緩存的方式都要優於 Picasso,速度更快,並且 Glide 更有利於減少 OutOfMemoryError 的發生。,GIF 動畫是 Glide 的殺手鐧,不過 Picasso 的圖片質量更高。

建議使用 Glide,如有需要,可以 將Bitmap 格式換成 ARGB_8888、讓 Glide 緩存同時緩存全尺寸和改變尺寸兩種。

2018-12-20

附件列表

     


    免責聲明!

    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



     
    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM