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

首頁 > 系統(tǒng) > Android > 正文

Android中Glide庫的使用小技巧

2020-02-21 17:23:42
字體:
供稿:網(wǎng)友

Glide是Google推薦的圖片加載庫,相信大家并不陌生,本文是武林技術(shù)頻道小編為大家?guī)淼?a target="_blank">Android中Glide庫的使用小技巧,對您的學(xué)習(xí)或工作有一定的參考價值。

簡單使用?

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項目中,圖片加載是必備的功課。經(jīng)歷過多個第三方圖片加載庫后,用到了Glide。感覺挺好用,記錄下使用中總結(jié)的小技巧。

  • AS導(dǎo)入Glide庫
  • Glide方法介紹

AS導(dǎo)入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.ALLDiskCacheStrategy.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 );}@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,但是會相對耗內(nèi)存,而且這兩種格式在手機端看起來,效果相差并不大。

如何修改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設(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); }@Overridepublic void registerComponents(Context context, Glide glide) {}} 

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

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

一些實用技巧

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

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

2.Glide.clear()

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

3.ListPreloader

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

以上就是關(guān)于Android中Glide庫的使用小技巧的介紹,其實學(xué)習(xí)知識要持之以恒,才能看到效果,感謝大家一起以來對js.Vevb.com的支持。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 仪征市| 徐州市| 武强县| 雅安市| 城步| 杭锦后旗| 旌德县| 科技| 太康县| 平远县| 藁城市| 苏尼特右旗| 茶陵县| 平塘县| 饶河县| 隆子县| 思南县| 荣昌县| 开平市| 乐至县| 寻甸| 黑水县| 高安市| 阿尔山市| 泰兴市| 修武县| 铜山县| 漳浦县| 武功县| 诸城市| 兴仁县| 紫云| 永新县| 大化| 房山区| 寻乌县| 沙湾县| 湖北省| 白城市| 德阳市| 刚察县|