Skip to content

Latest commit

 

History

History
93 lines (71 loc) · 3.7 KB

decode_interceptor_zh.md

File metadata and controls

93 lines (71 loc) · 3.7 KB

Decode Interceptor

翻译:English

Sketch 的解码过程支持拦截器,你可以通过拦截器来改变解码前后的输入和输出

Sketch 将解码分为 Drawable 和 Bitmap 两种,因此拦截也同样分为两种 BitmapDecodeInterceptorDrawableDecodeInterceptor

首先,实现 BitmapDecodeInterceptorDrawableDecodeInterceptor 接口定义你的 DecodeInterceptor,如下:

class MyBitmapDecodeInterceptor : BitmapDecodeInterceptor {

    // 如果当前 BitmapDecodeInterceptor 会修改返回的结果并且仅用于部分请求,那么请给一个不重复的 key 用于构建缓存 key,否则给 null 即可
    override val key: String = "MyBitmapDecodeInterceptor"

    // 用于排序,值越大在列表中越靠后。取值范围是 0 ~ 100。通常是零。只有 EngineBitmapDecodeInterceptor 可以是 100
    override val sortWeight: Int = 0

    @WorkerThread
    override suspend fun intercept(
        chain: BitmapDecodeInterceptor.Chain,
    ): Result<BitmapDecodeResult> {
        val newRequest = chain.request.newRequest {
            bitmapConfig(Bitmap.Config.ARGB_4444)
        }
        return chain.proceed(newRequest)
    }
}

class MyDrawableDecodeInterceptor : DrawableDecodeInterceptor {

    // 如果当前 DrawableDecodeInterceptor 会修改返回的结果并且仅用于部分请求,那么请给一个不重复的 key 用于构建缓存 key,否则给 null 即可
    override val key: String = "MyDrawableDecodeInterceptor"

    // 用于排序,值越大在列表中越靠后。取值范围是 0 ~ 100。通常是零。只有 EngineDrawableDecodeInterceptor 可以是 100
    override val sortWeight: Int = 0

    @WorkerThread
    override suspend fun intercept(
        chain: DrawableDecodeInterceptor.Chain,
    ): Result<DrawableDecodeResult> {
        val newRequest = chain.request.newRequest {
            disallowAnimatedImage()
        }
        return chain.proceed(newRequest)
    }
}
  1. MyBitmapDecodeInterceptor 演示了一个将所有请求的 Bitmap.Config 改为 ARGB_4444 的案例
  2. MyDrawableDecodeInterceptor 演示了一个禁止所有请求解码动图的案例
  3. 如果你想修改返回结果,就拦截 proceed 方法返回的结果,返回一个新的 BitmapDecodeResultDrawableDecodeResult 即可
  4. 如果想不再执行请求只需不执行 proceed 方法即可

然后,通过 addBitmapDecodeInterceptor() 和 addDrawableDecodeInterceptor() 方法注册你的 DecodeInterceptor,如下:

/* 为所有 ImageRequest 注册 */
class MyApplication : Application(), SketchFactory {

    override fun createSketch(): Sketch {
        return Sketch.Builder(this).apply {
            components {
                addBitmapDecodeInterceptor(MyBitmapDecodeInterceptor())
                addDrawableDecodeInterceptor(MyDrawableDecodeInterceptor())
            }
        }.build()
    }
}

/* 为单个 ImageRequest 注册 */
imageView.displayImage("file:///sdcard/sample.mp4") {
    components {
        addBitmapDecodeInterceptor(MyBitmapDecodeInterceptor())
        addDrawableDecodeInterceptor(MyDrawableDecodeInterceptor())
    }
}