Glide用法與技巧以及優(yōu)秀庫(kù)的推薦
當(dāng)前較為知名的幾個(gè)圖片加載庫(kù)是Universal-ImageLoader
、Glide
、Fresco
、Picasso
比較如下:
Universal-ImageLoader庫(kù)2015年年底作者已經(jīng)停止維護(hù)Gilde是Picasso的優(yōu)化版最后就是Facebook的Fresco,聽說(shuō)極為強(qiáng)大和高效率,但是大小有4M。最后Glide,google維護(hù)。Picasson的優(yōu)化版,使用簡(jiǎn)單,也許沒有Fresco那么強(qiáng)大,但是覺得完全可以hold住大部分項(xiàng)目。
比較 Picasso 與 Glide
- 1.兩者使用方式類似,但Glide的with()接受的不僅僅是Context,還可以是Activity或是Fragment,Context會(huì)自動(dòng)的從他們獲取。同時(shí)將Activity/Fragment作為with()參數(shù)的好處是:圖片加載會(huì)和Activity/Fragment的生命周期保持一致,比如Paused狀態(tài)在暫停加載,在Resumed的時(shí)候又自動(dòng)重新加載。所以我建議傳參的時(shí)候傳遞Activity 和 Fragment給Glide,而不是Context。
- 2.Glide加載的圖片質(zhì)量要略差于Picasso,這又是為什么呢?這是因?yàn)镚lide默認(rèn)的Bitmap格式是RGB_565,比ARGB_8888格式的內(nèi)存開銷要小一半。Glide當(dāng)然也可以通過(guò)GlideModule設(shè)置格式。
- 3.兩者在磁盤緩存策略上有很大的不同。Picasso緩存的是全尺寸的,而Glide緩存的是跟ImageView尺寸相同的。Glide的這種方式優(yōu)點(diǎn)是加載顯示非???。而Picasso的方式則因?yàn)樾枰陲@示之前重新調(diào)整大小而導(dǎo)致一些延遲。
- 4.Glide可以加載GIF動(dòng)態(tài)圖,而Picasso不能。
- 5.Picasso (v2.5.1)大小約為118KB,然而Glide (v3.5.2)的大小約為430KB。Picasso的方法數(shù)大約480,然而Glide的方法數(shù)約2678。
導(dǎo)入
Picasso和Glide都在jcenter上。在項(xiàng)目中添加依賴非常簡(jiǎn)單:
Picasso
dependencies { compile 'com.squareup.picasso:picasso:2.5.1' }
Glide
dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support:support-v4:24.0.0' }
Glide的with方法不光接受Context,還接受Activity 和 Fragment,Context會(huì)自動(dòng)的從他們獲取。
1.網(wǎng)絡(luò)加載圖片到ImageView中
Glide.with(context).load(imageUrl).into(imageView);
2.當(dāng)加載網(wǎng)絡(luò)圖片時(shí),由于加載過(guò)程中圖片未能及時(shí)顯示,此時(shí)可能需要設(shè)置等待時(shí)的圖片,通過(guò)placeHolder()方法
Glide.with(context).load(imageUrl).placeholder(R.mipmap.ic_launcher).into(imageView);
3.當(dāng)加載圖片失敗時(shí),通過(guò)error(Drawable drawable)方法設(shè)置加載失敗后的圖片顯示:
Glide.with(context).load(imageUrl).error(R.mipmap.ic_launcher).into(imageView);
4.圖片的縮放,centerCrop()和fitCenter():
1)使用centerCrop是利用圖片圖填充ImageView設(shè)置的大小,如果ImageView的Height是match_parent則圖片就會(huì)被拉伸填充
Glide.with(context).load(imageUrl).centerCrop().into(imageView);
2)使用fitCenter即縮放圖像讓圖像都測(cè)量出來(lái)等于或小于 ImageView 的邊界范圍,該圖像將會(huì)完全顯示,但可能不會(huì)填滿整個(gè)ImageView。
Glide.with(context).load(imageUrl).fitCenter().into(imageView);
5.顯示gif動(dòng)畫,asGif()判斷是否是gif動(dòng)畫
Glide.with(context).load(imageUrl).asGif().into(imageView);
6.顯示本地視頻
String filePath = "/storage/emulated/0/Pictures/video.mp4"; Glide.with( context ).load( Uri.fromFile( new File( filePath ) ) ).into(imageViewGifAsBitmap );
7.緩存策略
Glide.with( context ).load(imageUrl).skipMemoryCache(true).into(imageViewInternet );//跳過(guò)內(nèi)存緩存
Glide.with( context ).load(imageUrl).diskCacheStrategy(DiskCacheStrategy.NONE).into( imageViewInternet );//跳過(guò)硬盤緩存
DiskCacheStrategy.NONE
什么都不緩存DiskCacheStrategy.SOURCE
僅僅只緩存原來(lái)的全分辨率的圖像DiskCacheStrategy.RESULT
僅僅緩存最終的圖像,即降低分辨率后的(或者是轉(zhuǎn)換后的)DiskCacheStrategy.ALL
緩存所有版本的圖像(默認(rèn)行為)
8.優(yōu)先級(jí),設(shè)置圖片加載的順序:
Glide.with(context).load(imageUrl).priority( Priority.HIGH).into( imageView);
9.獲取Bitmap,設(shè)置CircleImageVIew可以使用這個(gè)ImageView庫(kù)
Glide.with(mContext) .load(url) .placeholder(R.drawable.loading_spinner) .into(new SimpleTarget<Bitmap>(width, height) { @Override public void onResourceReady(Bitmap bitmap, GlideAnimation anim) { // setImageBitmap(bitmap) on CircleImageView } };
10.加載圓形圖片和圓角圖片
//圓形圖片 public class GlideCircleTransform extends BitmapTransformation { public GlideCircleTransform(Context context) { super(context); } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return circleCrop(pool, toTransform); } private static Bitmap circleCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; int size = Math.min(source.getWidth(), source.getHeight()); int x = (source.getWidth() - size) / 2; int y = (source.getHeight() - size) / 2; // TODO this could be acquired from the pool too Bitmap squared = Bitmap.createBitmap(source, x, y, size, size); Bitmap result = pool.get(size, size, Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(squared, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); float r = size / 2f; canvas.drawCircle(r, r, r, paint); return result; } @Override public String getId() { return getClass().getName(); } }
//圓角圖片 public class GlideRoundTransform extends BitmapTransformation { private static float radius = 0f; public GlideRoundTransform(Context context) { this(context, 4); } public GlideRoundTransform(Context context, int dp) { super(context); this.radius = Resources.getSystem().getDisplayMetrics().density * dp; } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { return roundCrop(pool, toTransform); } private static Bitmap roundCrop(BitmapPool pool, Bitmap source) { if (source == null) return null; Bitmap result = pool.get(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); if (result == null) { result = Bitmap.createBitmap(source.getWidth(), source.getHeight(), Bitmap.Config.ARGB_8888); } Canvas canvas = new Canvas(result); Paint paint = new Paint(); paint.setShader(new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP)); paint.setAntiAlias(true); RectF rectF = new RectF(0f, 0f, source.getWidth(), source.getHeight()); canvas.drawRoundRect(rectF, radius, radius, paint); return result; } @Override public String getId() { return getClass().getName() + Math.round(radius); } }
Glide.with(this).load("https://www.baidu.com/img/bdlogo.png").transform(new GlideRoundTransform(context)).into(imageView); Glide.with(this).load("https://www.baidu.com/img/bdlogo.png").transform(new GlideRoundTransform(context,10)).into(imageView); Glide.with(this).load("https://www.baidu.com/img/bdlogo.png").transform(new GlideCircleTransform(context)).into(imageView);
一些使用技巧
1.Glide.with(context).resumeRequests()
和Glide.with(context).pauseRequests()
當(dāng)列表在滑動(dòng)的時(shí)候,調(diào)用pauseRequests()取消請(qǐng)求,滑動(dòng)停止時(shí),調(diào)用resumeRequests()恢復(fù)請(qǐng)求。這樣是不是會(huì)好些呢?
2.Glide.clear()
當(dāng)你想清除掉所有的圖片加載請(qǐng)求時(shí),這個(gè)方法可以幫助到你。
3.ListPreloader
如果你想讓列表預(yù)加載的話,不妨試一下ListPreloader這個(gè)類。
一些基于Glide的優(yōu)秀庫(kù)
1.glide-transformations
一個(gè)基于Glide的transformation庫(kù),擁有裁剪,著色,模糊,濾鏡等多種轉(zhuǎn)換效果,贊的不行不行的~~
2.GlidePalette
一個(gè)可以在Glide加載時(shí)很方便使用Palette的庫(kù)。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- 導(dǎo)入takephoto庫(kù)編譯失敗與glide庫(kù)沖突應(yīng)排除依賴
- Glide4 高效加載圖片的配置詳解
- Glide4.6.1 GlideApp無(wú)法生成的問(wèn)題的解決
- Android中Glide獲取圖片Path、Bitmap用法詳解
- Android將Glide動(dòng)態(tài)加載不同大小的圖片切圓角與圓形的方法
- Android添加glide庫(kù)報(bào)錯(cuò)Error: Failed to resolve: com.android.support:support-annotations:26.0.2的解決
- android中Glide實(shí)現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
- 詳解Android中Glide與CircleImageView加載圓形圖片的問(wèn)題
- Android基于Glide v4.x的圖片加載進(jìn)度監(jiān)聽
- Android利用Glide獲取圖片真正的寬高的實(shí)例
相關(guān)文章
Flutter中使用setState時(shí)的6個(gè)簡(jiǎn)單技巧總結(jié)
平常在使用flutter的控件時(shí)我們都知道,要刷新頁(yè)面那么只需要調(diào)用setState()方法即可,這篇文章主要給大家介紹了關(guān)于Flutter中使用setState時(shí)的6個(gè)簡(jiǎn)單技巧,需要的朋友可以參考下2022-05-05Android編程中出現(xiàn)The connection to adb is down問(wèn)題的解決方法
這篇文章主要介紹了Android編程中出現(xiàn)The connection to adb is down問(wèn)題的解決方法,涉及Android進(jìn)程與服務(wù)的相關(guān)操作技巧,需要的朋友可以參考下2015-12-12Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Kotlin標(biāo)準(zhǔn)庫(kù)函數(shù)使用分析及介紹
Kotlin提供了一個(gè)系統(tǒng)庫(kù),是Java庫(kù)的增強(qiáng)。其中有很多函數(shù)在適配了Java的類型和方法同時(shí)使用Kotlin的語(yǔ)法。其中一些底層的函數(shù) 是使用比較廣泛的2022-09-09Android ndk獲取手機(jī)內(nèi)部存儲(chǔ)卡的根目錄方法
今天小編就為大家分享一篇Android ndk獲取手機(jī)內(nèi)部存儲(chǔ)卡的根目錄方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Android中@id和@+id及@android:id的區(qū)別介紹
這篇文章主要給大家介紹了關(guān)于Android中@id和@+id及@android:id的區(qū)別的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Android自定義相機(jī)、預(yù)覽區(qū)域裁剪
這篇文章主要為大家詳細(xì)介紹了Android自定義相機(jī)、預(yù)覽區(qū)域裁剪,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05Android開發(fā)使用UncaughtExceptionHandler捕獲全局異常
本文主要介紹在Android開發(fā)中使用UncaughtExceptionHandler捕獲全局異常,需要的朋友可以參考下。2016-06-06