文档地址:https://muyangmin.github.io/glide-docs-cn/doc/download-setup.html
android minSDK version 14
compile SDK verison 27
support Library Version SDK 27
如果你需要使用不同的支持库版本,你需要在你的 build.gradle
文件里去从 Glide 的依赖中去除 "com.android.support"
。例如,假如你想使用 v26 的支持库:
dependencies {
implementation ("com.github.bumptech.glide:glide:4.8.0") {
exclude group: "com.android.support"
}
implementation "com.android.support:support-fragment:26.1.0"
}
如果使用 Gradle,可从 Maven Central 或 JCenter 中添加对 Glide 的依赖。同样,你还需要添加 Android 支持库的依赖。
repositories {
mavenCentral()
maven { url 'https://maven.google.com' }
}
dependencies {
compile 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}
注意:如果可能,请尽量在你的依赖中避免使用 @aar
。如果你必须这么做,请添加 transitive=true
以确保所有必要的类都被包含到你的 API 中:
dependencies {
implementation ("com.github.bumptech.glide:glide:4.8.0@aar") {
transitive = true
}
}
<use-permission android:name="android.permission.INTERNET"/>
<use-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<use-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
如果Glide 配置的缓存地址为外部缓存配置如下:
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
builder.setLogLevel(Log.DEBUG); //设置日志级别
int diskCacheSizeBytes = 1024 * 1024 * 100; //100 MB
String folderName = "test"; //设置缓存目录
builder.setDiskCache(new ExternalPreferredCacheDiskCacheFactory(context,
builder.setDiskCacheExecutor(newDiskCacheExecutor(myUncaughtThrowableStrategy));
builder.setResizeExecutor(newSourceExecutor(myUncaughtThrowableStrategy));
}
还需要用到
<use-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
一:加载图片
Glide.with(fragment)
.load(myUrl)
.into(imageView);
取消加载:
Glide.with(context).clear(ImageView);
二:application 模块的使用
如下配置:
@GlideModule
public class MyAppGlideModule extends AppGlideModule {
@Override
public void applyOptions(@NonNull Context context, @NonNull GlideBuilder builder) {
builder.setLogLevel(Log.DEBUG); //设置日志级别
int diskCacheSizeBytes = 1024 * 1024 * 100; //100 MB
String folderName = "test"; //设置缓存目录
builder.setDiskCache(new ExternalPreferredCacheDiskCacheFactory(context, folderName, diskCacheSizeBytes));
//未捕获异常策略 (UncaughtThrowableStrategy) 在加载图片时假如发生了一个异常 (例如, OOM), Glide 将会使用一个 GlideExecutor.UncaughtThrowableStrategy 。
final GlideExecutor.UncaughtThrowableStrategy myUncaughtThrowableStrategy = new GlideExecutor.UncaughtThrowableStrategy() {
@Override
public void handle(Throwable t) {
}
};
builder.setDiskCacheExecutor(newDiskCacheExecutor(myUncaughtThrowableStrategy));
builder.setResizeExecutor(newSourceExecutor(myUncaughtThrowableStrategy));
}
/**
* ,你可以完全禁用清单解析。这样可以改善 Glide 的初始启动时间,并避免尝试解析元数据时的一些潜在问题。要禁用清单解析,请在你的 AppGlideModule 实现中复写 isManifestParsingEnabled() 方法:
* @return
*/
@Override
public boolean isManifestParsingEnabled() {
return false;
}
}
图片加载:
import android.app.Application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.ImageView;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
/***
* @descption 图片加载工具类
* @author xiaobo
* @date 2019/1/29
* @time 15:40
* @version 1.0
*/
public class GlideLoadImageUtils {
private static final float THUMBNAILSIZE = 0.25f;
public static void loadImageWithActivity(Builder builder) {
try {
if (builder.fragment == null || TextUtils.isEmpty(builder.url)) {
throw new IllegalArgumentException("context and url must be not null");
}
GlideRequest<Drawable> with = GlideApp.with(builder.fragment).load(builder.url);
if (builder.placeholder != 0) {
with = with.placeholder(builder.placeholder);
} else if (builder.isCache) {
with = with.diskCacheStrategy(DiskCacheStrategy.ALL);
} else if (!builder.isThumbnial) {
with = with.thumbnail(THUMBNAILSIZE);
}
with.error(builder.placeholder).fallback(builder.placeholder).into(builder.imageView);
} catch (IllegalArgumentException e) {
Log.i("info",e.getMessage());
}
}
public static void clearImage(Builder builder) {
GlideApp.with(builder.context).clear(builder.imageView);
builder.imageView.setImageResource(builder.placeholder);
}
public static class Builder {
private Context context;
private android.support.v4.app.Fragment fragment;
private String url;
private int placeholder;
private ImageView imageView;
private boolean isCache = true;
private boolean isThumbnial = false;
public Builder build(Context context, String url, int placeholder,ImageView imageView) {
this.context = context;
this.url = url;
this.placeholder = placeholder;
this.imageView = imageView;
return this;
}
public Builder build(Fragment fragment, String url, int placeholder, ImageView imageView) {
this.fragment = fragment;
this.url = url;
this.placeholder = placeholder;
this.imageView = imageView;
return this;
}
}
}