com.bumptech.glide.Registry.replace()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(299)

本文整理了Java中com.bumptech.glide.Registry.replace方法的一些代码示例,展示了Registry.replace的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Registry.replace方法的具体详情如下:
包路径:com.bumptech.glide.Registry
类名称:Registry
方法名:replace

Registry.replace介绍

[英]Removes all default and previously registered ModelLoaderFactorys for the given data and model class and replaces all of them with the single ModelLoader provided.

If you're attempting to add additional functionality or add a backup that should run only after the default ModelLoaders run, use #append(Class,Class,ModelLoaderFactory). This method should be used only when you want to ensure that Glide's default ModelLoaders do not run.

One good use case for this method is when you want to replace Glide's default networking library with your OkHttp, Volley, or your own implementation. Using #prepend(Class,Class,ModelLoaderFactory) or #append(Class,Class,ModelLoaderFactory) may still allow Glide's default networking library to run in some cases. Using this method will ensure that only your networking library will run and that the request will fail otherwise.
[中]删除给定数据和模型类的所有默认和以前注册的ModelLoaderFactory,并用提供的单个ModelLoader替换所有ModelLoader。
如果试图添加其他功能或添加只应在默认ModelLoader运行后运行的备份,请使用#append(Class,Class,ModelLoaderFactory)。仅当您希望确保Glide的默认ModelLoader不运行时,才应使用此方法。
这种方法的一个很好的使用案例是,当您想用OkHttp、Volley或您自己的实现替换Glide的默认网络库时。在某些情况下,使用#prepend(Class,Class,ModelLoaderFactory)或#append(Class,Class,ModelLoaderFactory)仍然可以运行Glide的默认网络库。使用此方法将确保只有您的网络库将运行,否则请求将失败。

代码示例

代码示例来源:origin: bumptech/glide

@Override
 public void registerComponents(Context context, Glide glide, Registry registry) {
  registry.replace(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(context));
 }
}

代码示例来源:origin: bumptech/glide

@Override
 public void registerComponents(@NonNull Context context, @NonNull Glide glide,
   @NonNull Registry registry) {
  registry.replace(GlideUrl.class, InputStream.class, new VolleyUrlLoader.Factory(context));
 }
}

代码示例来源:origin: bumptech/glide

@Override
 public void registerComponents(Context context, Glide glide, Registry registry) {
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
 }
}

代码示例来源:origin: bumptech/glide

@Override
 public void registerComponents(Context context, Glide glide, Registry registry) {
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
 }
}

代码示例来源:origin: bumptech/glide

@Override
 public void registerComponents(Context context, Glide glide, Registry registry) {
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
 }
}

代码示例来源:origin: bumptech/glide

@Override
 public void registerComponents(@NonNull Context context, @NonNull Glide glide,
   @NonNull Registry registry) {
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory());
 }
}

代码示例来源:origin: JessYanCoding/MVPArms

@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
  //Glide 默认使用 HttpURLConnection 做网络请求,在这切换成 Okhttp 请求
  AppComponent appComponent = ArmsUtils.obtainAppComponentFromContext(context);
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(appComponent.okHttpClient()));
}

代码示例来源:origin: JessYanCoding/ProgressManager

@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
  BaseApplication application = (BaseApplication) context.getApplicationContext();
  //Glide 底层默认使用 HttpConnection 进行网络请求,这里替换为 Okhttp 后才能使用本框架,进行 Glide 的加载进度监听
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(application.getOkHttpClient()));
}

代码示例来源:origin: Piasy/BigImageViewer

public static void init(Glide glide, OkHttpClient okHttpClient) {
  OkHttpClient.Builder builder;
  if (okHttpClient != null) {
    builder = okHttpClient.newBuilder();
  } else {
    builder = new OkHttpClient.Builder();
  }
  builder.addNetworkInterceptor(createInterceptor(new DispatchingProgressListener()));
  glide.getRegistry().replace(GlideUrl.class, InputStream.class,
      new OkHttpUrlLoader.Factory(builder.build()));
}

代码示例来源:origin: bumptech/glide

private static <X, Y> void registerMockModelLoader(Class<X> modelClass, Class<Y> dataClass,
  Y loadedData, Registry registry) {
 DataFetcher<Y> mockStreamFetcher = mock(DataFetcher.class);
 when(mockStreamFetcher.getDataClass()).thenReturn(dataClass);
 try {
  doAnswer(new Util.CallDataReady<>(loadedData))
    .when(mockStreamFetcher)
    .loadData(isA(Priority.class), isA(DataFetcher.DataCallback.class));
 } catch (Exception e) {
  throw new RuntimeException(e);
 }
 ModelLoader<X, Y> mockUrlLoader = mock(ModelLoader.class);
 when(mockUrlLoader.buildLoadData(isA(modelClass), anyInt(), anyInt(), isA(Options.class)))
   .thenReturn(new ModelLoader.LoadData<>(mock(Key.class), mockStreamFetcher));
 when(mockUrlLoader.handles(isA(modelClass))).thenReturn(true);
 ModelLoaderFactory<X, Y> mockUrlLoaderFactory = mock(ModelLoaderFactory.class);
 when(mockUrlLoaderFactory.build(isA(MultiModelLoaderFactory.class)))
   .thenReturn(mockUrlLoader);
 registry.replace(modelClass, dataClass, mockUrlLoaderFactory);
}

代码示例来源:origin: bumptech/glide

@SuppressWarnings("unchecked")
public static <ModelT, DataT> void mock(final ModelT model, final DataT data) {
 Context context = InstrumentationRegistry.getTargetContext();
 Glide.get(context)
   .getRegistry()
   .replace(
     (Class<ModelT>) model.getClass(),
     (Class<DataT>) data.getClass(),
     new ModelLoaderFactory<ModelT, DataT>() {
      @NonNull
      @Override
      public ModelLoader<ModelT, DataT> build(
        @NonNull MultiModelLoaderFactory multiFactory) {
       return new MockModelLoader<>(model, data);
      }
      @Override
      public void teardown() {
       // Do nothing.
      }
     });
}

代码示例来源:origin: bumptech/glide

public static synchronized <T> WaitModel<T> waitOn(T model) {
 @SuppressWarnings("unchecked") ModelLoaderFactory<WaitModel<T>, InputStream> streamFactory =
   new Factory<>((Class<T>) model.getClass(), InputStream.class);
 Glide.get(InstrumentationRegistry.getTargetContext())
   .getRegistry()
   .replace(WaitModel.class, InputStream.class, streamFactory);
 return new WaitModel<>(model);
}

代码示例来源:origin: sunfusheng/GlideImageView

@Override
  public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    super.registerComponents(context, glide, registry);
    registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(ProgressManager.getOkHttpClient()));
  }
}

代码示例来源:origin: albert-lii/ImageViewer

@Override
  public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    super.registerComponents(context, glide, registry);
    // 添加拦截器到 Glide
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(new ProgressInterceptor());
    OkHttpClient okHttpClient = builder.build();
    registry.replace(GlideUrl.class, InputStream.class,  new OkHttpUrlLoader.Factory(okHttpClient));
  }
}

代码示例来源:origin: JessYanCoding/MVPArt

@Override
public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
  //Glide 默认使用 HttpURLConnection 做网络请求,在这切换成 Okhttp 请求
  AppComponent appComponent = ArtUtils.obtainAppComponentFromContext(context);
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(appComponent.okHttpClient()));
}

代码示例来源:origin: lizixian18/StarrySky

@Override
  public void registerComponents(@NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
    super.registerComponents(context, glide, registry);
    HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
    OkHttpClient mHttpClient = new OkHttpClient().newBuilder()
        .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
        .build();
    registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(mHttpClient));
  }
}

代码示例来源:origin: jruesga/rview

@Override
public final void registerComponents(
    @NonNull Context context, @NonNull Glide glide, @NonNull Registry registry) {
  final File cacheDir = CacheHelper.getAvatarsCacheDir(context);
  OkHttpClient client = NetworkingHelper.createNetworkClient()
      .addNetworkInterceptor(chain -> {
        Response originalResponse = chain.proceed(chain.request());
        return CacheHelper.addCacheControl(originalResponse.newBuilder()).build();
      })
      .addInterceptor(chain -> {
        Response response = chain.proceed(chain.request());
        // Skip Gravatars identicons. They are ugly.
        String url = response.request().url().toString();
        String contentDisposition = response.header("Content-Disposition");
        if (isGravatarIdenticonUrl(url) && TextUtils.isEmpty(contentDisposition)) {
          // Gravatars identicons doesn't have an inline content disposition
          response.close();
          return response.newBuilder().code(404).build();
        }
        return response;
      })
      .cache(new Cache(cacheDir, CacheHelper.MAX_DISK_CACHE))
      .build();
  OkHttpUrlLoader.Factory okhttp3 = new OkHttpUrlLoader.Factory(client);
  glide.getRegistry().replace(GlideUrl.class, InputStream.class, okhttp3);
}

代码示例来源:origin: Shimingli/ImageLoader

/**
 OkHttp 是一个底层网络库(相较于 Cronet 或 Volley 而言),尽管它也包含了 SPDY 的支持。
 OkHttp 与 Glide 一起使用可以提供可靠的性能,并且在加载图片时通常比 Volley 产生的垃圾要少。
 对于那些想要使用比 Android 提供的 HttpUrlConnection 更 nice 的 API,
 或者想确保网络层代码不依赖于 app 安装的设备上 Android OS 版本的应用,OkHttp 是一个合理的选择。
 如果你已经在 app 中某个地方使用了 OkHttp ,这也是选择继续为 Glide 使用 OkHttp 的一个很好的理由,就像选择其他网络库一样。
 添加 OkHttp 集成库的 Gradle 依赖将使 Glide 自动开始使用 OkHttp 来加载所有来自 http 和 https URL 的图片
 * @param context
 * @param glide
 * @param registry
 */
@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
  super.registerComponents(context, glide, registry);
  registry.replace(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(ProgressManager.getOkHttpClient()));
}

代码示例来源:origin: leftcoding/GankLy

@Override
public void registerComponents(Context context, Glide glide, Registry registry) {
  OkHttpClient client = getHttpClient();
  client.networkInterceptors().add(createInterceptor(new DispatchingProgressListener()));
  registry.replace(GlideUrl.class, InputStream.class, new MyOkHttpUrlLoader.Factory(client));
}

相关文章