基于Glide 4.4 实现加载进度监听 超大图片显示 自定义磁盘缓存路径
来源:互联网 发布:3d地图制作软件 编辑:程序博客网 时间:2024/04/30 04:13
Glide升级V4后 导致以前封装的实用类无法编译 相关资料较少 所以根据项目需要 我们来实现三个项目中常用的功能
1:实现加载进度监听
参考资料 Android图片加载框架最全解析(七),实现带进度的Glide图片加载功能
具体实现方法参考郭霖大神的代码 这里说一下替换类
使用该官方的okhttp辅助库
compile 'com.github.bumptech.glide:okhttp3-integration:4.3.1'将辅助库中的 OkHttpStreamFetcher 和 OkHttpUrlLoader 类 与demo中的的 OkHttpFetcher、OkHttpGlideUrlLoader类替换
升级V4后 实现GlideModule类 里面的 registerComponents参数 发生了变化
升级前:
public class MyGlideModule implements GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { } @Override public void registerComponents(Context context, Glide glide) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.addInterceptor(new ProgressInterceptor()); OkHttpClient okHttpClient = builder.build(); glide.register(GlideUrl.class, InputStream.class, new OkHttpGlideUrlLoader.Factory(okHttpClient)); }}升级后
public class GlideCache implements GlideModule { @Override public void applyOptions(Context context, GlideBuilder builder) { } @Override public void registerComponents(Context context, Glide glide, Registry registry) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.addInterceptor(new ProgressInterceptor()); OkHttpClient okHttpClient = builder.build(); registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient)); }}
在manifest配置文件配置你的实现Module
<meta-data android:name="com.demo.glide.GlideCache" android:value="GlideModule" />
使用方法
ProgressInterceptor.addListener(url, new ProgressListener() { @Override public void onProgress(int progress) { progressDialog.setProgress(progress); } }); SimpleTarget<Drawable> simpleTarget = new SimpleTarget<Drawable>() { @Override public void onStart() { super.onStart(); progressDialog.show(); } @Override public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) { progressDialog.dismiss(); ProgressInterceptor.removeListener(url); imageView2.setImageDrawable(resource); } }; RequestOptions options = new RequestOptions() .diskCacheStrategy(DiskCacheStrategy.NONE) //测试进度实现 采用不缓存 .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); Glide.with(this) .load(url) .apply(options) .into(simpleTarget);
实现超大图片方法(类似微博长图)
使用最多是 SubsamplingScaleImageView
我们在gradle中导入
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.9.0'
由于subsampling-scale-image-view不支持网络图片直接展示 但是支持图片缓存文件显示 所以我们利用Glide的缓存来实现
ProgressInterceptor.addListener(path, new ProgressListener() { @Override public void onProgress(int progress) { progressDialog.setProgress(progress); } }); SimpleTarget<File> simpleTarget = new SimpleTarget<File>() { @Override public void onStart() { super.onStart(); progressDialog.show(); } @Override public void onResourceReady(File resource, Transition<? super File> transition) { imageView.setImage(ImageSource.uri(resource.getAbsolutePath()), new ImageViewState(1.0f, new PointF(0, 0), 0)); progressDialog.dismiss(); ProgressInterceptor.removeListener(path); } }; Glide.with(getActivity()).asFile().load(path).into(simpleTarget);关于更多的subsampling-scale-image-view使用方法请参考其他
3:自定义磁盘缓存路径
public class GlideCache implements GlideModule { public static final int GLIDE_DISK_SIZE = 1024 * 1024 * 1000; // 图片缓存子目录 public static final String GLIDE_DISK = Environment.getExternalStorageDirectory().getPath() + "/GlideV4/GlideCache"; @Override public void applyOptions(Context context, GlideBuilder builder) { builder.setDiskCache(new DiskLruCacheFactory(GLIDE_DISK, GLIDE_DISK_SIZE)); //builder.setLogLevel(Log.DEBUG); } @Override public void registerComponents(Context context, Glide glide, Registry registry) { OkHttpClient.Builder builder = new OkHttpClient.Builder(); builder.addInterceptor(new ProgressInterceptor()); OkHttpClient okHttpClient = builder.build(); registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient)); }}manifest不要忘记配置
CSDN传不上去 只好放网盘了
Demo下载
阅读全文