Glide之旅 —— Key

本文深入探讨了Glide框架中的GlideUrl组件如何用于图片加载的过程。从源码角度解析了GlideUrl如何被创建及使用,并介绍了它在缓存策略中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

glide是谷歌推荐的Android图片加载框架,其优秀的缓存策略、Activity的生命周期的继承、GIF图片的支持等都是为人所称道的地方。下面是用glide加载一张图片的调用。

private void loadImage() {
    Glide.with(this)
         .load("http://pic2.orsoon.com/2017/0118/20170118011032176.jpg")
         .into(ivTest);
}

那么,该框架是如何实际运作的呢,我会通过“Glide之旅”系列博客尽可能详细地将我的心得记录下来。“Glide之旅”系列文章汇总:

概述

Key(com.bumptech.glide.load.Key),顾名思义,是用来作为图片的唯一标识,那么具体又是怎样的呢?

已知实现类

Key是一个接口,其所有实现类有

  • com.bumptech.glide.load.resource.bitmap.BitmapDrawableTransformation

  • com.bumptech.glide.load.engine.prefill.BitmapPreFillRunner.UniqueKey

  • com.bumptech.glide.load.resource.bitmap.BitmapTransformation

  • com.bumptech.glide.load.resource.bitmap.CenterCrop

  • com.bumptech.glide.load.resource.bitmap.CenterInside

  • com.bumptech.glide.load.resource.bitmap.CircleCrop

  • com.bumptech.glide.load.engine.DataCacheKey

  • com.bumptech.glide.signature.EmptySignature

  • com.bumptech.glide.load.engine.EngineKey

  • com.bumptech.glide.load.resource.bitmap.FitCenter

  • com.bumptech.glide.load.resource.gif.GifDrawableTransformation

  • com.bumptech.glide.load.resource.gif.GifFrameLoader.FrameSignature

  • com.bumptech.glide.load.model.GlideUrl

  • com.bumptech.glide.signature.MediaStoreSignature

  • com.bumptech.glide.load.MultiTransformation<T>

  • com.bumptech.glide.signature.ObjectKey

  • com.bumptech.glide.load.Options

  • com.bumptech.glide.load.engine.ResourceCacheKey

  • com.bumptech.glide.load.resource.bitmap.RoundedCorners

  • com.bumptech.glide.load.resource.UnitTransformation<T>

GlideUrl

当调用前言中的loadImage()进行图片加载的时候,结合我的Glide之旅 —— DecodeJob一文中的源码流程可知,用SourceGenerator解码远程图片的时候,首先会执行到com.bumptech.glide.load.model.stream.HttpUriLoader.Factory#build(MultiModelLoaderFactory),先来看下源码

package com.bumptech.glide.load.model.stream;

...

public class HttpUriLoader implements ModelLoader<Uri, InputStream> {

    ...

    public static class Factory implements ModelLoaderFactory<Uri, InputStream> {

        @Override
        public ModelLoader<Uri, InputStream> build(MultiModelLoaderFactory multiFactory) {
            return new HttpUriLoader(multiFactory.build(GlideUrl.class, InputStream.class));
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }
}

那么显然,根据Glide之旅 —— Registry中介绍的获取注册项方法可知,需要在ModelLoaderRegistry数据集合中找出Entry第一个传参为GlideUrl.class,第二个传参为InputStream.class的数据项,可知符合条件的有

23. new MultiModelLoaderFactory.Entry(GlideUrl.class, InputStream.class, new HttpGlideUrlLoader.Factory())

那么,此时再来看下com.bumptech.glide.load.model.stream.HttpGlideUrlLoader的源码

package com.bumptech.glide.load.model.stream;

...

public class HttpGlideUrlLoader implements ModelLoader<GlideUrl, InputStream> {
    public static final Option<Integer> TIMEOUT = Option.memory(
            "com.bumptech.glide.load.model.stream.HttpGlideUrlLoader.Timeout", 2500);

    @Nullable
    private final ModelCache<GlideUrl, GlideUrl> modelCache;

    public HttpGlideUrlLoader() {
        this(null);
    }

    public HttpGlideUrlLoader(ModelCache<GlideUrl, GlideUrl> modelCache) {
        this.modelCache = modelCache;
    }

    @Override
    public LoadData<InputStream> buildLoadData(GlideUrl model, int width, int height,
                                               Options options) {
        GlideUrl url = model;
        if (modelCache != null) {
            url = modelCache.get(model, 0, 0);
            if (url == null) {
                modelCache.put(model, 0, 0, model);
                url = model;
            }
        }
        int timeout = options.get(TIMEOUT);
        return new LoadData<>(url, new HttpUrlFetcher(url, timeout));
    }

    @Override
    public boolean handles(GlideUrl model) {
        return true;
    }

    public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
        private final ModelCache<GlideUrl, GlideUrl> modelCache = new ModelCache<>(500);

        @Override
        public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
            return new HttpGlideUrlLoader(modelCache);
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }
}

可以看到,buildLoadData(GlideUrl, int, int, Options)的返回值为new LoadData<>(url, new HttpUrlFetcher(url, timeout)),其中,url就是一个用访问的图片地址转换成的GlideUrl对象

package com.bumptech.glide.load.model;

...

public interface ModelLoader<Model, Data> {
    class LoadData<Data> {
        public final Key sourceKey;
        public final List<Key> alternateKeys;
        public final DataFetcher<Data> fetcher;

        public LoadData(Key sourceKey, DataFetcher<Data> fetcher) {
            this(sourceKey, Collections.<Key>emptyList(), fetcher);
        }

        public LoadData(Key sourceKey, List<Key> alternateKeys, DataFetcher<Data> fetcher) {
            this.sourceKey = Preconditions.checkNotNull(sourceKey);
            this.alternateKeys = Preconditions.checkNotNull(alternateKeys);
            this.fetcher = Preconditions.checkNotNull(fetcher);
        }
    }

    ...

}

也就是说,该LoadData对象的sourceKey为一个GlidUrl对象,而该GlidUrl对象是有图片来源地址转换而来的,看下GlideUrl源码

package com.bumptech.glide.load.model;

...

public class GlideUrl implements Key {
    private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
    private final Headers headers;
    @Nullable
    private final URL url;
    @Nullable
    private final String stringUrl;

    @Nullable
    private String safeStringUrl;
    @Nullable
    private URL safeUrl;
    @Nullable
    private volatile byte[] cacheKeyBytes;

    private int hashCode;

    public GlideUrl(URL url) {
        this(url, Headers.DEFAULT);
    }

    public GlideUrl(String url) {
        this(url, Headers.DEFAULT);
    }

    public GlideUrl(URL url, Headers headers) {
        this.url = Preconditions.checkNotNull(url);
        stringUrl = null;
        this.headers = Preconditions.checkNotNull(headers);
    }

    public GlideUrl(String url, Headers headers) {
        this.url = null;
        this.stringUrl = Preconditions.checkNotEmpty(url);
        this.headers = Preconditions.checkNotNull(headers);
    }

    public URL toURL() throws MalformedURLException {
        return getSafeUrl();
    }

    private URL getSafeUrl() throws MalformedURLException {
        if (safeUrl == null) {
            safeUrl = new URL(getSafeStringUrl());
        }
        return safeUrl;
    }

    public String toStringUrl() {
        return getSafeStringUrl();
    }

    private String getSafeStringUrl() {
        if (TextUtils.isEmpty(safeStringUrl)) {
            String unsafeStringUrl = stringUrl;
            if (TextUtils.isEmpty(unsafeStringUrl)) {
                unsafeStringUrl = url.toString();
            }
            safeStringUrl = Uri.encode(unsafeStringUrl, ALLOWED_URI_CHARS);
        }
        return safeStringUrl;
    }

    public Map<String, String> getHeaders() {
        return headers.getHeaders();
    }

    public String getCacheKey() {
        return stringUrl != null ? stringUrl : url.toString();
    }

    @Override
    public String toString() {
        return getCacheKey();
    }

    @Override
    public void updateDiskCacheKey(MessageDigest messageDigest) {
        messageDigest.update(getCacheKeyBytes());
    }

    private byte[] getCacheKeyBytes() {
        if (cacheKeyBytes == null) {
            cacheKeyBytes = getCacheKey().getBytes(CHARSET);
        }
        return cacheKeyBytes;
    }

    @Override
    public boolean equals(Object o) {
        if (o instanceof GlideUrl) {
            GlideUrl other = (GlideUrl) o;
            return getCacheKey().equals(other.getCacheKey())
                    && headers.equals(other.headers);
        }
        return false;
    }

    @Override
    public int hashCode() {
        if (hashCode == 0) {
            hashCode = getCacheKey().hashCode();
            hashCode = 31 * hashCode + headers.hashCode();
        }
        return hashCode;
    }
}
### Swin Transformer 论文精读:Hierarchical Vision Transformer Using Shifted Windows Swin Transformer 是一种基于视觉的分层 Transformer 模型,其核心创新在于通过 **Shifted Window-based Self-Attention** 实现了线性计算复杂度,同时能够生成多尺度特征表示。这种方法在图像分类、目标检测和语义分割等任务中取得了显著的性能提升 [^2]。 #### 核心架构概述 Swin Transformer 的整体结构分为多个阶段(Stage),每个阶段包含多个 Swin Transformer Block。这些块使用 **窗口化自注意力机制** 和 **移位窗口策略** 来实现高效计算并捕捉长距离依赖关系。 - **分层特征提取** 类似于传统卷积神经网络(如 ResNet),Swin Transformer 采用分层设计来逐步降低空间分辨率并增加通道维度。这种设计允许模型从局部到全局地构建特征表示。 - **窗口划分与移位窗口机制** 在每个 Swin Transformer Block 中,输入特征图被划分为不重叠的窗口,并在这些窗口内执行自注意力计算。为了增强跨窗口的信息交互,在下一个 Block 中对窗口进行移位操作(Shifted Windows)。这种方式既减少了计算量,又保持了模型对全局信息的感知能力 [^1]。 ```python # 窗口划分伪代码示例 def window_partition(x, window_size): B, H, W, C = x.shape # 将图像划分为多个窗口 x = tf.reshape(x, shape=[B, H // window_size, window_size, W // window_size, window_size, C]) windows = tf.transpose(x, perm=[0, 1, 3, 2, 4, 5]) return tf.reshape(windows, shape=[-1, window_size, window_size, C]) # 移位窗口伪代码 def shifted_window_attention(x, window_size, shift_size): B, H, W, C = x.shape # 对特征图进行滚动操作以实现窗口移位 x = tf.roll(x, shift=(-shift_size, -shift_size), axis=(1, 2)) return window_partition(x, window_size) ``` #### 自注意力机制优化 传统的 Vision Transformer(ViT)在整个图像上应用自注意力机制,导致计算复杂度为 $O(n^2)$,其中 $n$ 是图像块的数量。而 Swin Transformer 通过将注意力限制在局部窗口内,将复杂度降低到 $O(n)$,使其适用于高分辨率图像处理 [^4]。 此外,移位窗口机制确保了相邻窗口之间的信息流动,从而避免了局部注意力带来的信息隔离问题。这种设计使得 Swin Transformer 能够在保持计算效率的同时实现全局建模能力。 #### 实验结果与性能优势 Swin Transformer 在多个视觉任务中表现出色: - **ImageNet 分类任务**:Swin-Tiny、Swin-Small、Swin-Base 和 Swin-Large 四种变体均在 ImageNet-1K 上实现了优于其他 Transformer 主干网络的 Top-1 准确率。 - **COCO 目标检测**:在 COCO 数据集上,Swin Transformer 在 Faster R-CNN 框架下达到了 SOTA 性能,mAP 超过之前的最佳方法。 - **ADE20K 语义分割**:在 ADE20K 数据集上,Swin Transformer 作为编码器也取得了领先的 mIoU 指标 [^2]。 #### 消融实验分析 论文还进行了详细的消融研究,验证了以下几个关键组件的有效性: - **窗口大小的影响**:较大的窗口有助于捕捉更广泛的上下文,但会增加计算开销。 - **移位窗口的重要性**:实验证明,移位机制可以显著提升模型性能,尤其是在长距离依赖任务中。 - **不同层级的设计**:通过对比不同层级深度和通道配置,论文展示了如何平衡精度与效率 [^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值