简介
对于Glide这个图片加载框架,相信很多人都在使用,我以前一直用的Picasso,相比于Picasso而言,Glide功能更加丰富。Glide是在泰国举行的谷歌开发者论坛上,谷歌为我们介绍了一个名叫 Glide 的图片加载库,作者是bumptech。这个库被广泛的运用在google的开源项目中,包括2014年google I/O大会上发布的官方app。
特点
(1)使用简单。
(2)可配置度高,自适应程度高。
(3)支持常见图片格式 Jpg、png、gif、webp。
(4)支持多种数据源 网络、本地、资源、Assets 等。
(5)高效缓存策略 支持Memory和Disk图片缓存 默认Bitmap格式采用RGB_565内存使用至少减少一半。
(6)生命周期集成 根据Activity/Fragment生命周期自动管理请求。
(7)高效处理Bitmap 使用Bitmap Pool使Bitmap复用,主动调用recycle回收需要回收的Bitmap,减小系统回收压力。
下载地址
github地址:https://github.com/bumptech/glide
Glide的使用
配置
①.在app/build.gradle文件当中添加如下依赖:
compile '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.READ_EXTERNAL_STORAGE" />
Glide.with()使用
①with(Context context).
使用Application上下文,Glide请求将不受Activity/Fragment生命周期控制。
②with(Activity activity).
使用Activity作为上下文,Glide的请求会受到Activity生命周期控制。
③with(FragmentActivity activity).
Glide的请求会受到FragmentActivity生命周期控制。
④with(android.app.Fragment fragment).
Glide的请求会受到Fragment 生命周期控制。
⑤with(android.support.v4.app.Fragment fragment).
Glide的请求会受到Fragment生命周期控制。
简单使用
①.加载网络图片
Glide.with(this)
.load("http://img1.imgtn.bdimg.com/it/u=2615772929,948758168&fm=21&gp=0.jpg")
.apply(options)
.into(mIvPhoto);
②.加载SD卡中图片
String path = Environment.getExternalStorageDirectory()+"/zshz/yule/ktv/xgn.jpg";
File file = new File(path);
Uri uri = Uri.fromFile(file);
Glide.with(this)
.load(uri)
.apply(options)
.into(mIvPhoto);
③.加载资源中的图片
Glide.with(this)
.load(R.drawable.tupian)
.apply(options)
.into(mIvPhoto);
④.加载网络gif动图
Glide.with(this)
.load("http://img.mp.itc.cn/upload/20161115/6163765431c44d538b37d6efb32ee885_th.jpg")
.apply(options)
.into(mIvPhoto);
⑤.加载本地gif动图
Glide.with(this)
.load(R.drawable.gif)
.apply(options)
.into(mIvPhoto);
⑥.设置缩略图比例
Glide.with(this)
.load(R.drawable.tupian)
.thumbnail(0.1f)
.apply(options)
.into(mIvPhoto);
⑦.在recyclerView中加载图片
// 初始化RecyclerView
RecyclerViewAdapter adapter=new RecyclerViewAdapter(this);
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
在adapter中调用Glide加载图片
RequestOptions options = new RequestOptions()
.centerCrop()
.fitCenter()
.override(width,height)
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher);
Glide.with(mContext)
.load(mDatas[position])
.apply(options)
.into(holder.mIvAdapterPhoto);
⑧.在recyclerView中变换图片
变换图片需要在app/build.gradle文件当中添加如下依赖:
//添加变换库
implementation 'jp.wasabeef:glide-transformations:4.0.0'
implementation 'jp.co.cyberagent.android:gpuimage:2.0.0'
具体实现代码如下:
case 1:
int width1 = Utils.dip2px(mContext, 133.33f);
int height1 = Utils.dip2px(mContext, 126.33f);
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().override(width1, height1)
.bitmapTransform(new MaskTransformation(R.drawable.mask_starfish)))
.into(holder.mIvTransformPhoto);
break;
case 2:
int width = Utils.dip2px(mContext, 150.0f);
int height = Utils.dip2px(mContext, 100.0f);
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().centerCrop() .override(width, height).bitmapTransform(
new MaskTransformation(R.drawable.mask_chat_right)))
.into(holder.mIvTransformPhoto);
break;
case 3:
Glide.with(mContext)
.load(R.drawable.tan)
.apply(new RequestOptions().bitmapTransform(
new CropTransformation( 300, 100, CropTransformation.CropType.TOP)))
.into(holder.mIvTransformPhoto);
break;
case 4:
Glide.with(mContext)
.load(R.drawable.tan)
.apply(new RequestOptions().bitmapTransform(new CropTransformation(300, 100)))
.into(holder.mIvTransformPhoto);
break;
case 5:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions() .bitmapTransform(
new CropTransformation( 300, 100, CropTransformation.CropType.BOTTOM)))
.into(holder.mIvTransformPhoto);
break;
case 6:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new CropSquareTransformation()))
.into(holder.mIvTransformPhoto);
break;
case 7:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new CropCircleTransformation()))
.into(holder.mIvTransformPhoto);
break;
case 8:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new ColorFilterTransformation( Color.argb(80, 255, 0, 0))))
.into(holder.mIvTransformPhoto);
break;
case 9:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new GrayscaleTransformation()))
.into(holder.mIvTransformPhoto);
break;
case 10:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions() .bitmapTransform(new RoundedCornersTransformation(30, 0,
RoundedCornersTransformation.CornerType.BOTTOM)))
.into(holder.mIvTransformPhoto);
break;
case 11:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new BlurTransformation(25)))
.into(holder.mIvTransformPhoto);
break;
case 12:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new ToonFilterTransformation()))
.into(holder.mIvTransformPhoto);
break;
case 13:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new SepiaFilterTransformation()))
.into(holder.mIvTransformPhoto);
break;
case 14:
Glide.with(mContext)
.load(R.drawable.tan)
.apply(new RequestOptions().bitmapTransform(new ContrastFilterTransformation(2.0f)))
.into(holder.mIvTransformPhoto);
break;
case 15:
Glide.with(mContext)
.load(R.drawable.tan)
.apply(new RequestOptions().bitmapTransform(new InvertFilterTransformation()))
.into(holder.mIvTransformPhoto);
break;
case 16:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions(). bitmapTransform(new PixelationFilterTransformation( 20)))
.into(holder.mIvTransformPhoto);
break;
case 17:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform(new SketchFilterTransformation()))
.into(holder.mIvTransformPhoto);
break;
case 18:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions().bitmapTransform( new SwirlFilterTransformation(0.5f, 1.0f, new PointF(0.5f, 0.5f))))
.into(holder.mIvTransformPhoto);
break;
case 19:
Glide.with(mContext)
.load(R.drawable.tupian)
.apply(new RequestOptions(). bitmapTransform(new BrightnessFilterTransformation(0.5f)))
.into(holder.mIvTransformPhoto);
break;
case 20:
Glide.with(mContext)
.load(R.drawable.tan)
.apply(new RequestOptions().bitmapTransform(new KuwaharaFilterTransformation( 25)))
.into(holder.mIvTransformPhoto);
break;
case 21:
Glide.with(mContext)
.load(R.drawable.tan)
.apply(new RequestOptions().bitmapTransform(new VignetteFilterTransformation( new PointF(0.5f, 0.5f),
new float[]{0.0f, 0.0f, 0.0f}, 0f, 0.75f)))
.into(holder.mIvTransformPhoto);
break;
缓存策略
①禁止内存缓存: .skipMemoryCache(true)
②清除内存缓存: Glide.get(context).clearMemory();// 必须在UI线程中调用
③设置缓存策略:
.DiskCacheStrategy.ALL //缓存源资源和转换后的资源
.DiskCacheStrategy.NONE//不做任何磁盘缓存
.DiskCacheStrategy.RESULT //缓存转换后的资源
.DiskCacheStrategy.SOURCE缓存源资源
④清除磁盘缓存: Glide.get(applicationContext).clearDiskCache();// 必须在后台线程中调用,建议同时clearMemory()
⑤获取缓存大小:
class GetDiskCacheSizeTask extends AsyncTask<File, Long, Long> {
private final TextView resultView;
public GetDiskCacheSizeTask(TextView resultView) {
this.resultView = resultView;
}
@Override
protected void onPreExecute() {
resultView.setText("Calculating...");
}
@Override
protected void onProgressUpdate(Long... values) { /* onPostExecute(values[values.length - 1]); */ }
@Override
protected Long doInBackground(File... dirs) {
try {
long totalSize = 0;
for (File dir : dirs) {
publishProgress(totalSize);
totalSize += calculateSize(dir);
}
return totalSize;
} catch (RuntimeException ex) {
final String message = String.format("Cannot get size of %s: %s", Arrays.toString(dirs), ex);
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
resultView.setText("error");
Toast.makeText(resultView.getContext(), message, Toast.LENGTH_LONG).show();
}
});
}
return 0L;
}
@Override
protected void onPostExecute(Long size) {
String sizeText = android.text.format.Formatter.formatFileSize(resultView.getContext(), size);
resultView.setText(sizeText);
}
private static long calculateSize(File dir) {
if (dir == null) return 0;
if (!dir.isDirectory()) return dir.length();
long result = 0;
File[] children = dir.listFiles();
if (children != null)
for (File child : children)
result += calculateSize(child);
return result;
}
}
⑥指定资源的优先加载顺序:
//优先加载
Glide
.with(context)
.load(heroImageUrl)
.priority(Priority.HIGH)
.into(imageViewHero);
//后加载
Glide
.with(context)
.load(itemImageUrl)
.priority(Priority.LOW)
.into(imageViewItem);
总结
Glide是一款很强大的图片加载框架,功能齐全,希望我这篇博客对大家学习Glide有帮助。