alibaba / alibaba/power_image

Android animated WebP、新版 Flutter 兼容与多图渲染优化

Open
#60 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
516
Forks
64
PR merge metrics
No merged PRs in 30d

Description

## 最终结论

Android animated WebP 与新版 Flutter 兼容修复已在 PR #59 完成。最终 fork 分支:,当前 HEAD 为 `f327b24`。本次仅修改 Android,iOS 未改。

## 原来为什么比 cached_network_image / extended_image 占内存

原生方案中,每张 animated WebP 都同时持有 Android decoder/Drawable 和一个独立 `SurfaceProducer` 图形缓冲队列;Surface 通常是多缓冲。20 张图就会累积 20 份 decoder/frame state 和 20 组图形缓冲。帧合并、丢帧和及时释放能防止任务堆积与退出后泄漏,但不能消除正在播放时的这部分固定成本。

## 最终 Android 实现

- 网络 animated WebP:Glide `downloadOnly()` 缓存压缩文件,经 `FlutterEncodedImage` 把路径交给 Flutter codec;不再为每张 WebP 创建原生 Drawable + SurfaceProducer,并保持各图片独立播放。
- GIF/自定义 animated Drawable:继续使用 SurfaceProducer,Drawable 直接画 Surface;API 23+ 优先 hardware canvas,失败回退 software canvas。
- 原生动图:VSync 合帧、固定 render pool 公平调度、每图最多一个待绘制任务、过期帧丢弃。
- 跟随 `TickerMode`、应用前后台和 Surface 生命周期暂停/恢复;取消 Glide Target 并确定性释放资源。
- density-aware 目标尺寸和 Flutter image cache 接入,避免纹理缓存与 Flutter 缓存重复持有。
- 增加结构化日志、20 个不同动物 fixture、唯一性/动画性测试和三库 Macrobenchmark。

## 最终模拟器实测

Android 16 模拟器,相同 20 个 512×512 animated WebP、相同 160×160 logical target 和滚动手势,每库 5 次:

| 方案 | heap max 中位数 | RSS anon max 中位数 | RSS file max 中位数 | UI frame count 中位数 | UI avg / max |
|---|---:|---:|---:|---:|---:|
| PowerImage | 18.098 MB | 95.262 MB | 116.863 MB | 300 | 0.964 / 14.054 ms |
| cached_network_image | 5.071 MB | 94.156 MB | 118.293 MB | 283 | 1.175 / 20.227 ms |
| extended_image | 4.853 MB | 92.734 MB | 122.168 MB | 282 | 1.173 / 16.884 ms |

WebP Flutter-codec 路径已把匿名 RSS 峰值降到与两库接近,文件 RSS 更低;但 heap max 仍高约 13 MB。因此不能声称 PowerImage 内存全面领先。以上为模拟器回归数据,不代表真实设备性能结论。

## 验证

- Flutter 3.38.7;package tests 55/55,example tests 3/3。
- 20 个 WebP 的编码内容、首帧和动画序列均唯一。
- Android unit tests + debug APK + benchmark APK:209 tasks successful。
- Android API 36 emulator Macrobenchmark:3/3 tests passed,三库各 5 次;308 tasks successful。
- 测试后模拟器 Clash 代理已恢复为 `10.0.2.2:7897`。

相关问题:#49 #51 #57

Contributor guide

No contributing guide indexed for this repository

Research direction

The issue records that the Android animated WebP and newer Flutter compatibility work was completed in PR #59 and the feature/android-animated-webp fork at commit f327b24. Review PR #59, the Android unit tests, package/example tests, and Macrobenchmark results first; no remaining implementation task is identified here.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, dart, flutter
Domain
mobile, performance
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
10/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.