Mobile Development 13 min read

Understanding Apng and Implementing Animation Playback on Android

APNG, a high‑quality animated PNG format offering 24‑bit color and 8‑bit alpha transparency, outperforms GIF in visual fidelity and file size, and on Android can be parsed and rendered via MappedByteBuffer and SurfaceView using background threads to load frames from memory, eliminating I/O lag and UI blocking.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding Apng and Implementing Animation Playback on Android

Apng (Animated Portable Network Graphics) is an animation format extended from PNG format, adding support for animated images with 24-bit color depth and 8-bit alpha transparency, while maintaining backward compatibility with PNG.

Apng vs GIF Comparison:

1. Image Quality: GIF supports only 8-bit color depth with noticeable jagged edges, while Apng supports 24-bit color depth with better anti-aliasing effects.

2. Transparency: GIF supports only full transparency or opacity without gradient transparency, while Apng supports 8-bit alpha transparency channel.

3. File Size: GIF uses LZW compression algorithm, while Apng uses Deflate compression algorithm. Under the same color depth conditions, Apng files are smaller.

4. Application Scenarios: GIF has high compatibility with nearly all browsers. Apng is supported by Chrome, Firefox, Safari, and Opera. GIF is suitable for low-quality animation scenarios (like dynamic emojis), while Apng is better for high-quality animations (like live streaming gift effects). Apng is also preferred for animations requiring transparency gradients (day-to-night transitions) and scenes with minimal pixel changes.

Android Apng Animation Implementation:

Apng file structure includes IHDR (Image Header), acTL (Animation Control Chunk), fcTL (Frame Control Chunk), and fdAT (Frame Data Chunk). The implementation involves two main steps: Apng parsing and Apng rendering.

Apng parsing can be done through two approaches: 1) Decompress Apng to PNG sequence frames using ApngExact, then load them; 2) Use ApngReader to directly read specific frames from the Apng file.

Apng rendering includes three steps: Dispose (specifies operations on buffer before drawing next frame), Blend (specifies operations on buffer before drawing current frame), and Draw (render Bitmap to canvas).

The implementation uses MappedByteBuffer to load Apng files, reads acTL control frames, then uses SurfaceView to read fdAT and fcTL data frames in child threads. After disposal and blending processing, frames are drawn on SurfaceView's Canvas.

Advantages of this solution: 1) Each frame is loaded from memory rather than disk, eliminating I/O operations and preventing animation stuttering; 2) Rendering is completed in child threads without blocking the UI main thread, avoiding ANR.

mobile developmentanimationAndroidPNGAPNGGraphics Programmingimage format
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.