2025-03-25

android高級之旅 (三 )picasso、glide、imageloader等幾個常用的圖片加載框架,現在網絡上的圖片加載庫已經多如牛毛瞭,所以選擇一個適合自己的圖片加載庫是非常有助於項目開發的。

現在幾個被用的比較多的庫有:UIL,Picasso, Glide, ImageLoader, Volley 和 Fresco。

如下簡介:

Universal Image Loader:一個強大的圖片加載庫,包含各種各樣的配置,最老牌,使用也最廣泛。隻可惜作者已經停止該項目的維護瞭,所以不太推薦使用。

Picasso: Square出品,和OkHttp搭配使用效果簡直杠杠滴。主要特點就是使用簡單,擴展性強,支持各種來源的圖片,包括網絡、Resources、assets、files、content providers等。內部集成瞭OkHttp的網絡框架,所以如果你的項目中使用瞭Square公司的其他框架,那麼推薦使用Picasso,兼容性會好一些。

Volley :Google官方出品

Fresco:Facebook出的,就我看來,除非你的項目有非常多圖片展示的模塊,慎用,但用好瞭,還是非常不錯的。

Glide:Google推薦的圖片加載庫,專註於流暢的滾動。代碼風格與Picasso非常相似,增加瞭更多的功能,非常重要的就是支持gif,當然它的包會大一些。如果你的項目對圖片的使用場景非常多,並且需要支持gif,則推薦使用。

今天我們就說 Picasso ImageLoader Glide 三個庫。

一 、 picasso

特點

1 提供內存和磁盤緩存,默認開啟,可以設置不進行緩存
2 圖片加載過程中默認顯示的圖片
3 圖片加載失敗或出錯後顯示的圖片
4 圖片加載成功或失敗的回調
5 自定義圖片大小、自動測量ImageView大小、裁剪、旋轉圖片等
6 對圖片進行轉換
7 標簽管理,暫停和恢復圖片加載
8 請求優先級管理
9 可以從不同來源加載圖片,網絡、Resources、assets、files、content providers
10 更加豐富的擴展功能

添加:

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

picasso基本的使用及API相當簡單,說隻需要一行代碼就能滿足你也不為過Picasso.with(this).load(imageUrl).into(imageView);

常用API也不多 如下:

 Picasso.with(this)
                .load(url)
                .centerCrop()
                .error()
                .resize()
                .into(iv);
一般項目中的圖片異步加載需求,picasso完全可以滿足需求。

二、Imageloader

算瞭,相對於其他幾個庫來說,imageloader已經可以淘汰瞭,我就不再多說。

三、 Glide

Glide 使用與picasso大同小異,使用這些庫基本上是一法通萬法通,api也很好理解。但欲鉆研其原理底層之類的還是需要仔細研究。

網絡加載圖片

Glide.with(context).load(internetUrl).into(targetImageView);

從文件加載

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

從資源id

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

從uri

Glide.with(context).load(uri).into(imageViewUri);

加載gif

String gifUrl = "xxxxx";
Glide.with( context ).load( gifUrl ).into( imageViewGif );

一些常規方法(待加載時顯示,加載出錯時顯示)

Glide.with( context ).load( gifUrl ).placeholder( R.drawable.cupcake ).error( R.drawable.full_cake ).into( imageViewGif );

強制轉化為gif

Glide.with( context ).load( gifUrl ).asGif().error( R.drawable.full_cake ).into( imageViewGif );

用bitMap播放Gif

Glide.with( context ).load( gifUrl ).asBitmap().into( imageViewGifAsBitmap );

播放本地mp4,隻能是本地

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

實現圓形圖片

Glide.with(this).load(ur2).asBitmap().centerCrop().into(new BitmapImageViewTarget(iv){
            @Override
            protected void setResource(Bitmap resource) {
                super.setResource(resource);
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(getApplicationContext().getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                iv.setImageDrawable(circularBitmapDrawable);
            }
        });

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *