Skip to content

04. Custom decoders

davemorrissey edited this page Nov 2, 2017 · 3 revisions

The view uses Android's BitmapFactory class to decode full size images when you have tiling disabled, or are using a preview image, and BitmapRegionDecoder to decode tiles when you have tiling enabled. These classes are based on the Skia library, which has some bugs, mostly affecting BitmapRegionDecoder.

You may encounter problems displaying CMYK and grayscale PNGs, and images exported from certain graphics packages. Different devices and Android versions have different issues. The vast majority of images are displayed properly, and you can test your images on a variety of devices, but if you don't control the origin of the images you're displaying, you may require a more reliable decoder.

Implementing a custom decoder

To use your own decoder based on a different image library, implement ImageDecoder when using a preview image or you have disabled tiling, and ImageRegionDecoder when you are displaying an image using tiling. Your class must have no constructor, or a public no-arg constructor.

Before setting the image source, configure the view to use your custom classes:

imageView.setBitmapDecoderClass(MyImageDecoder.class);
imageView.setRegionDecoderClass(MyImageRegionDecoder.class);

RapidDecoder

As an example, see RapidImageDecoder and RapidImageRegionDecoder, which are based on RapidDecoder.

This library is better at decoding grayscale JPG images but cannot display CMYK JPGs and does not handle large images as well as BitmapRegionDecoder - it is significantly slower and more likely to throw out of memory errors. It appears to be very fast and reliable for PNG images, so it's a viable replacement for the default Skia-based decoder for PNGs. If you can detect the size and type of an image before displaying it, you can use different decoders for different images to get the best results.

Whenever possible, convert your images to a format Android's Skia library can support, and test them with a variety of devices.

Clone this wiki locally