Skip to content

Latest commit

 

History

History
76 lines (53 loc) · 2.29 KB

save_cellular_traffic_zh.md

File metadata and controls

76 lines (53 loc) · 2.29 KB

节省蜂窝流量

翻译:English

Important

必须导入 sketch-extensions-viewsketch-extensions-compose 模块

节省蜂窝流量功能可以在检测到当前是蜂窝流量时将 ImageRequest 的 depth 参数设置为 Depth .LOCAL,这样就不会再从网络下载图片

配置

首先注册 SaveCellularTrafficDisplayInterceptor 请求拦截器,如下:

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

    override fun createSketch(): Sketch {
        return Sketch.Builder(this).apply {
            components {
                addRequestInterceptor(SaveCellularTrafficDisplayInterceptor())
            }
        }.build()
    }
}

/* 为单个 ImageRequest 注册 */
imageView.displayImage("https://www.sample.com/image.jpg") {
    components {
        addRequestInterceptor(SaveCellularTrafficDisplayInterceptor())
    }
}

注意:SaveCellularTrafficDisplayInterceptor 仅对 DisplayRequest 有效

然后针对单个请求开启节省蜂窝流量功能,如下:

imageView.displayImage("https://www.sample.com/image.jpg") {
    saveCellularTraffic(true)
}

最后配置节省蜂窝流量功能专用的错误状态图片,如下:

imageView.displayImage("https://www.sample.com/image.jpg") {
    saveCellularTraffic(true)

    error(R.drawable.ic_error) {
        saveCellularTrafficError(R.drawable.ic_signal_cellular)
    }
}

可选. 开启点击 ImageView 忽略节省蜂窝流量并重新显示图片功能

此功能需要使用 SketchImageView

sketchImageView.setClickIgnoreSaveCellularTrafficEnabled(true)