Skip to content

Latest commit

 

History

History
195 lines (140 loc) · 12.1 KB

decoder.md

File metadata and controls

195 lines (140 loc) · 12.1 KB

Decoder

Translations: 简体中文

Decoder is used to decode image files. Each supported image type has a corresponding Decoder implementation, as shown in the following table:

Format Decoder Dependent modules Android iOS Desktop Web
jpeg BitmapFactoryDecoder -
jpeg SkiaDecoder -
png BitmapFactoryDecoder -
png SkiaDecoder -
webp BitmapFactoryDecoder -
webp SkiaDecoder -
bmp BitmapFactoryDecoder -
bmp SkiaDecoder -
heif BitmapFactoryDecoder - ✅ (API 28)
avif BitmapFactoryDecoder - ✅ (API 31)
gif ImageDecoderGifDecoder sketch-animated-gif ✅ (API 28)
gif MovieGifDecoder
(Not Support resize)
sketch-animated-gif
gif SkiaGifDecoder
(Not Support resize)
sketch-animated-gif
gif KoralGifDecoder sketch-animated-gif-koral
Animated webp ImageDecoderAnimatedWebpDecoder sketch-animated-webp ✅ (API 28)
Animated webp SkiaAnimatedWebpDecoder
(Not Support resize)
sketch-animated-webp
Animated heif ImageDecoderAnimatedHeifDecoder sketch-animated-heif ✅ (API 30)
svg SvgDecoder sketch-svg
(Not Support CSS)

(Not Support CSS)

(Not Support CSS)
Video frames VideoFrameDecoder sketch-video
Video frames FFmpegVideoFrameDecoder sketch-video-ffmpeg
Apk Icon ApkIconDecoder sketch-extensions-core

Important

The above components all support automatic registration. You only need to import them without additional configuration. If you need to register manually, please read the documentation: 《Register component》

Extend Decoder

First implement the Decoder interface to define your Decoder and its Factory

Then refer to the document 《Register component》 to register your Decoder.

Caution

  1. Customizing Decoder requires applying many properties related to image quality and size in ImageRequest, such as size, colorType, colorSpace, etc. You can refer to other Decoder implementations
  2. If your Decoder is decoding animated images, you must determine the ImageRequest .disallowAnimatedImage parameter.

Decoding Properties

BitmapColorType

BitmapColorType is used to set the color type of the bitmap. The optional values are:

  • FixedColorType: always use the specified color type
  • LowQualityColorType: Prioritize low-quality color types
    • jpeg images on the Android platform use RGB_565, and other values use the default value.
    • jpeg and webp images on non-Android platforms use RGB_565, others use ARGB_4444
  • HighQualityColorType: Give priority to high-quality color types
    • On the Android platform, API 26 and above use RGBA_F16, and others use the default value.
    • Always use RGBA_F16 on non-Android platforms

Example:

ImageRequest(context, "https://example.com/image.jpg") {
    // Use specified color type on Android platform
    colorType(Bitmap.Config.RGB_565)

    // Use specified color type on non-Android platforms
    colorType(ColorType.RGBA_F16)

    // Prioritize lower quality color types
    colorType(LowQualityColorType)

    // Prioritize high-quality color types
    colorType(HighQualityColorType)
}

BitmapColorSpace

BitmapColorSpace is used to set the color space of the bitmap. The optional values are:

  • FixedColorSpace: Always use the specified color space

Example:

ImageRequest(context, "https://example.com/image.jpg") {
    // Use specified color space on Android platform
    colorSpace(ColorSpace.Named.DISPLAY_P3)

    // Use specified color space on non-Android platforms
    colorSpace(ColorSpace.displayP3)
}

preferQualityOverSpeed

preferQualityOverSpeed is used to set quality priority when decoding. It can only be used on the Android platform.

Example:

ImageRequest(context, "https://example.com/image.jpg") {
    preferQualityOverSpeed(true)
}