public class GifDrawable extends Drawable implements Animatable, MediaController.MediaPlayerControl
Drawable which can be used to hold GIF images, especially animations.
Basic GIF metadata can also be examined.Drawable.Callback, Drawable.ConstantState| Modifier and Type | Field and Description |
|---|---|
protected Paint |
mPaint
Paint used to draw on a Canvas
|
| Constructor and Description |
|---|
GifDrawable(AssetFileDescriptor afd)
Creates drawable from AssetFileDescriptor.
|
GifDrawable(AssetManager assets,
String assetName)
Creates drawable from asset.
|
GifDrawable(byte[] bytes)
Creates drawable from byte array.
|
GifDrawable(ByteBuffer buffer)
Creates drawable from
ByteBuffer. |
GifDrawable(ContentResolver resolver,
Uri uri)
Creates drawable from
Uri which is resolved using resolver. |
GifDrawable(File file)
Equivalent to GifDrawable(file.getPath())}
|
GifDrawable(FileDescriptor fd)
Creates drawable from FileDescriptor
|
GifDrawable(InputStream stream)
Creates drawable from InputStream.
|
GifDrawable(Resources res,
int id)
Creates drawable from resource.
|
GifDrawable(String filePath)
Constructs drawable from given file path.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addAnimationListener(AnimationListener listener)
Adds a new animation listener
|
boolean |
canPause()
Checks whether pause is supported.
|
boolean |
canSeekBackward()
Checks whether seeking backward can be performed.
|
boolean |
canSeekForward()
Checks whether seeking forward can be performed.
|
static GifDrawable |
createFromResource(Resources res,
int resourceId)
An
GifDrawable(Resources, int) wrapper but returns null
instead of throwing exception if creation fails. |
void |
draw(Canvas canvas)
Reads and renders new frame if needed then draws last rendered frame.
|
long |
getAllocationByteCount()
Returns size of the memory needed to store pixels of this object.
|
int |
getAlpha() |
int |
getAudioSessionId()
Used by MediaPlayer.
|
int |
getBufferPercentage()
Used by MediaPlayer for secondary progress bars.
|
ColorFilter |
getColorFilter() |
String |
getComment()
Returns GIF comment
|
float |
getCornerRadius() |
Bitmap |
getCurrentFrame()
Retrieves a copy of currently buffered frame.
|
int |
getCurrentFrameIndex()
Returns zero-based index of recently rendered frame in given loop or -1 when drawable is recycled.
|
int |
getCurrentLoop()
Returns zero-based index of currently played animation loop.
|
int |
getCurrentPosition()
Retrieves elapsed time from the beginning of a current loop of animation.
|
int |
getDuration()
Retrieves duration of one loop of the animation.
|
GifError |
getError()
Retrieves last error which is also the indicator of current GIF status.
|
int |
getFrameByteCount()
Returns the minimum number of bytes that can be used to store pixels of the single frame.
|
int |
getFrameDuration(int index)
Returns duration of the given frame (in milliseconds).
|
long |
getInputSourceByteCount()
Returns length of the input source obtained at the opening time or -1 if
length cannot be determined.
|
int |
getIntrinsicHeight() |
int |
getIntrinsicWidth() |
int |
getLoopCount()
Returns loop count previously read from GIF's application extension block.
|
long |
getMetadataAllocationByteCount()
Returns the maximum possible size of the allocated memory used to store pixels and metadata of this object.
|
int |
getNumberOfFrames() |
int |
getOpacity()
|
Paint |
getPaint() |
int |
getPixel(int x,
int y)
Returns the
Color at the specified location. |
void |
getPixels(int[] pixels)
Returns in pixels[] a copy of the data in the current frame.
|
Transform |
getTransform() |
boolean |
isAnimationCompleted()
Returns whether all animation loops has ended.
|
boolean |
isPlaying()
Equivalent of
isRunning() |
boolean |
isRecycled() |
boolean |
isRunning() |
boolean |
isStateful() |
protected void |
onBoundsChange(Rect bounds) |
protected boolean |
onStateChange(int[] stateSet) |
void |
pause()
Equivalent of
stop() |
void |
recycle()
Frees any memory allocated native way.
|
boolean |
removeAnimationListener(AnimationListener listener)
Removes an animation listener
|
void |
reset()
Causes the animation to start over.
|
void |
seekTo(int position)
Seeks animation to given absolute position (within given loop) and refreshes the canvas.
|
void |
seekToFrame(int frameIndex)
Like
seekTo(int) but uses index of the frame instead of time. |
Bitmap |
seekToFrameAndGet(int frameIndex)
Like
seekToFrame(int) but performs operation synchronously and returns that frame. |
Bitmap |
seekToPositionAndGet(int position)
Like
seekTo(int) but performs operation synchronously and returns that frame. |
void |
setAlpha(int alpha) |
void |
setColorFilter(ColorFilter cf) |
void |
setCornerRadius(float cornerRadius)
Sets the corner radius to be applied when drawing the bitmap.
|
void |
setDither(boolean dither)
Deprecated.
|
void |
setFilterBitmap(boolean filter) |
void |
setLoopCount(int loopCount)
Sets loop count of the animation.
|
void |
setSpeed(float factor)
Sets new animation speed factor.
|
void |
setTintList(ColorStateList tint) |
void |
setTintMode(PorterDuff.Mode tintMode) |
void |
setTransform(Transform transform)
Specify a
Transform implementation to customize how the GIF's current Bitmap is drawn. |
boolean |
setVisible(boolean visible,
boolean restart)
Sets whether this drawable is visible.
|
void |
start()
Starts the animation.
|
void |
stop()
Stops the animation.
|
String |
toString() |
applyTheme, canApplyTheme, clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getChangingConfigurations, getConstantState, getCurrent, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getOutline, getPadding, getState, getTransparentRegion, inflate, inflate, invalidateSelf, isAutoMirrored, isFilterBitmap, isVisible, jumpToCurrentState, mutate, onLayoutDirectionChanged, onLevelChange, resolveOpacity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, unscheduleSelfprotected final Paint mPaint
public GifDrawable(@NonNull Resources res, @RawRes @DrawableRes int id) throws Resources.NotFoundException, IOException
res - Resources to read fromid - resource id (raw or drawable)Resources.NotFoundException - if the given ID does not exist.IOException - when opening failedNullPointerException - if res is nullpublic GifDrawable(@NonNull AssetManager assets, @NonNull String assetName) throws IOException
assets - AssetManager to read fromassetName - name of the assetIOException - when opening failedNullPointerException - if assets or assetName is nullpublic GifDrawable(@NonNull String filePath) throws IOException
StrictMode policy if disk reads detection is enabled.filePath - path to the GIF fileIOException - when opening failedNullPointerException - if filePath is nullpublic GifDrawable(@NonNull File file) throws IOException
file - the GIF fileIOException - when opening failedNullPointerException - if file is nullpublic GifDrawable(@NonNull InputStream stream) throws IOException
stream - stream to read fromIOException - when opening failedIllegalArgumentException - if stream does not support markingNullPointerException - if stream is nullpublic GifDrawable(@NonNull AssetFileDescriptor afd) throws IOException
GifDrawable(FileDescriptor)afd - sourceNullPointerException - if afd is nullIOException - when opening failedpublic GifDrawable(@NonNull FileDescriptor fd) throws IOException
fd - sourceIOException - when opening failedNullPointerException - if fd is nullpublic GifDrawable(@NonNull byte[] bytes) throws IOException
bytes - raw GIF bytesIOException - if bytes does not contain valid GIF dataNullPointerException - if bytes are nullpublic GifDrawable(@NonNull ByteBuffer buffer) throws IOException
ByteBuffer. Only direct buffers are supported.
Buffer can be larger than size of the GIF data. Bytes beyond GIF terminator are not accessed.buffer - buffer containing GIF dataIOException - if buffer does not contain valid GIF data or is indirectNullPointerException - if buffer is nullpublic GifDrawable(@Nullable ContentResolver resolver, @NonNull Uri uri) throws IOException
Uri which is resolved using resolver.
ContentResolver.openAssetFileDescriptor(android.net.Uri, String)
is used to open an Uri.uri - GIF Uri, cannot be null.resolver - resolver used to query uri, can be null for file:// scheme UrisIOException - if resolution fails or destination is not a GIF.public void recycle()
Bitmap.recycle() this is an advanced call and
is invoked implicitly by finalizer.public boolean isRecycled()
public int getIntrinsicHeight()
getIntrinsicHeight in class Drawablepublic int getIntrinsicWidth()
getIntrinsicWidth in class Drawablepublic void setColorFilter(@Nullable ColorFilter cf)
setColorFilter in class Drawablepublic int getOpacity()
getOpacity in class DrawablePixelFormat.TRANSPARENT or PixelFormat.OPAQUE
depending on current Paint and GifOptions.setInIsOpaque(boolean) used to construct this Drawablepublic void start()
start in interface Animatablestart in interface MediaController.MediaPlayerControlpublic void reset()
public void stop()
stop in interface Animatablepublic boolean isRunning()
isRunning in interface Animatable@Nullable public String getComment()
public int getLoopCount()
public void setLoopCount(int loopCount)
<0 ,65535>loopCount - loop count, 0 means infinitypublic String toString()
public int getNumberOfFrames()
@NonNull public GifError getError()
GifError.NO_ERROR if there was no error or drawable is recycled@Nullable public static GifDrawable createFromResource(@NonNull Resources res, @RawRes @DrawableRes int resourceId)
GifDrawable(Resources, int) wrapper but returns null
instead of throwing exception if creation fails.res - resources to read fromresourceId - resource idpublic void setSpeed(float factor)
draw(Canvas)) was already called)
then effects will be visible starting from the next frame. Duration of the currently rendered
frame is not affected.factor - new speed factor, eg. 0.5f means half speed, 1.0f - normal, 2.0f - double speedIllegalArgumentException - if factor<=0public void pause()
stop()pause in interface MediaController.MediaPlayerControlpublic int getDuration()
getNumberOfFrames() to determine if there is one or more frames.getDuration in interface MediaController.MediaPlayerControlpublic int getCurrentPosition()
getCurrentPosition in interface MediaController.MediaPlayerControlpublic void seekTo(int position)
position is greater than duration of the loop of animation (or whole animation if there is no loop)
then animation will be sought to the end, no exception will be thrown.seekTo in interface MediaController.MediaPlayerControlposition - position to seek to in millisecondsIllegalArgumentException - if position<0public void seekToFrame(int frameIndex)
seekTo(int) but uses index of the frame instead of time.
If frameIndex exceeds number of frames, seek stops at the end, no exception is thrown.frameIndex - index of the frame to seek to (zero based)IllegalArgumentException - if frameIndex<0public Bitmap seekToFrameAndGet(int frameIndex)
seekToFrame(int) but performs operation synchronously and returns that frame.frameIndex - index of the frame to seek to (zero based)IndexOutOfBoundsException - if frameIndex<0public Bitmap seekToPositionAndGet(int position)
seekTo(int) but performs operation synchronously and returns that frame.position - position to seek to in millisecondsIndexOutOfBoundsException - if position<0public boolean isPlaying()
isRunning()isPlaying in interface MediaController.MediaPlayerControlpublic int getBufferPercentage()
getBufferPercentage in interface MediaController.MediaPlayerControlpublic boolean canPause()
canPause in interface MediaController.MediaPlayerControlpublic boolean canSeekBackward()
canSeekBackward in interface MediaController.MediaPlayerControlpublic boolean canSeekForward()
canSeekForward in interface MediaController.MediaPlayerControlpublic int getAudioSessionId()
getAudioSessionId in interface MediaController.MediaPlayerControlpublic int getFrameByteCount()
This method should not be used to calculate the memory usage of the bitmap.
Instead see getAllocationByteCount().
public long getAllocationByteCount()
public long getMetadataAllocationByteCount()
public long getInputSourceByteCount()
InputStream -1 is always returned.
In case of byte array and ByteBuffer length is always known.
In other cases length -1 can be returned if length cannot be determined.public void getPixels(@NonNull int[] pixels)
Color.pixels - the array to receive the frame's colorsArrayIndexOutOfBoundsException - if the pixels array is too small to receive required number of pixelspublic int getPixel(int x,
int y)
Color at the specified location. Throws an exception
if x or y are out of bounds (negative or >= to the width or height
respectively). The returned color is a non-premultiplied ARGB value.x - The x coordinate (0...width-1) of the pixel to returny - The y coordinate (0...height-1) of the pixel to returnColor at the specified coordinateIllegalArgumentException - if x, y exceed the drawable's boundsIllegalStateException - if drawable is recycledprotected void onBoundsChange(Rect bounds)
onBoundsChange in class Drawablepublic void draw(@NonNull Canvas canvas)
public void setFilterBitmap(boolean filter)
setFilterBitmap in class Drawable@Deprecated public void setDither(boolean dither)
public void addAnimationListener(@NonNull AnimationListener listener)
listener - animation listener to be added, not nullNullPointerException - if listener is nullpublic boolean removeAnimationListener(AnimationListener listener)
listener - animation listener to be removedpublic ColorFilter getColorFilter()
getColorFilter in class Drawablepublic Bitmap getCurrentFrame()
public void setTintList(ColorStateList tint)
setTintList in class Drawablepublic void setTintMode(@NonNull PorterDuff.Mode tintMode)
setTintMode in class Drawableprotected boolean onStateChange(int[] stateSet)
onStateChange in class Drawablepublic boolean isStateful()
isStateful in class Drawablepublic boolean setVisible(boolean visible,
boolean restart)
GifDrawableBuilder.setRenderingTriggeredOnDraw(boolean) was used with true)
when the drawable becomes invisible, it will pause its animation. A
subsequent change to visible with restart set to true will
restart the animation from the first frame. If restart is
false, the animation will resume from the most recent frame.setVisible in class Drawablevisible - true if visible, false otherwiserestart - when visible and rendering is triggered on draw, true to force the animation to restart
from the first framepublic int getCurrentFrameIndex()
public int getCurrentLoop()
public boolean isAnimationCompleted()
public int getFrameDuration(int index)
index - index of the frameIndexOutOfBoundsException - if index < 0 or index >= number of framespublic void setCornerRadius(float cornerRadius)
Paint shader by BitmapShader.
Transform set by setTransform(Transform) will also be replaced.cornerRadius - corner radius or 0 to remove roundingpublic float getCornerRadius()
public void setTransform(@Nullable Transform transform)
Transform implementation to customize how the GIF's current Bitmap is drawn.transform - new Transform or null to remove current one