android-gif-drawable

repository·dev·Indexed 27 days ago

https://github.com/koral--/android-gif-drawable

An Android library providing efficient Views and Drawables for rendering animated GIFs using a bundled GIFLib via JNI. It offers specialized components like GifImageView, GifImageButton, and GifTextView, and provides a GifDrawable class that implements Animatable and MediaPlayerControl for playback management. Supports Android 4.2+ (API level 17+).

Tokens
2.5K
Snippets
9
Records
14
Agent score
44%

What's inside android-gif-drawable

  1. Requirements for android-gif-drawable

    dev

    System Requirements

    • Android: 4.2+ (API level 17+)
    • Hardware Acceleration: Required for GifTextureView.
    • OpenGL ES 2.0+: Required for GifTexImage2D.

    Build Requirements

    • Android NDK: Required if building from source to compile native components.
  2. Associate a single GifDrawable with multiple Views

    dev

    By default, a single GifDrawable instance will only animate on the last View it is associated with. To animate it across multiple Views, use a MultiCallback:

    1. Create a MultiCallback instance.
    2. Add all target Views to the callback using addView().
    3. Set the callback on the GifDrawable using setCallback().
    MultiCallback multiCallback = new MultiCallback();
    
    imageView.setImageDrawable(gifDrawable);
    multiCallback.addView(imageView);
    
    anotherImageView.setImageDrawable(gifDrawable);
    multiCallback.addView(anotherImageView);
    
    gifDrawable.setCallback(multiCallback);
  3. Upgrade Proguard configuration for 1.0.x migrations

    dev

    When upgrading from version 1.0.x, you must update your Proguard configuration to include the following rules to prevent class shrinking or obfuscation issues:

    -keep public class pl.droidsonroids.gif.GifIOException{<init>(int);}
    -keep class pl.droidsonroids.gif.GifInfoHandle{<init>(long,int,int,int);}
  4. Install android-gif-drawable snapshot builds

    dev

    To use current development builds from the dev branch, add the OSS snapshot repository to your repositories block and use a version range.

    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
    dependencies {
        implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.+'
    }
  5. Install android-gif-drawable via Maven

    dev

    Use the following dependency configuration for Maven projects.

    <dependency>
        <groupId>pl.droidsonroids.gif</groupId>
        <artifactId>android-gif-drawable</artifactId>
        <version>insert latest version here</version>
        <type>aar</type>
    </dependency>
  6. Handle changes in Drawable recycling behavior

    dev
    In versions following 1.0.x, GifDrawable uses android.graphics.Bitmap as a frame buffer. If you attempt to access pixels or draw a recycled GifDrawable, the application will throw an IllegalStateException, matching the standard behavior of Bitmap.
  7. Use GifImageView and GifImageButton in XML

    dev

    The simplest way to display GIFs is using GifImageView or GifImageButton. If the android:src or android:background attributes point to a GIF, they will be automatically recognized and animated.

    <pl.droidsonroids.gif.GifImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/src_anim"
        android:background="@drawable/bg_anim"
        />
  8. Use MediaController to control GifDrawable

    dev

    Since GifDrawable implements MediaPlayerControl, you can use a standard Android MediaController to show progress and provide playback controls.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GifImageButton gib = new GifImageButton(this);
        setContentView(gib);
        gib.setImageResource(R.drawable.sample);
        final MediaController mc = new MediaController(this);
        mc.setMediaPlayer((GifDrawable) gib.getDrawable());
        mc.setAnchorView(gib);
        gib.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mc.show();
            }
       });
    }
  9. Use GifTextView for compound drawables and backgrounds

    dev

    GifTextView allows you to use GIFs as compound drawables (e.g., drawableTop, drawableStart) and as a background.

    <pl.droidsonroids.gif.GifTextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawableTop="@drawable/left_anim"
        android:drawableStart="@drawable/left_anim"
        android:background="@drawable/bg_anim"
        />
  10. Retrieve GifDrawable metadata

    dev

    Use the following methods to inspect the GIF content:

    • getLoopCount(): Returns the loop count defined in the NETSCAPE 2.0 extension.
    • getNumberOfFrames(): Returns the number of frames (at least 1).
    • getComment(): Returns the comment text (null if none).
    • getFrameByteCount(): Returns the minimum bytes needed to store a single frame's pixels.
    • getAllocationByteCount(): Returns the size in bytes of the allocated memory for pixels.
    • getInputSourceByteCount(): Returns the length in bytes of the backing input data.
    • toString(): Returns human-readable info (size and frame count) for debugging.
  11. Control GifDrawable animation

    dev

    GifDrawable implements Animatable and MediaPlayerControl. Use these methods to manage playback:

    • stop(): Stops the animation (thread-safe).
    • start(): Starts the animation (thread-safe).
    • isRunning(): Returns whether the animation is currently running.
    • reset(): Rewinds the animation without restarting it.
    • setSpeed(float factor): Sets the animation speed (e.g., 2.0f doubles the speed).
    • seekTo(int position): Seeks to a specific position in milliseconds within the current loop.
    • getDuration(): Returns the duration of one loop in milliseconds.
    • getCurrentPosition(): Returns the elapsed time from the beginning of the current loop.