詳解Glide4.0集成及使用注意事項(xiàng)
Glide 4.0由Google的各種團(tuán)隊(duì)內(nèi)部使用,4.0被認(rèn)為是內(nèi)部穩(wěn)定的。但外部用戶可能會(huì)發(fā)現(xiàn)內(nèi)部尚未發(fā)現(xiàn)的問(wèn)題。因此,將此作為RC發(fā)布。如果沒(méi)有發(fā)現(xiàn)穩(wěn)定性或API中的重大問(wèn)題,預(yù)計(jì)不久之后就會(huì)發(fā)布非RC版本。
一、集成
1、project gradle
repositories {
mavenLocal()
}
2、app gradle
compile 'com.android.support:support-v4:25.3.1' compile 'com.github.bumptech.glide:glide:4.0.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.0.0'
3、混淆
#glide4.0
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.AppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
# 從glide4.0開(kāi)始,GifDrawable沒(méi)有提供getDecoder()方法,
# 需要通過(guò)反射獲取gifDecoder字段值,所以需要保持GifFrameLoader和GifState類不被混淆
-keep class com.bumptech.glide.load.resource.gif.GifDrawable$GifState{*;}
-keep class com.bumptech.glide.load.resource.gif.GifFrameLoader {*;}
4、在4.0中不用像3.X需要在AndroidManifest.xml配置GlideModule,而是通過(guò)注解繼承AppGlideModule的子類來(lái)配置。
@GlideModule
public class GlideConfiguration extends AppGlideModule {
@Override
public void applyOptions(Context context, GlideBuilder builder) {
//自定義緩存目錄,磁盤(pán)緩存給150M 另外一種設(shè)置緩存方式
builder.setDiskCache(new InternalCacheDiskCacheFactory(context, "GlideImgCache", 150 * 1024 * 1024));
//配置圖片緩存格式 默認(rèn)格式為8888
builder.setDefaultRequestOptions(RequestOptions.formatOf(DecodeFormat.PREFER_ARGB_8888));
ViewTarget.setTagId(R.id.glide_tag_id);
}
/**
* 禁止解析Manifest文件
* 主要針對(duì)V3升級(jí)到v4的用戶,可以提升初始化速度,避免一些潛在錯(cuò)誤
* @return
*/
@Override
public boolean isManifestParsingEnabled() {
return false;
}
}
二、使用注意事項(xiàng)
1、使用GlideApp代替Glide,asBitmap、asGif、asDrawable、asFile都要放到load之前(glide3.7.0都是要在load之后調(diào)用)。
public static void loadImg(Context context,String url, ImageView imageView){
GlideApp.with(context)
.asBitmap()
.load(url)
.placeholder(R.drawable.placeholder) //設(shè)置資源加載過(guò)程中的占位符
.into(imageView);
}
2、占位符.placeholder(R.drawable.placeholder)不能用.9圖,占位圖片和加載的目標(biāo)圖片會(huì)同時(shí)顯示,只是目標(biāo)圖片會(huì)先顯示縮略圖,然后顯示正常。fallback和error還沒(méi)測(cè)試過(guò),有興趣的可以測(cè)試看看。
3、加載gif圖時(shí),若調(diào)用dontAnimate()移除所有動(dòng)畫(huà),gif就會(huì)加載失敗。
4、計(jì)算gif播放一次的動(dòng)畫(huà)時(shí)長(zhǎng)。
glide 3.7.0你可以這樣獲取動(dòng)畫(huà)時(shí)長(zhǎng):
public void loadGif(Context context,ImageView mIvGif,int url){
Glide.with(context)
.load(url)
.listener(new RequestListener<Integer, GlideDrawable>() {
@Override
public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
try {
int duration = 0;
GifDrawable gifDrawable = (GifDrawable) resource;
GifDecoder decoder = gifDrawable.getDecoder();
for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
duration += decoder.getDelay(i);
}
Log.e("Glide3.7.0","gif播放一次動(dòng)畫(huà)時(shí)長(zhǎng)duration:"+duration);
} catch (Throwable e) {
}
return false;
}
})
.into(new GlideDrawableImageViewTarget(mIvGif, 1));
}
glide4.0中GifDrawable沒(méi)有提供getDecoder()方法并且還去掉了decoder這個(gè)成員變量。除此之外,glide4.0還去掉了GlideDrawableImageViewTarget類,那我們?cè)撊绾蝸?lái)計(jì)算gif播放一次的時(shí)長(zhǎng)呢?只能從源碼中找答案了。
(1)尋找decoder
glide3.7.0 GifDrawable中我們可以發(fā)現(xiàn)decoder最終于會(huì)傳入GifFrameLoader類中并賦值給gifDecoder變量。
//源碼
//glide 3.7.0 GifDrawable.java
GifDrawable(GifState state) {
if (state == null) {
throw new NullPointerException("GifState must not be null");
}
this.state = state;
this.decoder = new GifDecoder(state.bitmapProvider);
this.paint = new Paint();
decoder.setData(state.gifHeader, state.data);
frameLoader = new GifFrameLoader(state.context, this, decoder, state.targetWidth, state.targetHeight);
frameLoader.setFrameTransformation(state.frameTransformation);
}
/*---------------------------------------------------------------------------------------------------*/
//glide 3.7.0 GifFrameLoader.java
private final GifDecoder gifDecoder;//私有屬性
public GifFrameLoader(Context context, FrameCallback callback, GifDecoder gifDecoder, int width, int height) {
this(callback, gifDecoder, null,
getRequestBuilder(context, gifDecoder, width, height, Glide.get(context).getBitmapPool()));
}
GifFrameLoader(FrameCallback callback, GifDecoder gifDecoder, Handler handler,
GenericRequestBuilder<GifDecoder, GifDecoder, Bitmap, Bitmap> requestBuilder) {
if (handler == null) {
handler = new Handler(Looper.getMainLooper(), new FrameLoaderCallback());
}
this.callback = callback;
//看這里
this.gifDecoder = gifDecoder;
this.handler = handler;
this.requestBuilder = requestBuilder;
}
glide4.0 GifDrawable類的構(gòu)造中我們可以看到有一個(gè)gifDecoder的參數(shù),這個(gè)參數(shù)的解釋是解碼器用于解碼GIF數(shù)據(jù)(The decoder to use to decode GIF data)。繼續(xù)看這個(gè)構(gòu)造,發(fā)現(xiàn)gifDecoder最終也是被傳到GifFrameLoader類中并賦值給gifDecoder變量。所以glide3.7.0中的decoder其實(shí)就是4.0中的gifDecoder。
//源碼
//glide 4.0 GifDrawable.java
private final GifState state;
/*
* @param gifDecoder The decoder to use to decode GIF data.
* @param firstFrame The decoded and transformed first frame of this GIF.
* @see #setFrameTransformation(com.bumptech.glide.load.Transformation, android.graphics.Bitmap)
*/
public GifDrawable(Context context, GifDecoder gifDecoder, BitmapPool bitmapPool,
Transformation<Bitmap> frameTransformation, int targetFrameWidth, int targetFrameHeight,
Bitmap firstFrame) {
this(
new GifState(
bitmapPool,
new GifFrameLoader(
// TODO(b/27524013): Factor out this call to Glide.get()
Glide.get(context),
gifDecoder,
targetFrameWidth,
targetFrameHeight,
frameTransformation,
firstFrame)));
}
/*---------------------------------------------------------------------------------------------*/
//glide4.0 GifFrameLoader.java
private final GifDecoder gifDecoder;//私有屬性
public GifFrameLoader(
Glide glide,
GifDecoder gifDecoder,
int width,
int height,
Transformation<Bitmap> transformation,
Bitmap firstFrame) {
this(
glide.getBitmapPool(),
Glide.with(glide.getContext()),
gifDecoder,
null /*handler*/,
getRequestBuilder(Glide.with(glide.getContext()), width, height),
transformation,
firstFrame);
}
@SuppressWarnings("PMD.ConstructorCallsOverridableMethod")
GifFrameLoader(
BitmapPool bitmapPool,
RequestManager requestManager,
GifDecoder gifDecoder,
Handler handler,
RequestBuilder<Bitmap> requestBuilder,
Transformation<Bitmap> transformation,
Bitmap firstFrame) {
this.requestManager = requestManager;
if (handler == null) {
handler = new Handler(Looper.getMainLooper(), new FrameLoaderCallback());
}
this.bitmapPool = bitmapPool;
this.handler = handler;
this.requestBuilder = requestBuilder;
//看這里
this.gifDecoder = gifDecoder;
setFrameTransformation(transformation, firstFrame);
}
(2)獲取decoder
從上面Glide4.0的GifDrawable構(gòu)造中可以看到gifDecoder被傳遞到GifFrameLoader中賦值給私有屬性gifDecoder;,而GifFrameLoader又被傳入GifState中并被賦值給它的成員變量frameLoader,那要怎么獲取frameLoader?
從源碼中,可以看到GifDrawable提供了getConstantState()方法來(lái)獲取state變量(這個(gè)變量的類型就是GifState),但是GifState并沒(méi)有g(shù)et方法獲取frameLoader,frameLoader對(duì)象中的gifDecoder也是私有,也沒(méi)有提供get方法來(lái)獲取,那么我們只能通過(guò)反射來(lái)獲取了。
//源碼
//glide4.0 GifDrawable.java
private final GifState state;
@Override
public ConstantState getConstantState() {
return state;
}
static class GifState extends ConstantState {
static final int GRAVITY = Gravity.FILL;
final BitmapPool bitmapPool;
final GifFrameLoader frameLoader;
public GifState(BitmapPool bitmapPool, GifFrameLoader frameLoader) {
this.bitmapPool = bitmapPool;
this.frameLoader = frameLoader;
}
@Override
public Drawable newDrawable(Resources res) {
return newDrawable();
}
@Override
public Drawable newDrawable() {
return new GifDrawable(this);
}
@Override
public int getChangingConfigurations() {
return 0;
}
}
通過(guò)反射來(lái)獲取獲取decoder
.listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable gifDrawable, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
try {
int duration = 0;
// 計(jì)算動(dòng)畫(huà)時(shí)長(zhǎng)
//GifDecoder decoder = gifDrawable.getDecoder();//4.0開(kāi)始沒(méi)有這個(gè)方法了
Drawable.ConstantState state = gifDrawable.getConstantState();
if(state!=null){
//不能混淆GifFrameLoader和GifState類
Object gifFrameLoader = getValue(state,"frameLoader");
if(gifFrameLoader!=null){
Object decoder = getValue(gifFrameLoader,"gifDecoder");
if(decoder!=null && decoder instanceof GifDecoder){
for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
duration += ((GifDecoder) decoder).getDelay(i);
}
}
}
Log.e("Glide4.0","gif播放動(dòng)畫(huà)時(shí)長(zhǎng)duration:"+duration);
}
} catch (Throwable e) {
}
return false;
}
})
/*---------------------------------------------------------------------------------------------*/
/**
* 通過(guò)字段名從對(duì)象或?qū)ο蟮母割愔械玫阶侄蔚闹?
* @param object 對(duì)象實(shí)例
* @param fieldName 字段名
* @return 字段對(duì)應(yīng)的值
* @throws Exception
*/
public static Object getValue(Object object, String fieldName) throws Exception {
if (object == null) {
return null;
}
if (TextUtils.isEmpty(fieldName)) {
return null;
}
Field field = null;
Class<?> clazz = object.getClass();
for (; clazz != Object.class; clazz = clazz.getSuperclass()) {
try {
field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
return field.get(object);
} catch (Exception e) {
//這里甚么都不要做!并且這里的異常必須這樣寫(xiě),不能拋出去。
//如果這里的異常打印或者往外拋,則就不會(huì)執(zhí)行clazz = clazz.getSuperclass(),最后就不會(huì)進(jìn)入到父類中了
}
}
return null;
}
(3)設(shè)置gif循環(huán)播放次數(shù)
glide4.0中沒(méi)有GlideDrawableImageViewTarget類,那么怎么設(shè)置循環(huán)播放次數(shù)呢?
從glide3.7.0源碼可以發(fā)現(xiàn)GlideDrawableImageViewTarget是通過(guò)GlideDrawable的setLoopCount方法來(lái)設(shè)置循環(huán)播放次數(shù)的,查看setLoopCount具體實(shí)現(xiàn)地方是在GifDrawable,所以這里調(diào)用的其實(shí)是GifDrawable的setLoopCount方法。glide4.0中沒(méi)有GlideDrawable類和GlideDrawableImageViewTarget類,但是仍然有GifDrawable類,并且onResourceReady方法中第一個(gè)參數(shù)就是GifDrawable,所以可以直接調(diào)用GifDrawable的setLoopCount(loopCount)來(lái)設(shè)置播放次數(shù)。
//源碼
//3.7.0 GlideDrawableImageViewTarget.java
public GlideDrawableImageViewTarget(ImageView view, int maxLoopCount) {
super(view);
this.maxLoopCount = maxLoopCount;
}
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
if (!resource.isAnimated()) {
//TODO: Try to generalize this to other sizes/shapes.
// This is a dirty hack that tries to make loading square thumbnails and then square full images less costly
// by forcing both the smaller thumb and the larger version to have exactly the same intrinsic dimensions.
// If a drawable is replaced in an ImageView by another drawable with different intrinsic dimensions,
// the ImageView requests a layout. Scrolling rapidly while replacing thumbs with larger images triggers
// lots of these calls and causes significant amounts of jank.
float viewRatio = view.getWidth() / (float) view.getHeight();
float drawableRatio = resource.getIntrinsicWidth() / (float) resource.getIntrinsicHeight();
if (Math.abs(viewRatio - 1f) <= SQUARE_RATIO_MARGIN
&& Math.abs(drawableRatio - 1f) <= SQUARE_RATIO_MARGIN) {
resource = new SquaringDrawable(resource, view.getWidth());
}
}
super.onResourceReady(resource, animation);
this.resource = resource;
//********看這里******
//android studio可以通過(guò)快捷鍵Ctrl+Alt+B查看其實(shí)現(xiàn)
resource.setLoopCount(maxLoopCount);
resource.start();
}
glide4.0 計(jì)算gif一次播放時(shí)長(zhǎng)代碼:
public static void loadGifImg(Context context,String url, ImageView imageView){
GlideApp.with(context)
.asGif()
.load(url)
.placeholder(R.drawable.placeholder)
.fallback(R.drawable.fallback)
.listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(GifDrawable gifDrawable, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
try {
//設(shè)置循環(huán)播放次數(shù)為1次
gifDrawable.setLoopCount(1);
// 計(jì)算動(dòng)畫(huà)時(shí)長(zhǎng)
int duration = 0;
//GifDecoder decoder = gifDrawable.getDecoder();//4.0開(kāi)始沒(méi)有這個(gè)方法了
Drawable.ConstantState state = gifDrawable.getConstantState();
if(state!=null){
//不能混淆GifFrameLoader和GifState類
Object gifFrameLoader = getValue(state,"frameLoader");
if(gifFrameLoader!=null){
Object decoder = getValue(gifFrameLoader,"gifDecoder");
if(decoder!=null && decoder instanceof GifDecoder){
for (int i = 0; i < gifDrawable.getFrameCount(); i++) {
duration += ((GifDecoder) decoder).getDelay(i);
}
}
}
Log.e("Glide4.0","gif播放一次動(dòng)畫(huà)時(shí)長(zhǎng)duration:"+duration);
}
} catch (Throwable e) {
}
return false;
}
})
.into(imageView);
}
注意:因?yàn)橛昧朔瓷浍@取decoder,所以不能混淆GifFrameLoader和GifState類
5、設(shè)置淡入淡出動(dòng)畫(huà)
glide3.7.0
Glide.with(context)
.load(url)
.crossFade(100) //系統(tǒng)漸變動(dòng)畫(huà)
.placeholder(R.drawable.placeholder)
.fallback(R.drawable.fallback)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
glide4.0
GlideApp.with(context)
.load(url)
.transition(DrawableTransitionOptions.withCrossFade(100))//淡入淡出100m
.placeholder(R.drawable.placeholder)
.fallback(R.drawable.fallback)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
6、緩存策略
glide3.7.0
//DiskCacheStrategy.SOURCE:緩存原始數(shù)據(jù) //DiskCacheStrategy.RESULT:緩存變換(如縮放、裁剪等)后的資源數(shù)據(jù) //DiskCacheStrategy.NONE:什么都不緩存 //DiskCacheStrategy.ALL:緩存SOURC和RESULT //默認(rèn)采用DiskCacheStrategy.RESULT策略,對(duì)于download only操作要使用DiskCacheStrategy.SOURCE
glide4.0
//DiskCacheStrategy.ALL 使用DATA和RESOURCE緩存遠(yuǎn)程數(shù)據(jù),僅使用RESOURCE來(lái)緩存本地?cái)?shù)據(jù)。 // DiskCacheStrategy.NONE 不使用磁盤(pán)緩存 // DiskCacheStrategy.DATA 在資源解碼前就將原始數(shù)據(jù)寫(xiě)入磁盤(pán)緩存 // DiskCacheStrategy.RESOURCE 在資源解碼后將數(shù)據(jù)寫(xiě)入磁盤(pán)緩存,即經(jīng)過(guò)縮放等轉(zhuǎn)換后的圖片資源。 // DiskCacheStrategy.AUTOMATIC 根據(jù)原始圖片數(shù)據(jù)和資源編碼策略來(lái)自動(dòng)選擇磁盤(pán)緩存策略。 //默認(rèn)采用DiskCacheStrategy.AUTOMATIC策略 /*-------------------------------------------------------------------------------*/ //源碼 RequestOptions.java private DiskCacheStrategy diskCacheStrategy = DiskCacheStrategy.AUTOMATIC;
7、占位符、錯(cuò)誤圖片設(shè)置
glide4.0 若into中設(shè)置的是target,占位符(placeholder、error)需要在回調(diào)中再次設(shè)置,否則無(wú)效。
public static void loadImg(String url, ImageView imageView) {
//into中用Target,占位符(placeholder、error)需要在回調(diào)中設(shè)置
GlideApp.with(FanhuanApplication.getInstance().getApplication())
.asBitmap()
.load(url)
.placeholder(drawbleId) //設(shè)置資源加載過(guò)程中的占位符
.fallback(drawbleId)
.error(drawbleId)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
imageView.setImageBitmap(resource);
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
super.onLoadFailed(errorDrawable);
if(errorDrawable!=null){
imageView.setImageDrawable(errorDrawable);
}
}
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
super.onLoadStarted(placeholder);
if(placeholder!=null){
imageView.setImageDrawable(placeholder);
}
}
});
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android水平循環(huán)滾動(dòng)控件使用詳解
這篇文章主要為大家詳細(xì)介紹了android水平循環(huán)滾動(dòng)控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android Intent與IntentFilter案例詳解
這篇文章主要介紹了Android Intent與IntentFilter案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android 判斷是否能真正上網(wǎng)的實(shí)例詳解
這篇文章主要介紹了Android 判斷是否能真正上網(wǎng)的實(shí)例詳解相關(guān)資料,希望通過(guò)本文大家能夠掌握判斷是否上網(wǎng)的方法,需要的朋友可以參考下2017-10-10
Android Service開(kāi)發(fā)應(yīng)用實(shí)例
Android的服務(wù)是開(kāi)發(fā)Android應(yīng)用程序的重要組成部分。不同于活動(dòng)Activity,服務(wù)是在后臺(tái)運(yùn)行,服務(wù)沒(méi)有接口,生命周期也與活動(dòng)Activity非常不同。通過(guò)使用服務(wù)我們可以實(shí)現(xiàn)一些后臺(tái)操作,比如想從遠(yuǎn)程服務(wù)器加載一個(gè)網(wǎng)頁(yè)等,下面來(lái)看看詳細(xì)內(nèi)容,需要的朋友可以參考下2022-12-12
Android實(shí)現(xiàn)淘寶客戶端倒計(jì)時(shí)界面
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)淘寶客戶端倒計(jì)時(shí)界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
鴻蒙開(kāi)源第三方組件之連續(xù)滾動(dòng)圖像組件功能
這篇文章主要介紹了鴻蒙開(kāi)源第三方組件之連續(xù)滾動(dòng)圖像組件功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04
Android編程實(shí)現(xiàn)獲取當(dāng)前連接wifi名字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)獲取當(dāng)前連接wifi名字的方法,涉及Android針對(duì)WiFi屬性操作的相關(guān)技巧,需要的朋友可以參考下2015-11-11
Android studio 如何刪除項(xiàng)目 module
本篇文章主要介紹了Android studio 如何刪除項(xiàng)目module的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-05-05
Android實(shí)現(xiàn)波浪線效果(xml bitmap)
這篇文章主要介紹了Android xml bitmap實(shí)現(xiàn)波浪線效果,制作過(guò)程簡(jiǎn)單,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01

