public class StandardGifDecoder extends Object implements GifDecoder
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
GifDecoder.BitmapProvider, GifDecoder.GifDecodeStatus
STATUS_FORMAT_ERROR, STATUS_OK, STATUS_OPEN_ERROR, STATUS_PARTIAL_DECODE, TOTAL_ITERATION_COUNT_FOREVER
Constructor and Description |
---|
StandardGifDecoder(GifDecoder.BitmapProvider provider) |
StandardGifDecoder(GifDecoder.BitmapProvider provider,
GifHeader gifHeader,
ByteBuffer rawData) |
StandardGifDecoder(GifDecoder.BitmapProvider provider,
GifHeader gifHeader,
ByteBuffer rawData,
int sampleSize) |
Modifier and Type | Method and Description |
---|---|
void |
advance()
Move the animation frame counter forward.
|
void |
clear() |
int |
getByteSize()
Returns an estimated byte size for this decoder based on the data provided to
GifDecoder.setData(GifHeader, byte[]) , as well as internal buffers. |
int |
getCurrentFrameIndex()
Gets the current index of the animation frame, or -1 if animation hasn't not yet started.
|
ByteBuffer |
getData() |
int |
getDelay(int n)
Gets display duration for specified frame.
|
int |
getFrameCount()
Gets the number of frames read from file.
|
int |
getHeight() |
int |
getLoopCount()
Deprecated.
|
int |
getNetscapeLoopCount()
Gets the "Netscape" loop count, if any.
|
int |
getNextDelay()
Gets display duration for the upcoming frame in ms.
|
Bitmap |
getNextFrame()
Get the next frame in the animation sequence.
|
int |
getStatus()
Returns the current status of the decoder.
|
int |
getTotalIterationCount()
Gets the total count
which represents how many times the animation sequence should be displayed.
|
int |
getWidth() |
int |
read(byte[] data)
Reads GIF image from byte array.
|
int |
read(InputStream is,
int contentLength)
Reads GIF image from stream.
|
void |
resetFrameIndex()
Resets the frame pointer to before the 0th frame, as if we'd never used this decoder to
decode any frames.
|
void |
setData(GifHeader header,
byte[] data) |
void |
setData(GifHeader header,
ByteBuffer buffer) |
void |
setData(GifHeader header,
ByteBuffer buffer,
int sampleSize) |
void |
setDefaultBitmapConfig(Bitmap.Config config)
Sets the default
Bitmap.Config to use when decoding frames of a GIF. |
public StandardGifDecoder(@NonNull GifDecoder.BitmapProvider provider, GifHeader gifHeader, ByteBuffer rawData)
public StandardGifDecoder(@NonNull GifDecoder.BitmapProvider provider, GifHeader gifHeader, ByteBuffer rawData, int sampleSize)
public StandardGifDecoder(@NonNull GifDecoder.BitmapProvider provider)
public int getWidth()
getWidth
in interface GifDecoder
public int getHeight()
getHeight
in interface GifDecoder
@NonNull public ByteBuffer getData()
getData
in interface GifDecoder
public int getStatus()
GifDecoder
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.
getStatus
in interface GifDecoder
public void advance()
GifDecoder
advance
in interface GifDecoder
public int getDelay(int n)
GifDecoder
getDelay
in interface GifDecoder
n
- int index of frame.public int getNextDelay()
GifDecoder
getNextDelay
in interface GifDecoder
public int getFrameCount()
GifDecoder
getFrameCount
in interface GifDecoder
public int getCurrentFrameIndex()
GifDecoder
getCurrentFrameIndex
in interface GifDecoder
public void resetFrameIndex()
GifDecoder
resetFrameIndex
in interface GifDecoder
@Deprecated public int getLoopCount()
GifDecoder
getLoopCount
in interface GifDecoder
public int getNetscapeLoopCount()
GifDecoder
GifHeader.NETSCAPE_LOOP_COUNT_FOREVER
) means repeat indefinitely.
It must not be a negative value.
GifDecoder.getTotalIterationCount()
to know how many times the animation sequence should be displayed.getNetscapeLoopCount
in interface GifDecoder
GifHeader.NETSCAPE_LOOP_COUNT_DOES_NOT_EXIST
).public int getTotalIterationCount()
GifDecoder
GifDecoder.TOTAL_ITERATION_COUNT_FOREVER
) means repeat indefinitely.
It must not be a negative value.
The total count is calculated as follows by using GifDecoder.getNetscapeLoopCount()
.
This behavior is the same as most web browsers.
getNetscapeLoopCount() |
The total count |
---|---|
GifHeader.NETSCAPE_LOOP_COUNT_FOREVER |
GifDecoder.TOTAL_ITERATION_COUNT_FOREVER |
GifHeader.NETSCAPE_LOOP_COUNT_DOES_NOT_EXIST |
1 |
n (n > 0) |
n + 1 |
getTotalIterationCount
in interface GifDecoder
public int getByteSize()
GifDecoder
GifDecoder.setData(GifHeader, byte[])
, as well as internal buffers.getByteSize
in interface GifDecoder
@Nullable public Bitmap getNextFrame()
GifDecoder
getNextFrame
in interface GifDecoder
public int read(@Nullable InputStream is, int contentLength)
GifDecoder
read
in interface GifDecoder
is
- containing GIF file.public void clear()
clear
in interface GifDecoder
public void setData(@NonNull GifHeader header, @NonNull byte[] data)
setData
in interface GifDecoder
public void setData(@NonNull GifHeader header, @NonNull ByteBuffer buffer)
setData
in interface GifDecoder
public void setData(@NonNull GifHeader header, @NonNull ByteBuffer buffer, int sampleSize)
setData
in interface GifDecoder
public int read(@Nullable byte[] data)
GifDecoder
read
in interface GifDecoder
data
- containing GIF file.public void setDefaultBitmapConfig(@NonNull Bitmap.Config config)
GifDecoder
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.
setDefaultBitmapConfig
in interface GifDecoder