如何在Glide 4上使用GlideModule? [英] how to use GlideModule on Glide 4?

查看:1558
本文介绍了如何在Glide 4上使用GlideModule?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了我的应用程序,以使用Glide 4(确切地说是Glide 4.2.0). 摇篮:

I recently update my app to use Glide 4, to be precise, Glide 4.2.0. gradle:

compile 'com.github.bumptech.glide:glide:4.2.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.2.0'
compile ('com.github.bumptech.glide:okhttp3-integration:4.2.0'){
    exclude group: 'glide-parent'
}

在清单中:

<meta-data
            android:name="com.xxx.MyGlideModule"
            android:value="GlideModule"/>

GlideModule类:

GlideModule class:

@GlideModule
public class MyGlideModule extends AppGlideModule {

    @Override
    public void registerComponents(Context context, Glide glide, Registry registry) {
        OkHttpClient client = new OkHttpClient.Builder()
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .connectTimeout(30, TimeUnit.SECONDS)
                .build();

        OkHttpUrlLoader.Factory factory = new OkHttpUrlLoader.Factory(client);

        glide.getRegistry().replace(GlideUrl.class, InputStream.class, factory);
    }
}

我如何在适配器内使用滑行:

how I use glide inside an adapter:

        RequestOptions myOptions = new RequestOptions()
                .placeholder(R.drawable.ic_placeholder)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .dontAnimate()
                .skipMemoryCache(true)
                ;

        Glide.with(mContext)
                .load(Imageid[position])
                .apply(myOptions)
                .into(imageView);

使用这些代码,当我运行它时出现错误:

with these code, when I run it I got error:

java.lang.RuntimeException: Expected instanceof GlideModule, but found: [my app package].MyGlideModule@d1c2328
  at com.bumptech.glide.module.ManifestParser.parseModule(ManifestParser.java:81)
  at com.bumptech.glide.module.ManifestParser.parse(ManifestParser.java:43)
  at com.bumptech.glide.Glide.initializeGlide(Glide.java:193)
  at com.bumptech.glide.Glide.checkAndInitializeGlide(Glide.java:172)
  at com.bumptech.glide.Glide.get(Glide.java:156)
  at com.bumptech.glide.Glide.getRetriever(Glide.java:540)
  at com.bumptech.glide.Glide.with(Glide.java:566)
  at [adapter line where I implement Glide]

如何使用MyGlideModule?

how can I use MyGlideModule ?

推荐答案

Glide 4.0不必 AndroidManifest.xml 中声明"GlideModule".您只需要执行以下步骤:

Glide 4.0 need not have declare "GlideModule" in AndroidManifest.xml. You just need to following steps:

  1. YourGlideModule 扩展了 AppGlideModule ,您可以在 YourGlideModule 类中覆盖函数applyOptions.
  2. 您应该在"android studio-> build-> make project"中创建项目,这会生成GlideApp类.
  3. 使用GlideApp,例如GlideApp.with(this).load(imgUrl).into(glide_test_iv1)
  1. YourGlideModule extends AppGlideModule, you can override function applyOptions in the YourGlideModule class.
  2. You should make project in "android studio -> build -> make project", it will generate the GlideApp class.
  3. Use the GlideApp such as GlideApp.with(this).load(imgUrl).into(glide_test_iv1)

这篇关于如何在Glide 4上使用GlideModule?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆