Glide加载进度(自己看)

自己看,记录glide加载进度的问题:

在build.gradle中添加 依赖

implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
implementation "com.github.bumptech.glide:okhttp3-integration:4.7.1"

在 okhttp3-integration 中已经有为我们做好了 使用 okhttp 做为glide的加载底层

此时我们只需 替换一下即可

//这个注解表示 glide一进来就要替换的module
@com.bumptech.glide.annotation.GlideModule
public class MyGlideModule extends AppGlideModule {
    @Override
    public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
        // 写入咱们的okhttp
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        // 写入咱们的okhttp的拦截器,在拦截器中监听进度
        builder.addInterceptor(new ProgressInterceptor());
        OkHttpClient okHttpClient = builder.build();
        // glide吧urlConnection替换为okhttp
        registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
}

剩下来就可以按照 郭霖的一片博客开始就行了

其实t3 之上的东西 也就是 okhttp3-integration的东西

注意点 加载的时候 一定要把 diskCacheStrategy(DiskCacheStrategy.NONE) 跳过sd卡缓存, 否则他会走sd卡,不会从网上直接下载的

再咱们要监听的地方

        //开始监听某个url
         ProgressInterceptor.addListener(mImgurl, new ProgressListener() {
            @Override
            public void onProgress(int progress) {
                Log.e("download",progress+"");
            }
        });

        //开始展示
        Glide.with(ImagDetailActivity.this).load(mImgurl).listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
            //加载失败 移除监听
                ProgressInterceptor.removeListener(mImgurl);
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
            //成功 移除监听
                Log.e("download是否第一次",isFirstResource+"---"+model+"----"+dataSource);
                ProgressInterceptor.removeListener(mImgurl);
                return false;
            }
        }).into(mIvImagDetail);

猜你喜欢

转载自blog.csdn.net/qq_33408235/article/details/81224886