基于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下载

    

  



阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 徽州介绍 徽州在哪个省 徽州古城旅游 徽州古城门票 徽州古城游玩攻略 徽州古城门票价格 水墨徽州优美诗句 回到五代末 徽州盒饭 徽州府订餐电话 不灭鸿蒙 徽州 徽州古城在哪 印象徽州酒店 徽州旅游景点 一生痴绝处无梦到徽州 湖畔徽映阁二手房 徽标 徽标键 徽标图案大全 汽车徽标图案大全 徽标构图要素及寓意 小学生徽标设计图案 微标图片 徽派速写 徽派 徽派园林 徽派景观灯 徽派活动房 徽派门头 徽派建筑特点 徽派建筑图片 徽派建筑速写 徽派建筑特点顺口溜 徽派建筑在哪里 徽派建筑水彩画 徽派建筑设计图 徽派建筑的特色 徽派建筑特色 徽派室内设计 徽派建筑的特点 120平米徽派独栋别墅图 徽派建筑速写高清图片