项目作者: panpf

项目描述 :
Sketch 是 Android 上一个强大且全面的图片加载器,支持 GIF,手势缩放以及分块显示超大图片。Sketch is a powerful and comprehensive image loader on Android, with support for GIF, gesture zooming, block display super large image
高级语言: Java
项目地址: git://github.com/panpf/sketch.git


Sketch Image Loader" class="reference-link">logo_image Sketch Image Loader

Platform
License
version_icon
QQ Group

Translations: 简体中文

Sketch is an image loading library specially designed for Compose Multiplatform and Android View. It
has the following features:

  • Multiple sources: Supports loading images from multiple sources such as http, file,
    compose resource, android asset/content/resource, etc.
  • Powerful functions: Supports three-level caching, automatically cancels requests, automatically
    adjusts image size, automatically rotates images according to Exif Orientation, etc.
  • Rich functions: Supports Animated image, SVG images, Base64 images, and video frames
  • Easy to expand: Supports expansion of various aspects such as caching, decoding, transformation,
    transition, placeholder, etc.
  • Extended functions: Practical extensions such as pausing downloads when cellular data is
    provided, pausing loading during list scrolling, image type badges, download progress indicators,
    etc.
  • Modern: Completely based on Kotlin and Kotlin coroutine design

Sample App

Install

Published to mavenCentral

${LAST_VERSION}: Download (Not included ‘v’)

Compose Multiplatform:

Import the required Compose and network modules:

  1. // Provides the core functions of Sketch as well as singletons and extension
  2. // functions that rely on singleton implementations
  3. implementation("io.github.panpf.sketch4:sketch-compose:${LAST_VERSION}")
  4. // Provides the ability to load network images
  5. implementation("io.github.panpf.sketch4:sketch-http:${LAST_VERSION}")

[!IMPORTANT]
To improve the performance of compose, please copy compose_compiler_config.conf under
the sketch-core module file to your project and configure it according to
the Compose Stability Configuration documentation

Android View:

Import the required View and network modules:

  1. // Provides the core functions of Sketch as well as singletons and extension
  2. // functions that rely on singleton implementations
  3. implementation("io.github.panpf.sketch4:sketch-view:${LAST_VERSION}")
  4. // Provides the ability to load network images
  5. implementation("io.github.panpf.sketch4:sketch-http:${LAST_VERSION}")

Optional modules:

  1. // Use Android or Skia's built-in decoder to decode gif animations and play them
  2. implementation("io.github.panpf.sketch4:sketch-animated-gif:${LAST_VERSION}")
  3. // [Android only] Use the GifDrawable of the android-gif-drawable library to decode and play gif animations
  4. implementation("io.github.panpf.sketch4:sketch-animated-gif-koral:${LAST_VERSION}")
  5. // [Android only] Android or Skia's built-in decoder decodes heif animations and plays them
  6. implementation("io.github.panpf.sketch4:sketch-animated-heif:${LAST_VERSION}")
  7. // Use Android or Skia's built-in decoder to decode webp animations and play them
  8. implementation("io.github.panpf.sketch4:sketch-animated-webp:${LAST_VERSION}")
  9. // Support accessing compose resources through uri or placeholder, fallback, error, etc.
  10. implementation("io.github.panpf.sketch4:sketch-compose-resources:${LAST_VERSION}")
  11. implementation("io.github.panpf.sketch4:sketch-extensions-compose-resources:${LAST_VERSION}")
  12. // Provides practical functions such as download progress, image type icons,
  13. // pausing loading during list scrolling, and saving cellular traffic.
  14. implementation("io.github.panpf.sketch4:sketch-extensions-compose:${LAST_VERSION}")
  15. implementation("io.github.panpf.sketch4:sketch-extensions-view:${LAST_VERSION}")
  16. // [Android only] Support icon loading of apk files via file path
  17. implementation("io.github.panpf.sketch4:sketch-extensions-apkicon:${LAST_VERSION}")
  18. // [Android only] Support loading icons of installed apps by package name and version code
  19. implementation("io.github.panpf.sketch4:sketch-extensions-appicon:${LAST_VERSION}")
  20. // [JVM only] Support using HttpURLConnection to access network images
  21. implementation("io.github.panpf.sketch4:sketch-http-hurl:${LAST_VERSION}")
  22. // [JVM only] Support using OkHttp to access network images
  23. implementation("io.github.panpf.sketch4:sketch-http-okhttp:${LAST_VERSION}")
  24. // Supports using ktor version 2.x to access network images
  25. implementation("io.github.panpf.sketch4:sketch-http-ktor2:${LAST_VERSION}")
  26. // Supports using ktor version 3.x to access network images
  27. implementation("io.github.panpf.sketch4:sketch-http-ktor3:${LAST_VERSION}")
  28. // Support SVG images
  29. implementation("io.github.panpf.sketch4:sketch-svg:${LAST_VERSION}")
  30. // [Android only] Use Android's built-in MediaMetadataRetriever class to decode video frames
  31. implementation("io.github.panpf.sketch4:sketch-video:${LAST_VERSION}")
  32. // [Android only] Decoding video frames using wseemann's FFmpegMediaMetadataRetriever library
  33. implementation("io.github.panpf.sketch4:sketch-video-ffmpeg:${LAST_VERSION}")

[!TIP]

  • sketch-compose, sketch-view Modules all depend on the singleton provided by
    the sketch-singleton module. If you don’t need the singleton, you can directly rely on
    their *-core version.
  • The sketch-http module depends on sketch-http-hurl on jvm platforms and sketch-http-ktor3
    on non-jvm platforms.

Register component

Sketch supports automatic discovery and registration of Fetcher and Decoder components, which are
implemented through ServiceLoader on the JVM platform and through the @EagerInitialization
annotation on non-JVM platforms.

All built-in modules support automatic registration. If you want to disable automatic registration,
please refer to the documentation for manual
registration: 《Register component》

R8 / Proguard

  1. Android, iOS, Web and other platforms do not need to configure any obfuscation rules
  2. The following obfuscation rules are required for desktop platforms:
    1. # -------------------------- Sketch Privider ---------------------------- #
    2. -keep class * implements com.github.panpf.sketch.util.DecoderProvider { *; }
    3. -keep class * implements com.github.panpf.sketch.util.FetcherProvider { *; }
  3. It may also be necessary to add obfuscation configurations to the indirect dependencies of
    three-party libraries such as Kotlin Coroutines, OkHttp, Okio

Quickly Started

Compose Multiplatform:

  1. // val imageUri = "/Users/my/Downloads/image.jpg"
  2. // val imageUri = file:///compose_resource/composeResources/com.github.panpf.sketch.sample.resources/files/sample.png
  3. val imageUri = "https://www.sample.com/image.jpg"
  4. AsyncImage(
  5. uri = imageUri,
  6. contentDescription = "photo"
  7. )
  8. AsyncImage(
  9. uri = imageUri,
  10. state = rememberAsyncImageState(ComposableImageOptions {
  11. placeholder(Res.drawable.placeholder)
  12. error(Res.drawable.error)
  13. crossfade()
  14. // There is a lot more...
  15. }),
  16. contentDescription = "photo"
  17. )
  18. AsyncImage(
  19. rqeuest = ComposableImageRequest(imageUri) {
  20. placeholder(Res.drawable.placeholder)
  21. error(Res.drawable.error)
  22. crossfade()
  23. // There is a lot more...
  24. },
  25. contentDescription = "photo"
  26. )
  27. Image(
  28. painter = rememberAsyncImagePainter(
  29. request = ComposableImageRequest(imageUri) {
  30. placeholder(Res.drawable.placeholder)
  31. error(Res.drawable.error)
  32. crossfade()
  33. // There is a lot more...
  34. }
  35. ),
  36. contentDescription = "photo"
  37. )

[!TIP]
placeholder(Res.drawable.placeholder) needs to import the sketch-compose-resources module

Android View:

  1. // val imageUri = "/sdcard/download/image.jpg"
  2. // val imageUri = "file:///android_asset/image.jpg"
  3. // val imageUri = "content://media/external/images/media/88484"
  4. val imageUri = "https://www.sample.com/image.jpg"
  5. imageView.loadImage(imageUri)
  6. imageView.loadImage(imageUri) {
  7. placeholder(R.drawable.placeholder)
  8. error(R.drawable.error)
  9. crossfade()
  10. // There is a lot more...
  11. }
  12. val request = ImageRequest(context, imageUri) {
  13. placeholder(R.drawable.placeholder)
  14. error(R.drawable.error)
  15. crossfade()
  16. target(imageView)
  17. // There is a lot more...
  18. }
  19. context.sketch.enqueue(request)

Documents

Basic functions:

Featured functions:

Change log

Please review the CHANGELOG.md file

Test Platform

  • Android: Emulator; Arm64; API 21-34
  • Desktop: macOS; 14.6.1; JDK 17
  • iOS: iphone 16 simulator; iOS 18.1
  • Web: Chrome; 130

Run Sample App

Prepare the environment:

  1. Android Studio: Koala+ (2024.1.1+)
  2. JDK: 17+
  3. Use kdoctor to check the running environment and follow the prompts to install the required
    software
  4. Android Studio installs the Kotlin Multiplatform and Compose Multiplatform IDE Supportplugins

Run the sample app:

  1. Clone the project and open it using Android Studio
  2. The running configurations of each platform have been added to the .run directory. After
    synchronization is completed, directly select the running configuration of the corresponding
    platform in the running configuration drop-down box at the top of Android Studio and click Run.
  3. The running configuration of the ios platform requires you to manually create it according to the
    template, as follows:
    1. Copy the .run/iosSample.run.template.xml file and remove the .template suffix.
      The .ignore file has been configured to ignore iosSample.run.xml
    2. Click Edit Configurations in the run configuration drop-down box at the top,
      select iosSample and then configure Execute target

About version 4.0

  • The maven groupId is upgraded to io.github.panpf.sketch4, so versions 2.* and 3.* will not
    prompt for upgrade
  • Version 4.0 is specially built for Compose Multiplatform, so there are many breaking changes in
    the API, please upgrade with caution
  • Version 4.0 has made a lot of simplifications and is much simpler than version 3.0, please check
    the update log for details
  • Android minimum API raised to API 21
  • Kotlin version upgraded to 2.0.0

Special thanks

My Projects

The following are my other open source projects. If you are interested, you can learn about them:

  • zoomimage: Library for zoom images, supported Android View,
    Compose and Compose Multiplatform; supported double-click zoom, One or two fingers gesture zoom,
    single-finger drag, inertial sliding, positioning, rotation, super-large image subsampling and
    other functions.
  • assembly-adapter: A library on Android that provides
    multi-type Item implementations for various adapters. Incidentally, it also provides the most
    powerful divider for RecyclerView.
  • sticky-item-decoration: RecyclerView sticky item
    implementation

License

Apache 2.0. See the LICENSE file for details.