首页 > 学院 > 开发设计 > 正文

Glide的简单使用

2019-11-06 09:52:21
字体:
来源:转载
供稿:网友

Glide是谷歌推出的加载图片的,我通过查找资料来记录一下如何使用glide。

首先,我们配置一些基本设置

public class MyGlideConfig implements GlideModule { //设置 @Override public void applyOptions(Context context, GlideBuilder builder) { //设置缓存、线程池的大小 MemorySizeCalculator calculator = new MemorySizeCalculator(context); int defaultMemoryCacheSize = calculator.getMemoryCacheSize(); int defaultBitmapPoolSize = calculator.getBitmapPoolSize(); int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize); int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize); builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize)); builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize)); //设置磁盘缓存大小和路径 int cacheSize = 100 * 1024 * 1024; //获取sd卡的根目录 String downloadDirectoryPath = SDCardUtil.getSdPath(context)+SDCardUtil.FILECACHE; builder.setDiskCache(new DiskLruCacheFactory(downloadDirectoryPath, cacheSize)); //设置图片显示格式(默认的格式为PREFER_RGB_565)// builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888); } @Override public void registerComponents(Context context, Glide glide) { }}

这里,调用的SDCardUtil.getSdPath()方法来设置图片缓存地址。

然后,在AndroidManifest.xml中配置Glide

<application android:name=".MallApplication" android:allowBackup="true" android:icon="@mipmap/sjmall_logo" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/APPTheme"> <meta-data android:name="com.sanji.sjmallconfig.MyGlideConfig" android:value="GlideModule" />

那么,我们可以使用了

Glide.with(context).load(goodsBean.getUrl()) //使用centerCrop是利用图片图填充ImageView设置的大小,如果ImageView的Height是match_parent则图片就会被拉伸填充 .centerCrop() //当加载网络图片时,由于加载过程中图片未能及时显示,此时可能需要设置等待时的图片,通过placeHolder()方法 .placeholder(R.mipmap.ic_launcher) //当加载图片失败时,通过error(Drawable drawable)方法设置加载失败后的图片显示 .error(R.mipmap.ic_launcher) .crossFade() //DiskCacheStrategy.SOURCE:缓存原始数据,DiskCacheStrategy.RESULT:缓存变换(如缩放、裁剪等)后的资源数据, // DiskCacheStrategy.NONE:什么都不缓存,DiskCacheStrategy.ALL:缓存SOURC和RESULT。 // 默认采用DiskCacheStrategy.RESULT策略,对于download only操作要使用DiskCacheStrategy.SOURCE。// .diskCacheStrategy(DiskCacheStrategy.RESULT) .into(vHolder.searchGoodsImg);
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表