android - 如何让 Glide 保持连接?

标签 android android-glide

我正在使用 Android Glide 库远程加载图像。

图像 URL 协议(protocol)是 https,因此每次建立新连接时都会因 TSL 而对性能产生影响。

我正在使用 okhttp 集成:

def glide_version = "4.10.0"
implementation "com.github.bumptech.glide:glide:$glide_version"
implementation "com.github.bumptech.glide:okhttp3-integration:$glide_version"

是否有可能让 Glide 保持连接 Activity 以获得更好的性能?

最佳答案

尝试使用自定义 ConnectionPool 设置 HTTP 客户端。查看连接池的构造函数:

public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit)

所以你可以尝试这样的事情:

@GlideModule
class YourGlideModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry
                .replace(
                        GlideUrl::class.java,
                        InputStream::class.java,
                        OkHttpUrlLoader.Factory(
                                OkHttpClient.Builder()
                                        .connectionPool(ConnectionPool(5, 5, TimeUnit.MINUTES))
                                        .build()))
    }
}

这些是默认值,但您可以找到适合您的值。

关于android - 如何让 Glide 保持连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58998568/

相关文章:

android - Glide - 如何在更新 ImageView 之前设置大小占位符等于来自 URL 的大小图像

android - 将 Google 表格自动化为 strings.xml 以进行本地化

Android - 取消 Volley 请求

java - 组合具有不同字段的子类以提高效率?

android - Glide : How to find if the image is already cached and use the cached version?

android - 集成 glide gradle 后应用程序崩溃

android - 如何从相关应用程序内部使用自定义 url 启动 Intent ?

android - ExpandableListView 中的子选择

android - 加载较小分辨率的图像,直到较大分辨率的图像完成下载

android - 在后台线程上的磁盘上滑动缓存图像