APNG4Android

repository·master·Indexed 20 days ago

https://github.com/penfeizhou/apng4android

An efficient Android decoder library providing support for APNG, Animated WebP, GIF, and AVIF formats. It supports direct Drawable usage via AssetStreamLoader, ResourceStreamLoader, and FileStreamLoader, as well as seamless integration with the Glide library through a dedicated glide-plugin.

Tokens
1.9K
Snippets
5
Records
8
Agent score
69%

What's inside APNG4Android

  1. Use the Glide plugin to load animations

    master

    The library provides a Glide plugin that allows you to load animated formats (like APNG, WebP, etc.) using standard Glide syntax.

    1. Add the glide-plugin dependency to your build.gradle.
    2. Use Glide.with(...).load(...).into(...) as you normally would.
    // Add the plugin dependency
    dependencies {
        implementation 'com.github.penfeizhou.android.animation:glide-plugin:${VERSION}'
    }
    // Use Glide to load an animated URL or file
    Glide.with(imageView)
         .load("https://misc.aotu.io/ONE-SUNDAY/SteamEngine.png")
         .into(imageView);
  2. Integrate APNG4Android with Glide

    master

    To support APNG, WebP, Gif, or AVIF automatically within your Glide image loading pipeline, add the glide-plugin dependency. This allows you to load animated URLs or files directly into an ImageView using standard Glide syntax.

    ```gradle
    // Add to build.gradle
    repositories {
        mavenCentral()
    }
    
    dependencies {
        implementation 'com.github.penfeizhou.android.animation:glide-plugin:${VERSION}'
    }

    // Usage in code Glide.with(imageView).load("https://misc.aotu.io/ONE-SUNDAY/SteamEngine.png").into(imageView); Glide.with(imageView).load("https://isparta.github.io/compare-webp/image/gif_webp/webp/2.webp").into(imageView);

  3. Use APNG4Android for direct Drawable usage

    master

    You can manually load animation files using specific StreamLoader implementations and wrap them in the corresponding Drawable class. Supported loaders include AssetStreamLoader (for assets), ResourceStreamLoader (for resources), and FileStreamLoader (for local files).

    // 1. Choose a loader based on your source
    AssetStreamLoader assetLoader = new AssetStreamLoader(context, "wheel.png");
    ResourceStreamLoader resourceLoader = new ResourceStreamLoader(context, R.drawable.sample);
    FileStreamLoader fileLoader = new FileStreamLoader("/sdcard/Pictures/1.webp");
    
    // 2. Create the specific Drawable
    APNGDrawable apngDrawable = new APNGDrawable(assetLoader);
    WebPDrawable webpDrawable = new WebPDrawable(assetLoader);
    AVIFDrawable avifDrawable = new AVIFDrawable(assetLoader);
    
    // 3. Apply to an ImageView
    imageView.setImageDrawable(apngDrawable);
    
    // 4. Control playback (optional)
    apngDrawable.setLoopLimit(10);
    
    // 5. Listen to animation events
    drawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
        @Override
        public void onAnimationStart(Drawable drawable) {
            super.onAnimationStart(drawable);
        }
    });
  4. Load and play animations directly

    master

    You can load animations from Assets, Resources, or Files using specific loaders, then wrap them in the appropriate Drawable class (e.g., APNGDrawable or WebPDrawable).

    Supported loaders:

    • AssetStreamLoader: For files in the assets folder.
    • ResourceStreamLoader: For files in the raw resource folder.
    • FileStreamLoader: For files located on the file system.

    You can control playback by setting a loop limit via setLoopLimit(int) and listen to animation events by implementing Animatable2Compat.AnimationCallback.

    // 1. Choose a loader
    AssetStreamLoader assetLoader = new AssetStreamLoader(context, "wheel.png");
    
    // 2. Create the specific Drawable
    APNGDrawable apngDrawable = new APNGDrawable(assetLoader);
    
    // 3. Display in an ImageView
    imageView.setImageDrawable(apngDrawable);
    
    // 4. Control playback
    apngDrawable.setLoopLimit(10);
    
    // 5. Listen to callbacks
    drawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
        @Override
        public void onAnimationStart(Drawable drawable) {
            super.onAnimationStart(drawable);
        }
    });
  5. Add APNG4Android dependencies to build.gradle

    master

    To use the library, first ensure mavenCentral() is included in your repositories block. Then, add the specific dependency for the animation format you need. Replace ${VERSION} with the desired version number.

    Available modules:

    • Animated WebP: com.github.penfeizhou.android.animation:awebp
    • APNG: com.github.penfeizhou.android.animation:apng
    • Gif: com.github.penfeizhou.android.animation:gif
    • AVIF: com.github.penfeizhou.android.animation:avif
    repositories {
        mavenCentral()
    }
    
    // Example for APNG
    dependencies {
        implementation 'com.github.penfeizhou.android.animation:apng:${VERSION}'
    }
  6. Avoid putting APNG resources in drawable or mipmap directories

    master
    Do not place APNG files in the drawable or mipmap directories. During the Android app release build process, the aapt tool may zip and modify the frame information of APNG files, causing abnormal playback behavior. Instead, place APNG resources in the raw or assets folders.
  7. Avoid placing APNG resources in drawable or mipmap directories

    master

    When using APNG, do not place your resources in the drawable or mipmap directories. During the Android app release build process, the aapt tool may compress or modify the frame information of APNG resources, which will cause playback issues.

    Correct approach: Place APNG resources in the raw or assets directories instead.

  8. Configure Glide animation decoding with AnimationDecoderOption

    master
    When using the Glide plugin to transcode animated formats (APNG, WebP, GIF, AVIF) into Drawables, you can pass options to control the behavior of the resulting Drawable. Specifically, you can use AnimationDecoderOption.NO_ANIMATION_BOUNDS_MEASURE to prevent the animation from performing bounds measurement, which can be useful for performance or layout stability.