Class StandardGifDecoder

  • All Implemented Interfaces:
    GifDecoder

    public class StandardGifDecoder
    extends java.lang.Object
    implements GifDecoder
    Reads frame data from a GIF image source and decodes it into individual frames for animation purposes. Image data can be read from either and InputStream source or a byte[].

    This class is optimized for running animations with the frames, there are no methods to get individual frame images, only to decode the next frame in the animation sequence. Instead, it lowers its memory footprint by only housing the minimum data necessary to decode the next frame in the animation sequence.

    The animation must be manually moved forward using advance() before requesting the next frame. This method must also be called before you request the first frame or an error will occur.

    Implementation adapted from sample code published in Lyons. (2004). Java for Programmers, republished under the MIT Open Source License

    See Also:
    GIF 89a Specification
    • Method Detail

      • getData

        @NonNull
        public java.nio.ByteBuffer getData()
        Specified by:
        getData in interface GifDecoder
      • getStatus

        public int getStatus()
        Description copied from interface: GifDecoder
        Returns the current status of the decoder.

        Status will update per frame to allow the caller to tell whether or not the current frame was decoded successfully and/or completely. Format and open failures persist across frames.

        Specified by:
        getStatus in interface GifDecoder
      • advance

        public void advance()
        Description copied from interface: GifDecoder
        Move the animation frame counter forward.
        Specified by:
        advance in interface GifDecoder
      • getDelay

        public int getDelay​(int n)
        Description copied from interface: GifDecoder
        Gets display duration for specified frame.
        Specified by:
        getDelay in interface GifDecoder
        Parameters:
        n - int index of frame.
        Returns:
        delay in milliseconds.
      • getNextDelay

        public int getNextDelay()
        Description copied from interface: GifDecoder
        Gets display duration for the upcoming frame in ms.
        Specified by:
        getNextDelay in interface GifDecoder
      • getFrameCount

        public int getFrameCount()
        Description copied from interface: GifDecoder
        Gets the number of frames read from file.
        Specified by:
        getFrameCount in interface GifDecoder
        Returns:
        frame count.
      • getCurrentFrameIndex

        public int getCurrentFrameIndex()
        Description copied from interface: GifDecoder
        Gets the current index of the animation frame, or -1 if animation hasn't not yet started.
        Specified by:
        getCurrentFrameIndex in interface GifDecoder
        Returns:
        frame index.
      • resetFrameIndex

        public void resetFrameIndex()
        Description copied from interface: GifDecoder
        Resets the frame pointer to before the 0th frame, as if we'd never used this decoder to decode any frames.
        Specified by:
        resetFrameIndex in interface GifDecoder
      • getLoopCount

        @Deprecated
        public int getLoopCount()
        Deprecated.
        Description copied from interface: GifDecoder
        Gets the "Netscape" loop count, if any. A count of 0 means repeat indefinitely.
        Specified by:
        getLoopCount in interface GifDecoder
        Returns:
        loop count if one was specified, else 1.
      • getNextFrame

        @Nullable
        public android.graphics.Bitmap getNextFrame()
        Description copied from interface: GifDecoder
        Get the next frame in the animation sequence.
        Specified by:
        getNextFrame in interface GifDecoder
        Returns:
        Bitmap representation of frame.
      • read

        public int read​(@Nullable
                        java.io.InputStream is,
                        int contentLength)
        Description copied from interface: GifDecoder
        Reads GIF image from stream.
        Specified by:
        read in interface GifDecoder
        Parameters:
        is - containing GIF file.
        Returns:
        read status code (0 = no errors).
      • clear

        public void clear()
        Specified by:
        clear in interface GifDecoder
      • setData

        public void setData​(@NonNull
                            GifHeader header,
                            @NonNull
                            byte[] data)
        Specified by:
        setData in interface GifDecoder
      • setData

        public void setData​(@NonNull
                            GifHeader header,
                            @NonNull
                            java.nio.ByteBuffer buffer)
        Specified by:
        setData in interface GifDecoder
      • setData

        public void setData​(@NonNull
                            GifHeader header,
                            @NonNull
                            java.nio.ByteBuffer buffer,
                            int sampleSize)
        Specified by:
        setData in interface GifDecoder
      • read

        public int read​(@Nullable
                        byte[] data)
        Description copied from interface: GifDecoder
        Reads GIF image from byte array.
        Specified by:
        read in interface GifDecoder
        Parameters:
        data - containing GIF file.
        Returns:
        read status code (0 = no errors).
      • setDefaultBitmapConfig

        public void setDefaultBitmapConfig​(@NonNull
                                           android.graphics.Bitmap.Config config)
        Description copied from interface: GifDecoder
        Sets the default Bitmap.Config to use when decoding frames of a GIF.

        Valid options are Bitmap.Config.ARGB_8888 and Bitmap.Config.RGB_565. Bitmap.Config.ARGB_8888 will produce higher quality frames, but will also use 2x the memory of Bitmap.Config.RGB_565.

        Defaults to Bitmap.Config.ARGB_8888

        This value is not a guarantee. For example if set to Bitmap.Config.RGB_565 and the GIF contains transparent pixels, Bitmap.Config.ARGB_8888 will be used anyway to support the transparency.

        Specified by:
        setDefaultBitmapConfig in interface GifDecoder