sketch icon indicating copy to clipboard operation
sketch copied to clipboard

Sketch is a powerful and comprehensive image load library on Android, in addition to the basic functions, it also supports Jetpack Compose, GIF, SVG, video thumbnails, gesture zoom, huge images sampli...

logo_image Sketch Image Loader

Platform API License version_icon QQ Group

Sketch 是 Android 上的一个强大且全面的图片加载库,除了基础功能外,还支持 GIF、SVG,手势缩放、超大图采样、ExifInterface、视频缩略图、Jetpack Compose 等功能

关于 3.0 版本

  • maven groupId 改为 io.github.panpf.sketch3,因此 2.* 版本不会提示升级
  • 包名改为 com.github.panpf.sketch 因此与 2.* 版本不会冲突
  • 基于 kotlin 协程重写,API、功能实现全部重构,当一个新的库用
  • 不再要求必须使用 SketchImageView,任何 ImageView 及其子类都可以,结合自定义 Target 可以支持任意 View
  • Zoom 功能拆分成独立的可单独依赖的模块并且超大图采样功能重构且支持多线程解码速度更快
  • gif 模块现在直接依赖 android-gif-drawable 库不再二次修改,可自行升级
  • 支持 Jetpack Compose
  • 支持请求和解码拦截器
  • 参考 coil v2.2.0 版本并结合 sketch 原有功能实现,对比 coil 有以下区别:
    • sketch 最低支持 API 16,而 coil 最低仅支持 API 21
    • sketch 支持 bitmap 复用,而 coil 不支持
    • sketch 支持更加精细化的调整图片大小
    • sketch 明确区分显示、加载、下载请求
    • sketch 提供了图片缩放显示组件并且支持超大图采样

简介

  • 支持 http、asset、content、android.resource 等多种 URI
  • 支持播放 gif、webp、heif 等动图
  • 支持手势缩放及超大图采样
  • 支持下载、转换结果、内存三级缓存
  • 支持通过 Exif 纠正图片方向
  • 支持 Base64、视频帧、SVG 图片
  • 支持 Jetpack Compose
  • 支持根据 view 大小自动调整图片尺寸
  • 支持仅加载图片到内存或仅下载图片到磁盘
  • 支持节省蜂窝流量等各种实用功能
  • 支持对 URI、缓存、解码、转换、显示、占位图等各个环节的扩展
  • 基于 Kotlin 及 Kotlin 协程编写

导入

已发布到 mavenCentral

dependencies {
    implementation("io.github.panpf.sketch3:sketch:${LAST_VERSION}")
}

${LAST_VERSION}: Download (不包含 'v')

还有一些可选的模块用来扩展 sketch 的功能:

dependencies {
    // 支持 Jetpack Compose
    implementation("io.github.panpf.sketch3:sketch-compose:${LAST_VERSION}")

    // 支持下载进度蒙层、列表滑动中暂停加载、节省蜂窝流量、图片类型角标、加载 apk 文件和已安装 app 图标等实用功能
    implementation("io.github.panpf.sketch3:sketch-extensions:${LAST_VERSION}")

    // 通过 koral 的 android-gif-drawable 库的 GifDrawable 实现 gif 播放
    implementation("io.github.panpf.sketch3:sketch-gif-koral:${LAST_VERSION}")

    // 通过 Android 内置的 Movie 类实现 gif 播放
    implementation("io.github.panpf.sketch3:sketch-gif-movie:${LAST_VERSION}")

    // 支持 OkHttp
    implementation("io.github.panpf.sketch3:sketch-okhttp:${LAST_VERSION}")

    // 支持 SVG 图片
    implementation("io.github.panpf.sketch3:sketch-svg:${LAST_VERSION}")

    // 通过 Android 内置的 MediaMetadataRetriever 类实现读取视频帧
    implementation("io.github.panpf.sketch3:sketch-video:${LAST_VERSION}")

    // 通过 wseemann 的 FFmpegMediaMetadataRetriever 库实现读取视频帧
    implementation("io.github.panpf.sketch3:sketch-video-ffmpeg:${LAST_VERSION}")

    // 支持手势缩放以及超大图采样
    implementation("io.github.panpf.sketch3:sketch-zoom:${LAST_VERSION}")
}

R8 / Proguard

sketch 自己不需要配置任何混淆规则,但你可能需要为间接依赖的 Kotlin Coroutines, OkHttp, Okio 添加混淆配置

快速上手

ImageView

Sketch 为 ImageView 提供了一系列的名为 displayImage 的扩展函数,可以方便的显示图片

// http
imageView.displayImage("https://www.sample.com/image.jpg")

// File
imageView.displayImage("/sdcard/download/image.jpg")

// asset
imageView.displayImage("asset://image.jpg")

// There is a lot more...

还可以通过尾随的 lambda 函数配置参数:

imageView.displayImage("https://www.sample.com/image.jpg") {
    placeholder(R.drawable.placeholder)
    error(R.drawable.error)
    transformations(CircleCropTransformation())
    crossfade()
    // There is a lot more...
}

Jetpack Compose

需要先导入 sketch-compose 模块

AsyncImage(
    imageUri = "https://www.sample.com/image.jpg",
    modifier = Modifier.size(300.dp, 200.dp),
    contentScale = ContentScale.Crop,
    contentDescription = ""
) {
    placeholder(R.drawable.placeholder)
    error(R.drawable.error)
    transformations(CircleCropTransformation())
    crossfade()
    // There is a lot more...
}

文档

基础功能:

  • 入门
  • 播放 GIF、WEBP、HEIF 动图
  • Resize:修改图片尺寸
  • Transformation:转换图片
  • Transition:用炫酷的过渡方式显示图片
  • StateImage:占位图和错误图
  • Listener:监听请求状态和下载进度
  • Cache:了解下载、结果、内存缓存
  • Fetcher:了解 Fetcher 及扩展新的 URI 类型
  • Decoder:了解 Decoder 及扩展新的图片类型
  • Target:将加载结果应用到目标上
  • HttpStack:了解 http 部分及使用 okhttp
  • SVG:解码 SVG 静态图片
  • Video:解码视频帧
  • Exif:纠正图片方向
  • ImageOptions:统一管理图片配置
  • RequestInterceptor:拦截 ImageRequest
  • DecodeInterceptor:拦截 Bitmap 或 Drawable 解码
  • BitmapPool:复用 Bitmap,减少 GC
  • DownloadRequest:下载图片到磁盘
  • LoadRequest:加载图片获取 Bitmap
  • 预加载图片到内存
  • Lifecycle
  • Jetpack Compose

特色功能:

  • SketchImageView:通过 XML 属性配置请求
  • SketchZoomImageView:手势缩放及超大图采样
  • 提高长图在网格列表中的清晰度
  • 显示下载进度
  • 显示图片类型角标
  • 蜂窝数据网络下暂停下载图片节省流量
  • 列表滑动中暂停加载图片
  • 显示 APK 文件或已安装 APP 的图标
  • 日志

更新日志

请查看 CHANGELOG.md 文件

特别感谢

License

Copyright (C) 2022 panpf <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.