国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android中Glide庫的使用小技巧總結

2019-10-22 18:23:57
字體:
來源:轉載
供稿:網友

簡介

在泰國舉行的谷歌開發者論壇上,谷歌為我們介紹了一個名叫 Glide 的圖片加載庫,作者是bumptech。這個庫被廣泛的運用在google的開源項目中,包括2014年google I/O大會上發布的官方app。

https://github.com/bumptech/glide

簡單android/280380.html">使用 

dependencies {  compile 'com.github.bumptech.glide:glide:3.7.0'} 

如何查看最新版本

http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22glide%22 

詳細的Glide庫配置、使用方法及簡介看這里:http://m.survivalescaperooms.com/article/83156.htm

引言

所以大家都知道,在Android項目中,圖片加載是必備的功課。經歷過多個第三方圖片加載庫后,用到了Glide。感覺挺好用,記錄下使用中總結的小技巧。

  • 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使用

在需要加載圖片的地方,直接調用方法。在with()方法中,參數可以是activity,fragment以及context,以activity和fragment作為參數的好處在于,可以根據activity和fragment的生命周期來加載圖片。

基礎使用:

Glide.with(activity).load(url).into(view);

需要注意:

不要在非主線程里面使用Glide加載圖片。如果非要使用Glide在非主線程中加載圖片,那么請將context改成getApplicationContext

Glide擴展屬性介紹

1、override(int width, int height)

使用此方法,自定義圖片大小

2、fitCenter()/centerCrop()/fitStart()/fitEnd()

設置imageview的setScaleType,控制Glide在加載圖片的時候,能根據imageview的尺寸或者overide()的尺寸加載圖片。減少加載圖片OOM出現的可能性。

3、圖片緩存

Glide的圖片緩存策略是根據imageview尺寸進行相應處理,緩存與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) 緩存根據URL加載到imageview后,與imageview相同尺寸的圖片
  • DiskCacheStrategy.ALL caches all versions of the image (default behavior) 默認的緩存方式,會將URL得到的圖片各個尺寸都緩存一遍。

很明顯可知,在使用過程中,一般會考慮DiskCacheStrategy.ALLDiskCacheStrategy.RESULT。其中使用ALL,會占用較多的內存,但是同一張圖片,在不同地方顯示不同尺寸,是一次網絡請求而來;而使用RESULT,則會相對少的占用內存,但是一張圖片在不同地方顯示不同尺寸,會根據尺寸不同多次請求網絡。

4、占位圖,錯誤圖展示

placeholder() ,默認占位圖

error() ,默認加載錯誤顯示的圖片

5、使用Glide加載自定義imageview中圖片

使用Glide加載自定義view的時候,可能會出現如下情況:

Glide填寫了占位圖,查看自定義View,自定義View第一次不會顯示URL加載的圖片,而是顯示占位圖。需要取消再次查看自定義View,才會顯示正確。

出現原因:Glide加載自定義View的時候,需要使用Glide庫中的Transformations方法轉換自定義imageview或者在into()方法中使用 new simpleTarget()方法來處理圖片。

解決方法:

a、使用Transformations方法轉換

public class BlurTransformation extends BitmapTransformation {private RenderScript rs;public BlurTransformation(Context context) { super( context ); rs = RenderScript.create( context );}@Overrideprotected 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;}@Overridepublic 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,但是會相對耗內存,而且這兩種格式在手機端看起來,效果相差并不大。

如何修改Bitmap格式:

public class GlideConfiguration implements GlideModule {@Overridepublic void applyOptions(Context context, GlideBuilder builder) { // Apply options to the builder here. builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);}@Overridepublic void registerComponents(Context context, Glide glide) { // register ModelLoaders here.}} 

同時在Androidminifest.xml中,將GlideModul定義為meta-data

Glide設置圖片Tag

在使用過程中,想要給imageview設置tag,然后使用Glide加載,但是總會報錯~如何為ImageView設置Tag呢?

方案一:使用setTag(int,object)方法設置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之后,新添加了全局設置的方法。具體方法如下:

先實現GlideMoudle接口,全局設置ViewTaget的tagId:

public class MyGlideMoudle implements GlideModule{ @Override public void applyOptions(Context context, GlideBuilder builder) { ViewTarget.setTagId(R.id.glide_tag_id); }@Overridepublic void registerComponents(Context context, Glide glide) {}} 

同樣,也需要在ids.xml下添加id

最后在AndroidManifest.xml文件里面添加

一些實用技巧

1.Glide.with(context).resumeRequests()Glide.with(context).pauseRequests()

當列表在滑動的時候,調用pauseRequests()取消請求,滑動停止時,調用resumeRequests()恢復請求。這樣是不是會好些呢?

2.Glide.clear()

當你想清除掉所有的圖片加載請求時,這個方法可以幫助到你。

3.ListPreloader

如果你想讓列表預加載的話,不妨試一下ListPreloader這個類。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。

參考鏈接

  • http://www.wtoutiao.com/p/y3eaF0.html
  • http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html

注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 建湖县| 婺源县| 滨州市| 清远市| 乌什县| 河东区| 土默特左旗| 长治县| 堆龙德庆县| 湟中县| 泽州县| 庆云县| 临泉县| 新津县| 波密县| 古丈县| 韩城市| 师宗县| 密山市| 诸城市| 集安市| 霍城县| 石泉县| 甘孜| 江都市| 新晃| 墨江| 中宁县| 日土县| 兰考县| 新巴尔虎左旗| 永新县| 洞头县| 桃源县| 高平市| 如东县| 洪雅县| 太仓市| 罗城| 嘉鱼县| 福海县|