Android中Glide庫的使用小技巧總結(jié)
簡介
在泰國舉行的谷歌開發(fā)者論壇上,谷歌為我們介紹了一個名叫 Glide 的圖片加載庫,作者是bumptech。這個庫被廣泛的運用在google的開源項目中,包括2014年google I/O大會上發(fā)布的官方app。
https://github.com/bumptech/glide
簡單使用
dependencies { compile 'com.github.bumptech.glide:glide:3.7.0' }
如何查看最新版本
http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22glide%22
詳細的Glide庫配置、使用方法及簡介看這里:http://www.dbjr.com.cn/article/83156.htm
引言
所以大家都知道,在Android項目中,圖片加載是必備的功課。經(jīng)歷過多個第三方圖片加載庫后,用到了Glide。感覺挺好用,記錄下使用中總結(jié)的小技巧。
- AS導入Glide庫
- Glide方法介紹
AS導入Glide庫
dependencies { compile ‘com.github.bumptech.glide:glide:3.5.2' compile ‘com.android.support:support-v4:22.0.0' }
Glide使用
在需要加載圖片的地方,直接調(diào)用方法。在with()
方法中,參數(shù)可以是activity,fragment以及context,以activity和fragment作為參數(shù)的好處在于,可以根據(jù)activity和fragment的生命周期來加載圖片。
基礎(chǔ)使用:
Glide.with(activity).load(url).into(view);
需要注意:
不要在非主線程里面使用Glide加載圖片。如果非要使用Glide在非主線程中加載圖片,那么請將context改成getApplicationContext
Glide擴展屬性介紹
1、override(int width, int height)
使用此方法,自定義圖片大小
2、fitCenter()/centerCrop()/fitStart()/fitEnd()
設(shè)置imageview的setScaleType,控制Glide在加載圖片的時候,能根據(jù)imageview的尺寸或者overide()
的尺寸加載圖片。減少加載圖片OOM出現(xiàn)的可能性。
3、圖片緩存
Glide的圖片緩存策略是根據(jù)imageview尺寸進行相應(yīng)處理,緩存與imageview尺寸相同的圖片。
使用方法:
.diskCacheStrategy(DiskCacheStrategy.RESULT)
查看源碼可得
- DiskCacheStrategy.NONE caches nothing, as discussed 不緩存圖片
- DiskCacheStrategy.SOURCE caches only the original full-resolution image. In our example above that would be the 1000x1000 pixel one 僅緩存原圖片
- DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution (and possibly transformations) 緩存根據(jù)URL加載到imageview后,與imageview相同尺寸的圖片
- DiskCacheStrategy.ALL caches all versions of the image (default behavior) 默認的緩存方式,會將URL得到的圖片各個尺寸都緩存一遍。
很明顯可知,在使用過程中,一般會考慮DiskCacheStrategy.ALL
與DiskCacheStrategy.RESULT
。其中使用ALL,會占用較多的內(nèi)存,但是同一張圖片,在不同地方顯示不同尺寸,是一次網(wǎng)絡(luò)請求而來;而使用RESULT,則會相對少的占用內(nèi)存,但是一張圖片在不同地方顯示不同尺寸,會根據(jù)尺寸不同多次請求網(wǎng)絡(luò)。
4、占位圖,錯誤圖展示
placeholder()
,默認占位圖
error()
,默認加載錯誤顯示的圖片
5、使用Glide加載自定義imageview中圖片
使用Glide加載自定義view的時候,可能會出現(xiàn)如下情況:
Glide填寫了占位圖,查看自定義View,自定義View第一次不會顯示URL加載的圖片,而是顯示占位圖。需要取消再次查看自定義View,才會顯示正確。
出現(xiàn)原因:Glide加載自定義View的時候,需要使用Glide庫中的Transformations方法轉(zhuǎn)換自定義imageview或者在into()方法中使用 new simpleTarget()
方法來處理圖片。
解決方法:
a、使用Transformations方法轉(zhuǎn)換
public class BlurTransformation extends BitmapTransformation { private RenderScript rs; public BlurTransformation(Context context) { super( context ); rs = RenderScript.create( context ); } @Override protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) { Bitmap blurredBitmap = toTransform.copy( Bitmap.Config.ARGB_8888, true ); // Allocate memory for Renderscript to work with Allocation input = Allocation.createFromBitmap( rs, blurredBitmap, Allocation.MipmapControl.MIPMAP_FULL, Allocation.USAGE_SHARED ); Allocation output = Allocation.createTyped(rs, input.getType()); // Load up an instance of the specific script that we want to use. ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); script.setInput(input); // Set the blur radius script.setRadius(10); // Start the ScriptIntrinisicBlur script.forEach(output); // Copy the output to the blurred bitmap output.copyTo(blurredBitmap); toTransform.recycle(); return blurredBitmap; } @Override public String getId() { return "blur"; } }
Glide .with( context ) .load( eatFoodyImages[0] ) .transform( new BlurTransformation( context ) ) //.bitmapTransform( new BlurTransformation( context ) ) // this would work too! .into( imageView1 );
b、使用new simpleTarget()
Glide.with(activity).load(url).into(new SimpleTarget() { @Override public void onResourceReady(GlideDrawable resource, GlideAnimation
如何修改Glide Bimmap格式
默認Bitmap格式:
RGB_565,也可以使用RGB_8888,但是會相對耗內(nèi)存,而且這兩種格式在手機端看起來,效果相差并不大。
如何修改Bitmap格式:
public class GlideConfiguration implements GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { // Apply options to the builder here. builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } @Override public void registerComponents(Context context, Glide glide) { // register ModelLoaders here. } }
同時在Androidminifest.xml中,將GlideModul定義為meta-data
Glide設(shè)置圖片Tag
在使用過程中,想要給imageview設(shè)置tag,然后使用Glide加載,但是總會報錯~如何為ImageView設(shè)置Tag呢?
方案一:使用setTag(int,object)方法設(shè)置tag,具體用法如下:
Glide.with(context).load(urls.get(i).getUrl()).fitCenter().into(imageViewHolder.image); imageViewHolder.image.setTag(R.id.image_tag, i); imageViewHolder.image.setOnClickListener(new View.OnClickListener() { @Override int position = (int) v.getTag(R.id.image_tag); Toast.makeText(context, urls.get(position).getWho(), Toast.LENGTH_SHORT).show(); } });
同時在values文件夾下新建ids.xml,添加
方案二:從Glide的3.6.0之后,新添加了全局設(shè)置的方法。具體方法如下:
先實現(xiàn)GlideMoudle接口,全局設(shè)置ViewTaget的tagId:
public class MyGlideMoudle implements GlideModule{ @Override public void applyOptions(Context context, GlideBuilder builder) { ViewTarget.setTagId(R.id.glide_tag_id); } @Override public void registerComponents(Context context, Glide glide) { } }
同樣,也需要在ids.xml下添加id
最后在AndroidManifest.xml文件里面添加
一些實用技巧
1.Glide.with(context).resumeRequests()
和 Glide.with(context).pauseRequests()
當列表在滑動的時候,調(diào)用pauseRequests()
取消請求,滑動停止時,調(diào)用resumeRequests()
恢復請求。這樣是不是會好些呢?
2.Glide.clear()
當你想清除掉所有的圖片加載請求時,這個方法可以幫助到你。
3.ListPreloader
如果你想讓列表預(yù)加載的話,不妨試一下ListPreloader這個類。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
參考鏈接
- http://www.wtoutiao.com/p/y3eaF0.html
- http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html
- Android項目實戰(zhàn)之Glide 高斯模糊效果的實例代碼
- Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實現(xiàn)方法
- Android框架學習之Volley和Glide詳解
- Android Glide 4.0+使用詳解
- Android中Glide加載到RelativeLayout背景圖方法示例
- Android中Glide獲取圖片Path、Bitmap用法詳解
- Android將Glide動態(tài)加載不同大小的圖片切圓角與圓形的方法
- Android添加glide庫報錯Error: Failed to resolve: com.android.support:support-annotations:26.0.2的解決
- android中Glide實現(xiàn)加載圖片保存至本地并加載回調(diào)監(jiān)聽
- 詳解Android中Glide與CircleImageView加載圓形圖片的問題
- Android利用Glide獲取圖片真正的寬高的實例
- Android如何使用Glide加載清晰長圖
相關(guān)文章
Android使用ContentProvider初始化SDK庫方案小結(jié)
這篇文章主要介紹了Android使用ContentProvider初始化SDK庫方案總結(jié),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Android動態(tài)給ViewPager添加Indicator導航
這篇文章主要為大家詳細介紹了Android動態(tài)給ViewPager添加Indicator導航的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02Android自定義View實現(xiàn)廣告信息上下滾動效果
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)廣告信息上下滾動的具體代碼,感興趣的小伙伴們可以參考一下2016-05-05