ImageLoader,Picasso,Glide,Fresco图片加载库对比
Min Sdk Version - 使用 Glide 需要 min SDK 版本 API 14 (Ice Cream Sandwich) 或更高。
Compile Sdk Version - Glide 必须使用 API 27 (Oreo MR1) 或更高版本的 SDK 来编译。
Support Library Version - Glide 使用的支持库版本为 27。
//Support Library
implementation 'com.android.support:appcompat-v7:27.1.1'
//glide
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
# for DexGuard only
-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
Glide.with(context) .load(url).into(imageView)
Glide.with(context).load(url).placeholder(R.drawable.placeholder).into(imageView);
Glide.with(context).load(url).error(R.drawable.error).into(imageView);
Glide.with(context).load(url).fallback(R.drawable.fallback).into(imageView);
request options 可以实现(包括但不限于):
占位符(Placeholders)
转换(Transformations)
缓存策略(Caching Strategies)
组件特有的设置项,例如编码质量,或Bitmap的解码配置等
RequestOptions options = new RequestOptions().circleCrop();
RequestOptions options = new RequestOptions().override(100, 100);
RequestOptions options = new RequestOptions().skipMemoryCache(true);
RequestOptions options = new RequestOptions() .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
Glide.with(context).load(url).apply(options).into(imageView);
Glide.with(context).load(url).thumbnail(0.01F).into(imageView);
Glide.with(context).load(url)
.thumbnail(Glide
.with(context)
.load(thumbnailUrl)
.thumbnail(0.1F))
.into(imageView);
Glide.with(context).load(url).preload();
public void downloadImage() {
new Thread(new Runnable() {
@Override
public void run() {
try {
String url = "http://imgsrc.baidu.com/forum/w=580/sign=5729249949540923aa696376a259d1dc/ca816763f6246b60d6ab68ceebf81a4c500fa268.jpg";
final Context context = getApplicationContext();
final FutureTarget<File> target = Glide
.with(context)
.applyDefaultRequestOptions(new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC))//硬盘缓存策略:必須指定否則下載失敗
.asFile()
.load(url)
.submit(100, 100);
final File imageFile = target.get();//耗时操作必须在子线程
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(context, imageFile.getPath(), Toast.LENGTH_LONG).show();
ImageLoader.displayImage(context,imageFile.getPath(),mImg);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
/**
* 通过URL得到Bitmap对象
*
* @param context
* @param url
* @param listener
*/
public static void getBitmap(Context context, String url, final IGetBitmapListener listener) {
GlideApp.with(context)
.asBitmap()
.load(url)
.into(new BitmapTarget() {
@Override
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
if (listener != null) {
listener.onBitmap(resource);
}
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
if (listener != null) {
listener.onFailed();
}
}
});
}
/**
* 通过URL得到Drawable对象
*
* @param context
* @param url
* @param listener
*/
public static void getDrawable(Context context, String url, final IGetDrawableListener listener) {
GlideApp.with(context)
.asDrawable()
.load(url)
.into(new DrawableTarget() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
if (listener != null) {
listener.onDrawable(resource);
}
}
@Override
public void onLoadFailed(@Nullable Drawable errorDrawable) {
if (listener != null) {
listener.onFailed();
}
}
});
}
/***
* 清理内存缓存 可以在UI主线程中进行
* @param context
*/
public static void clearMemoryCache(Context context) {
try {
if (Looper.myLooper() == Looper.getMainLooper()) { //只能在主线程执行
GlideApp.get(context).clearMemory();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/***
* 清理磁盘缓存 需要在子线程中执行
* @param context
*/
public static void clearDiskCache(final Context context) {
try {
if (Looper.myLooper() == Looper.getMainLooper()) {
new Thread(new Runnable() {
@Override
public void run() {
GlideApp.get(context).clearDiskCache();
}
}).start();
} else {
Glide.get(context).clearDiskCache();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* @author SummerSoft.L
* @description glide 全局配置https://muyangmin.github.io/glide-docs-cn/doc/configuration.html
*
*/
@GlideModule
public class MyAppGlideModule extends com.bumptech.glide.module.AppGlideModule {
/***
*更改Glide配置
* @param context
* @param builder
*/
@Override
public void applyOptions(Context context, GlideBuilder builder) {
builder.setDefaultRequestOptions(
new RequestOptions()
.format(DecodeFormat.PREFER_RGB_565)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.placeholder(R.mipmap.ic_launcher)
.skipMemoryCache(false)); //Glide默认开启内存缓存。关掉内存缓存:true
}
/**
* 替换Glide组件
* @param context
* @param glide
* @param registry
*/
@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
super.registerComponents(context, glide, registry);
}
}