public class GifDrawable
extends android.graphics.drawable.Drawable
implements android.graphics.drawable.Animatable, android.widget.MediaController.MediaPlayerControl
Drawable
which can be used to hold GIF images, especially animations.
Basic GIF metadata can be also obtained.Modifier and Type | Field and Description |
---|---|
protected android.graphics.Paint |
mPaint
Paint used to draw on a Canvas
|
Constructor and Description |
---|
GifDrawable(android.content.res.AssetFileDescriptor afd)
Creates drawable from AssetFileDescriptor.
|
GifDrawable(android.content.res.AssetManager assets,
java.lang.String assetName)
Creates drawable from asset.
|
GifDrawable(byte[] bytes)
Creates drawable from byte array.
|
GifDrawable(java.nio.ByteBuffer buffer)
Creates drawable from
ByteBuffer . |
GifDrawable(android.content.ContentResolver resolver,
android.net.Uri uri)
Creates drawable from
Uri which is resolved using resolver . |
GifDrawable(java.io.File file)
Equivalent to GifDrawable(file.getPath())}
|
GifDrawable(java.io.FileDescriptor fd)
Creates drawable from FileDescriptor
|
GifDrawable(java.io.InputStream stream)
Creates drawable from InputStream.
|
GifDrawable(android.content.res.Resources res,
int id)
Creates drawable from resource.
|
GifDrawable(java.lang.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(android.content.res.Resources res,
int resourceId)
An
GifDrawable(Resources, int) wrapper but returns null
instead of throwing exception if creation fails. |
void |
draw(android.graphics.Canvas canvas)
Reads and renders new frame if needed then draws last rendered frame.
|
long |
getAllocationByteCount()
Returns size of the allocated memory used to store pixels of this object.
|
int |
getAlpha() |
int |
getAudioSessionId()
Used by MediaPlayer.
|
int |
getBufferPercentage()
Used by MediaPlayer for secondary progress bars.
|
android.graphics.ColorFilter |
getColorFilter() |
java.lang.String |
getComment()
Returns GIF comment
|
android.graphics.Bitmap |
getCurrentFrame()
Retrieves a copy of currently buffered frame.
|
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.
|
long |
getInputSourceByteCount()
Returns length of the input source obtained at the opening time or -1 if
length is unknown.
|
int |
getIntrinsicHeight() |
int |
getIntrinsicWidth() |
int |
getLoopCount()
Returns loop count previously read from GIF's application extension block.
|
int |
getMinimumHeight() |
int |
getMinimumWidth() |
int |
getNumberOfFrames() |
int |
getOpacity()
See
Drawable.getOpacity() |
android.graphics.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.
|
boolean |
isPlaying()
Equivalent of
isRunning() |
boolean |
isRecycled() |
boolean |
isRunning() |
boolean |
isStateful() |
protected void |
onBoundsChange(android.graphics.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
GifInfoHandle.seekToTime(long, int, android.graphics.Bitmap)
but uses index of the frame instead of time. |
void |
setAlpha(int alpha) |
void |
setColorFilter(android.graphics.ColorFilter cf) |
void |
setDither(boolean dither) |
void |
setFilterBitmap(boolean filter) |
void |
setSpeed(float factor)
Sets new animation speed factor.
|
void |
setTintList(android.content.res.ColorStateList tint) |
void |
setTintMode(android.graphics.PorterDuff.Mode tintMode) |
boolean |
setVisible(boolean visible,
boolean restart)
Sets whether this drawable is visible.
|
void |
start()
Starts the animation.
|
void |
stop()
Stops the animation.
|
java.lang.String |
toString() |
applyTheme, canApplyTheme, clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getChangingConfigurations, getConstantState, getCurrent, getDirtyBounds, getLevel, getOutline, getPadding, getState, getTransparentRegion, inflate, inflate, invalidateSelf, isAutoMirrored, isVisible, jumpToCurrentState, mutate, onLevelChange, resolveOpacity, scheduleSelf, setAutoMirrored, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setHotspot, setHotspotBounds, setLevel, setState, setTint, unscheduleSelf
public GifDrawable(android.content.res.Resources res, int id) throws android.content.res.Resources.NotFoundException, java.io.IOException
res
- Resources to read fromid
- resource id (raw or drawable)android.content.res.Resources.NotFoundException
- if the given ID does not exist.java.io.IOException
- when opening failedjava.lang.NullPointerException
- if res is nullpublic GifDrawable(android.content.res.AssetManager assets, java.lang.String assetName) throws java.io.IOException
assets
- AssetManager to read fromassetName
- name of the assetjava.io.IOException
- when opening failedjava.lang.NullPointerException
- if assets or assetName is nullpublic GifDrawable(java.lang.String filePath) throws java.io.IOException
StrictMode
policy if disk reads detection is enabled.filePath
- path to the GIF filejava.io.IOException
- when opening failedjava.lang.NullPointerException
- if filePath is nullpublic GifDrawable(java.io.File file) throws java.io.IOException
file
- the GIF filejava.io.IOException
- when opening failedjava.lang.NullPointerException
- if file is nullpublic GifDrawable(java.io.InputStream stream) throws java.io.IOException
stream
- stream to read fromjava.io.IOException
- when opening failedjava.lang.IllegalArgumentException
- if stream does not support markingjava.lang.NullPointerException
- if stream is nullpublic GifDrawable(android.content.res.AssetFileDescriptor afd) throws java.io.IOException
GifDrawable(FileDescriptor)
afd
- sourcejava.lang.NullPointerException
- if afd is nulljava.io.IOException
- when opening failedpublic GifDrawable(java.io.FileDescriptor fd) throws java.io.IOException
fd
- sourcejava.io.IOException
- when opening failedjava.lang.NullPointerException
- if fd is nullpublic GifDrawable(byte[] bytes) throws java.io.IOException
bytes
- raw GIF bytesjava.io.IOException
- if bytes does not contain valid GIF datajava.lang.NullPointerException
- if bytes are nullpublic GifDrawable(java.nio.ByteBuffer buffer) throws java.io.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 datajava.io.IOException
- if buffer does not contain valid GIF datajava.lang.IllegalArgumentException
- if buffer is indirectjava.lang.NullPointerException
- if buffer is nullpublic GifDrawable(android.content.ContentResolver resolver, android.net.Uri uri) throws java.io.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
, cannot be nulljava.io.IOException
- 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 android.graphics.drawable.Drawable
public int getIntrinsicWidth()
getIntrinsicWidth
in class android.graphics.drawable.Drawable
public void setAlpha(int alpha)
setAlpha
in class android.graphics.drawable.Drawable
public void setColorFilter(android.graphics.ColorFilter cf)
setColorFilter
in class android.graphics.drawable.Drawable
public int getOpacity()
Drawable.getOpacity()
getOpacity
in class android.graphics.drawable.Drawable
PixelFormat.TRANSPARENT
public void start()
start
in interface android.graphics.drawable.Animatable
start
in interface android.widget.MediaController.MediaPlayerControl
public void reset()
public void stop()
stop
in interface android.graphics.drawable.Animatable
public boolean isRunning()
isRunning
in interface android.graphics.drawable.Animatable
public java.lang.String getComment()
public int getLoopCount()
public java.lang.String toString()
toString
in class java.lang.Object
public int getNumberOfFrames()
public GifError getError()
GifError.NO_ERROR
if there was no error or drawable is recycledpublic static GifDrawable createFromResource(android.content.res.Resources res, 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 speedjava.lang.IllegalArgumentException
- if factor<=0public void pause()
stop()
pause
in interface android.widget.MediaController.MediaPlayerControl
public int getDuration()
getNumberOfFrames()
to determine if there is one or more frames.getDuration
in interface android.widget.MediaController.MediaPlayerControl
public int getCurrentPosition()
getCurrentPosition
in interface android.widget.MediaController.MediaPlayerControl
public void seekTo(int position)
seekTo
in interface android.widget.MediaController.MediaPlayerControl
position
- position to seek to in millisecondsjava.lang.IllegalArgumentException
- if position<0public void seekToFrame(int frameIndex)
GifInfoHandle.seekToTime(long, int, android.graphics.Bitmap)
but uses index of the frame instead of time.frameIndex
- index of the frame to seek to (zero based)java.lang.IllegalArgumentException
- if frameIndex<0public boolean isPlaying()
isRunning()
isPlaying
in interface android.widget.MediaController.MediaPlayerControl
public int getBufferPercentage()
getBufferPercentage
in interface android.widget.MediaController.MediaPlayerControl
public boolean canPause()
canPause
in interface android.widget.MediaController.MediaPlayerControl
public boolean canSeekBackward()
canSeekBackward
in interface android.widget.MediaController.MediaPlayerControl
public boolean canSeekForward()
canSeekForward
in interface android.widget.MediaController.MediaPlayerControl
public int getAudioSessionId()
getAudioSessionId
in interface android.widget.MediaController.MediaPlayerControl
public int getFrameByteCount()
This method should not be used to calculate the memory usage of the bitmap.
Instead see getAllocationByteCount()
.
public long getAllocationByteCount()
public long getInputSourceByteCount()
InputStream
and FileDescriptor
-1 is always returned.
In case of File
, file path, byte array and ByteBuffer
length is always known.public void getPixels(int[] pixels)
Color
.pixels
- the array to receive the frame's colorsjava.lang.ArrayIndexOutOfBoundsException
- 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 coordinatejava.lang.IllegalArgumentException
- if x, y exceed the drawable's boundsjava.lang.IllegalStateException
- if drawable is recycledprotected void onBoundsChange(android.graphics.Rect bounds)
onBoundsChange
in class android.graphics.drawable.Drawable
public void draw(android.graphics.Canvas canvas)
draw
in class android.graphics.drawable.Drawable
canvas
- canvas to draw intopublic final android.graphics.Paint getPaint()
public int getAlpha()
getAlpha
in class android.graphics.drawable.Drawable
public void setFilterBitmap(boolean filter)
setFilterBitmap
in class android.graphics.drawable.Drawable
public void setDither(boolean dither)
setDither
in class android.graphics.drawable.Drawable
public int getMinimumHeight()
getMinimumHeight
in class android.graphics.drawable.Drawable
public int getMinimumWidth()
getMinimumWidth
in class android.graphics.drawable.Drawable
public void addAnimationListener(AnimationListener listener)
listener
- animation listener to be added, not nulljava.lang.NullPointerException
- if listener is nullpublic boolean removeAnimationListener(AnimationListener listener)
listener
- animation listener to be removedpublic android.graphics.ColorFilter getColorFilter()
getColorFilter
in class android.graphics.drawable.Drawable
public android.graphics.Bitmap getCurrentFrame()
public void setTintList(android.content.res.ColorStateList tint)
setTintList
in class android.graphics.drawable.Drawable
public void setTintMode(android.graphics.PorterDuff.Mode tintMode)
setTintMode
in class android.graphics.drawable.Drawable
protected boolean onStateChange(int[] stateSet)
onStateChange
in class android.graphics.drawable.Drawable
public boolean isStateful()
isStateful
in class android.graphics.drawable.Drawable
public boolean setVisible(boolean visible, boolean restart)
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 android.graphics.drawable.Drawable
visible
- true if visible, false otherwiserestart
- when visible, true to force the animation to restart
from the first frame