1. 程式人生 > >Android Glide Google 推薦載入圖片框架(載入圖片詳解篇)

Android Glide Google 推薦載入圖片框架(載入圖片詳解篇)

每個時間,都會有不同的心情,學會享受現在的心情,無論快樂亦或悲傷的心情,都是生活的點滴印記。

   看到這個,你一定會說,我現在在用ImageLoader,Picasso,Fresco或其它框架中的圖片載入,都挺好用的,為什麼要選用Glide呢?
   答:因為是Google推薦的。(不要打我,我說的是實話)

看這篇譯文圖片載入框架介紹
和Picasso有90%相似度,但卻比Picasso省記憶體。如果你注重app的優化,注重記憶體的管理,可以轉換使用Glide來載入圖片。

1.從本地資源中載入:

int resourceId = R.mipmap.ic_launcher;
Glide .with(context) .load(resourceId) .into(imageViewResource);

2.從檔案中載入:

//這個檔案可能不存在於你的裝置中。然而你可以用任何檔案路徑,去指定一個圖片路徑。

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Running.jpg");
Glide
    .with(context)
    .load(file)
    .into(imageViewFile);

3.從Uri中載入:

//這可能是任何 Uri。為了演示的目的我們只是用一個 launcher icon 去建立了一個 Uri

Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);
Glide
    .with(context)
    .load(uri)
    .into(imageViewUri);

4.listView GridView 載入不會出現圖片錯位的情況。並自帶三級快取。

5.佔位圖的展示:placeholder()

Glide
    .with(context)
    .load
(R.mipmap.ic_launcher) .placeholder(R.mipmap.ic_launcher) // 設定在載入圖片過程圖顯示的佔位圖 .into(imageViewPlaceholder);

6.錯誤佔位符: .error()

Glide
    .with(context)
    .load("http://futurestud.io/non_existing_image.png")
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // 圖片載入錯誤時候的載入
    .into(imageViewError);

7.載入圖片時候淡入淡出動畫 :crossFade();(這個效果在3.6.1版是預設啟用的)

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    .crossFade()//增加圖片顯示時候的淡入淡出動畫
    .into(imageViewFade);

8.直接載入不要淡入淡出效果:dontAnimate();

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    .dontAnimate()//取消淡入淡出效果
    .into(imageViewFade);

9.resize(x,y) 調整圖片大小 override(horizontalSize,verticalSize)

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
    .into(imageViewResize);
(設定圖片大小,可能導致圖片失真變形)

10.CenterCrop 和FitCenter,看到這倆屬性,你一定想到了ImageView 也有同樣的屬性,就是為了按照需要縮放圖片。

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200) // resizes the image to these dimensions (in pixel)
    .centerCrop() //從中間開始擴散顯示
    .into(imageViewResizeCenterCrop);
Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200)
    .fitCenter() //裁剪技術,縮放影象讓影象都測量出來等於或小於 ImageView 的邊界範圍,該影象將會完全顯示,但可能不會填滿整個 ImageView。
    .into(imageViewResizeFitCenter);

11.顯示Gif 圖片 強制Glide 變成一個Gif載入 asGif()方法

Glide  
    .with( context )
    .load( gifUrl )
    .asGif()//強制顯示Gif
    .error( R.drawable.full_cake )
    .into( imageViewGif );

12.顯示本地視訊

String filePath = "/storage/emulated/0/Pictures/example_video.mp4";
Glide  
    .with( context )
    .load( Uri.fromFile( new File( filePath ) ) )
    .into( imageViewGifAsBitmap );

13.圖片載入快取(記憶體快取) .skipMemoryCache(true).

該方法將告訴glide跳過記憶體快取,就是不將圖片快取到記憶體快取中。

Glide  
    .with( context )
    .load( eatFoodyImages[0] )
    .skipMemoryCache( true )
    .into( imageViewInternet );

14.圖片載入快取(磁碟快取)

.diskCacheStrategy(),引數需要一個列舉型別的,而不是一個布林型別。
.diskCacheStrategy(DiskCacheStrategy.NONE);

Glide  
    .with( context )
    .load( eatFoodyImages[0] )
    .diskCacheStrategy( DiskCacheStrategy.NONE )
    .into( imageViewInternet );

15.自定義磁碟快取(磁碟快取)

Glide載入圖片和Picasso載入圖片有百分之90%的相似度,但是Glide佔用記憶體不足Picasso的一半(據說圖片會稍微不清晰,但是肉眼看不出來(我是沒看出來))
在快取方面,Picasso快取的是整張高清圖片,而Glide快取原始圖的同時,快取加載出來的圖片(比如你的圖片是1000*1000,載入到ImageView 中的圖片是500*500),這個時候,Glide會將1000*1000和500*500兩個引數都進行快取。

這個時候的就要看.diskCacheStrategy() 方法中幾個列舉引數了(看12)
DiskCacheStrategy.NONE 什麼都不快取,就像剛討論的那樣
DiskCacheStrategy.SOURCE 僅僅只快取原來的全解析度的影象。在我們上面的例子中,將會只有一個 1000x1000 畫素的圖片
DiskCacheStrategy.RESULT 僅僅快取最終的影象,即,降低解析度後的(或者是轉換後的)
DiskCacheStrategy.ALL 快取所有版本的影象(預設行為)
一目瞭然,磁碟快取就搞定了。可以快取原圖,可以快取處理過的圖。吊炸天
(備註:3.6.1以及最新的3.7.0 .diskCacheStrategy()預設都是DiskCacheStrategy.RESULT)

16.自定義圖片載入優先順序 .priority()方法。

介紹:當你要載入圖片的時候,希望某些圖片先載入,某些圖片之後載入,這個時候呼叫.priority()方法,其引數為列舉型別。
所包含的引數遞增的方式(優先順序越來越高)排列:

Priority.LOW
Priority.NORMAL
Priority.HIGH
Priority.IMMEDIATE

設定了這個引數,系統會盡可能的按照你的意願去載入圖片(就是可能因為某些規則,不按你設定的規則走)

17.設定縮圖 .thumbnail() 傳入float引數作為其大小的倍數。

Glide  
    .with( context )
    .load( UsageExampleGifAndVideos.gifUrl )
    .thumbnail( 0.1f ) //載入縮圖  為原圖的十分之一
    .into( imageView2 );

備註:使用縮圖的話,就要考慮ImageView 確保ScaleType 的屬性問題。要不然加載出來的圖片會很小。

上面的可能是要載入一個網路圖片,即使是縮圖,也要先從網路上拿到圖片,才能變成縮圖進行展示。所以我們也可以載入本地的一張圖片的縮圖。

private void loadImageThumbnailRequest() {  
    // setup Glide request without the into() method
    DrawableRequestBuilder<String> thumbnailRequest = Glide
        .with( context )
        .load( R.drawable.pic);
    // pass the request as a a parameter to the thumbnail request
    Glide
        .with( context )
        .load( UsageExampleGifAndVideos.gifUrl )
        .thumbnail( thumbnailRequest )
        .into( imageView3 );
}

載入本地的一張圖片作為縮圖進行載入。

隨後將收集Glide框架 除載入圖片其他功能實現,如果你喜歡我的部落格,關注一下,共同學習咯~麼麼噠