Class GifAnimationMetaData

java.lang.Object
pl.droidsonroids.gif.GifAnimationMetaData
All Implemented Interfaces:
Parcelable, Serializable

public class GifAnimationMetaData
extends Object
implements Serializable, Parcelable
Lightweight version of GifDrawable used to retrieve metadata of GIF only, without having to allocate the memory for its pixels.
See Also:
Serialized Form
  • Field Details

  • Constructor Details

    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull Resources res, @RawRes @DrawableRes int id) throws Resources.NotFoundException, IOException
      Retrieves from resource.
      Parameters:
      res - Resources to read from
      id - resource id
      Throws:
      Resources.NotFoundException - if the given ID does not exist.
      IOException - when opening failed
      NullPointerException - if res is null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull AssetManager assets, @NonNull String assetName) throws IOException
      Retrieves metadata from asset.
      Parameters:
      assets - AssetManager to read from
      assetName - name of the asset
      Throws:
      IOException - when opening failed
      NullPointerException - if assets or assetName is null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull String filePath) throws IOException
      Constructs metadata from given file path.
      Only metadata is read, no graphic data is decoded here. In practice can be called from main thread. However it will violate StrictMode policy if disk reads detection is enabled.
      Parameters:
      filePath - path to the GIF file
      Throws:
      IOException - when opening failed
      NullPointerException - if filePath is null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull File file) throws IOException
      Equivalent to GifMetadata(file.getPath())}
      Parameters:
      file - the GIF file
      Throws:
      IOException - when opening failed
      NullPointerException - if file is null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull InputStream stream) throws IOException
      Retrieves metadata from InputStream. InputStream must support marking, IllegalArgumentException will be thrown otherwise.
      Parameters:
      stream - stream to read from
      Throws:
      IOException - when opening failed
      IllegalArgumentException - if stream does not support marking
      NullPointerException - if stream is null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull AssetFileDescriptor afd) throws IOException
      Retrieves metadata from AssetFileDescriptor. Convenience wrapper for GifAnimationMetaData(FileDescriptor)
      Parameters:
      afd - source
      Throws:
      NullPointerException - if afd is null
      IOException - when opening failed
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull FileDescriptor fd) throws IOException
      Retrieves metadata from FileDescriptor
      Parameters:
      fd - source
      Throws:
      IOException - when opening failed
      NullPointerException - if fd is null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull byte[] bytes) throws IOException
      Retrieves metadata from byte array.
      It can be larger than size of the GIF data. Bytes beyond GIF terminator are not accessed.
      Parameters:
      bytes - raw GIF bytes
      Throws:
      IOException - if bytes does not contain valid GIF data
      NullPointerException - if bytes are null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@NonNull ByteBuffer buffer) throws IOException
      Retrieves metadata from ByteBuffer. Only direct buffers are supported. Buffer can be larger than size of the GIF data. Bytes beyond GIF terminator are not accessed.
      Parameters:
      buffer - buffer containing GIF data
      Throws:
      IOException - if buffer does not contain valid GIF data or is indirect
      NullPointerException - if buffer is null
    • GifAnimationMetaData

      public GifAnimationMetaData​(@Nullable ContentResolver resolver, @NonNull Uri uri) throws IOException
      Retrieves metadata from Uri which is resolved using resolver. ContentResolver.openAssetFileDescriptor(android.net.Uri, String) is used to open an Uri.
      Parameters:
      uri - GIF Uri, cannot be null.
      resolver - resolver, null is allowed for file:// scheme Uris only
      Throws:
      IOException - if resolution fails or destination is not a GIF.
  • Method Details

    • getWidth

      public int getWidth()
      Returns:
      width od the GIF canvas in pixels
    • getHeight

      public int getHeight()
      Returns:
      height od the GIF canvas in pixels
    • getNumberOfFrames

      public int getNumberOfFrames()
      Returns:
      number of frames in GIF, at least one
    • getLoopCount

      public int getLoopCount()
      Returns:
      loop count, 0 means that animation is infinite
    • getDuration

      public int getDuration()
      Returns:
      duration of of one loop the animation in milliseconds. Result is always multiple of 10.
    • isAnimated

      public boolean isAnimated()
      Returns:
      true if GIF is animated (has at least 2 frames and positive duration), false otherwise
    • getAllocationByteCount

      public long getAllocationByteCount()
      Like GifDrawable.getAllocationByteCount() but does not include memory needed for backing Bitmap. Bitmap in GifDrawable may be allocated at the time of creation or existing one may be reused if GifDrawableInit.with(GifDrawable) is used. This method assumes no subsampling (sample size = 1).
      To calculate allocation byte count of GifDrawable created from the same input source getDrawableAllocationByteCount(GifDrawable, int) can be used.
      Returns:
      possible size of the memory needed to store pixels excluding backing Bitmap and assuming no subsampling
    • getDrawableAllocationByteCount

      @Beta public long getDrawableAllocationByteCount​(@Nullable GifDrawable oldDrawable, @IntRange(from=1L,to=65535L) int sampleSize)
      Like getAllocationByteCount() but includes also backing Bitmap and takes sample size into account.
      Parameters:
      oldDrawable - optional old drawable to be reused, pass null if there is no one
      sampleSize - sample size, pass 1 if not using subsampling
      Returns:
      possible size of the memory needed to store pixels
      Throws:
      IllegalArgumentException - if sample size out of range
    • getMetadataAllocationByteCount

      public long getMetadataAllocationByteCount()
      Returns:
      maximum possible size of the allocated memory needed to store metadata
    • toString

      @NonNull public String toString()
      Overrides:
      toString in class Object
    • describeContents

      public int describeContents()
      Specified by:
      describeContents in interface Parcelable
    • writeToParcel

      public void writeToParcel​(Parcel dest, int flags)
      Specified by:
      writeToParcel in interface Parcelable