Glide圖片加載框架學習

學習資源來自博客園,請點擊

Glide(圖片加載框架)

Glide默認加載圖片的清晰度,即bitmap的格式爲RGB_565

添加依賴

compile 'com.github.bumptech.glide:glide:3.5.2'//由於glide需要依賴V4包
compile 'com.android.support:support-v4:24.2.1'

最簡單加載並設置在imageView上

Glide.with(this)
.load("http://imgstore.cdn.sogou.com/app/a/100540002/527471.jpg")
.into(((ImageView)findViewById(R.id.image)));

針對對清晰度有更高的要求的

可以通過自定義GlideModel

實現gilde的GlideModule接口

package com.tc.glideimageloader.glide;
import android.content.Context;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.module.GlideModule;
    /**
    * Created by 輝神 on 2016/10/5.
    */
    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.
    }
}

AndroidManifest.xml

<meta-data 
android:name="com.tc.glideimageloader.glide.GlideConfiguration"
android:value="GlideModule"/>

緩存策略

glide緩存所有尺寸的圖片

glide圖片加載原理:只加載固定尺寸,並在磁盤中緩存好。如:原爲200*200,如果imageView爲100*100,則加載時,glide會加載100*100的尺寸大小 到磁盤中,下次自動判斷內存否已經加載了這個圖片

但是如果,第一個頁面的imageview只有100*100,當點擊到第二個面的時候imageview是200*200了,第二頁面又要重新加載一次

可以使用下面的方法,緩存全尺寸的圖片,又緩存其他尺寸的圖片:

Glide.with(this)
.load("http://imgstore.cdn.sogou.com/app/a/100540002/527471.jpg")
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(((ImageView)findViewById(R.id.image)));

DiskCaceStrategy的四個常量解說

all:緩存源資源和轉換後的資源---緩存原尺寸圖片和其他小尺寸的圖片

none:不作任何磁盤緩存

source:緩存源資源----這個應該意思是隻緩存源資源,可能是原尺寸圖片

result:緩存轉換後的資源-----緩存固定尺寸,也有可能與原尺寸有很大差異

priority(Priority.NORMAL)下載優先級

Glide.with(this).load(imageUrl).priority(Priority.NORMAL).into(imageView);

override(200,200)按尺寸緩存圖片

centerCrop()和fitCenter()選擇放置類型,

centerCrop()centerCrop在ImageView屬性就有這個,用途就是,按比例擴大圖片的size居中顯示,使得圖片長(寬)等於或大於View的長(寬)

fitCenter()fitCenter在IMageview也有屬性,其實也一樣用途,把圖片按比例擴大/縮小到View的寬度,居中顯示

transform()設置轉換器,例:設置圖片圓角

設置需要加載的內容

項目中有很多需要先下載圖片然後再做一些合成的功能,比如項目中出現的圖文混排,該如何實現目標下

Glide.with(this).load(imageUrl).centerCrop().into(

new SimpleTarget<GlideDrawable>() {

@Override

public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {

imageView.setImageDrawable(resource);

}

}

);

設置佔位圖和加載出錯圖

Glide.with(this)
.load("http://imgstore.cdn.sogou.com/app/a/100540002/52741171.jpg")
.placeholder(R.mipmap.placeholder)
.error(R.mipmap.error)
.into(((ImageView)findViewById(R.id.image)));

加載GIF

無需設置什麼鬼,自動加載,picasso框架就不能加載gif圖片

1、只加載靜態圖

Glide.with(this).load(imageUrl).asBitmap().into(imageView);//顯示gif靜態圖片

2、只加載動態圖

Glide.with(this).load(imageUrl).asGif().into(imageView);//顯示gif動態圖片

加載縮略圖

Glide.with(this)
.load("http://imgstore.cdn.sogou.com/app/a/100540002/527471.jpg")
.placeholder(R.mipmap.placeholder)
.error(R.mipmap.error)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.thumbnail(0.1f)//這樣,gilde會加載縮略圖,再加載全圖
.into(((ImageView)findViewById(R.id.image)));

清理磁盤緩存

Glide.get(this).clearDiskCache();//清理磁盤緩存 需要在子線程中執行

Glide.get(this).clearMemory();//清理內存緩存 可以在UI主線程中進行
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章