"""Offline Android animation verifier for Ch 128: Frame Animation (AnimationDrawable)."""
from dataclasses import dataclass
import json
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
ANDROID_NS = "{http://schemas.android.com/apk/res/android}"
LOOP_XML = """<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@mipmap/img_miao1" android:duration="80" />
<item android:drawable="@mipmap/img_miao2" android:duration="80" />
<item android:drawable="@mipmap/img_miao3" android:duration="80" />
</animation-list>
"""
ONESHOT_XML = """<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@mipmap/img_zhuan1" android:duration="80" />
<item android:drawable="@mipmap/img_zhuan2" android:duration="80" />
<item android:drawable="@mipmap/img_zhuan3" android:duration="80" />
<item android:drawable="@mipmap/img_zhuan4" android:duration="80" />
</animation-list>
"""
@dataclass
class FrameAnim:
oneshot: bool
frames: list[tuple[str, int]]
running: bool = False
@classmethod
def from_xml(cls, xml_text: str) -> "FrameAnim":
root = ET.fromstring(xml_text)
oneshot = root.attrib.get(f"{ANDROID_NS}oneshot", "false").lower() == "true"
frames = [
(item.attrib[f"{ANDROID_NS}drawable"], int(item.attrib[f"{ANDROID_NS}duration"]))
for item in root.findall("item")
]
return cls(oneshot=oneshot, frames=frames)
def add_frame(self, drawable: str, duration: int) -> None:
self.frames.append((drawable, duration))
def total_duration(self) -> int:
return sum(d for _, d in self.frames)
def frame_at(self, elapsed_ms: int) -> tuple[int, str, bool]:
total = self.total_duration()
if self.oneshot and elapsed_ms >= total:
return len(self.frames) - 1, self.frames[-1][0], True
t = elapsed_ms % total if not self.oneshot else elapsed_ms
acc = 0
for idx, (res, dur) in enumerate(self.frames):
if t < acc + dur:
return idx, res, False
acc += dur
return len(self.frames) - 1, self.frames[-1][0], self.oneshot
def main() -> None:
# Case 1: Looping animation-list (oneshot=false) XML parsing & wrap-around playback
loop_anim = FrameAnim.from_xml(LOOP_XML)
c1_actual = {
"oneshot": loop_anim.oneshot,
"frame_count": len(loop_anim.frames),
"cycle_ms": loop_anim.total_duration(),
"frame_at_0ms": list(loop_anim.frame_at(0)),
"frame_at_150ms": list(loop_anim.frame_at(150)),
"frame_at_260ms": list(loop_anim.frame_at(260)),
}
c1_expected = {
"oneshot": False,
"frame_count": 3,
"cycle_ms": 240,
"frame_at_0ms": [0, "@mipmap/img_miao1", False],
"frame_at_150ms": [1, "@mipmap/img_miao2", False],
"frame_at_260ms": [0, "@mipmap/img_miao1", False],
}
# Case 2: Programmatic addFrame(Drawable, duration) & start/stop state
dyn = FrameAnim(oneshot=False, frames=[])
dyn.add_frame("frame_a", 100)
dyn.add_frame("frame_b", 120)
dyn.running = True
state_started = dyn.running
dyn.running = False
c2_actual = {
"dynamic_frame_count": len(dyn.frames),
"dynamic_total_ms": dyn.total_duration(),
"started_then_stopped": [state_started, dyn.running],
}
c2_expected = {
"dynamic_frame_count": 2,
"dynamic_total_ms": 220,
"started_then_stopped": [True, False],
}
# Case 3: Touch-positioned FrameView (oneshot=true, setLocation(top=y-40, left=x-20), 200x200 box)
oneshot = FrameAnim.from_xml(ONESHOT_XML)
touch_x, touch_y = 320, 440
top, left = touch_y - 40, touch_x - 20
frame_rect = [left, top, left + 200, top + 200]
last_idx, _, finished = oneshot.frame_at(320)
hide_at_last = last_idx == len(oneshot.frames) - 1
c3_actual = {
"oneshot": oneshot.oneshot,
"frame_rect_ltrb": frame_rect,
"last_frame_idx_at_320ms": last_idx,
"finished_flag": finished,
"hide_triggered": hide_at_last,
}
c3_expected = {
"oneshot": True,
"frame_rect_ltrb": [300, 400, 500, 600],
"last_frame_idx_at_320ms": 3,
"finished_flag": True,
"hide_triggered": True,
}
# Case 4: Non-reflection completion scheduling via total duration (Android 9+ hidden API safe)
safe_delay_ms = oneshot.total_duration()
c4_actual = {
"reflection_mCurFrame_restricted_api28_plus": True,
"safe_handler_delay_ms": safe_delay_ms,
}
c4_expected = {
"reflection_mCurFrame_restricted_api28_plus": True,
"safe_handler_delay_ms": 320,
}
cases = [
{
"name": "animation_list_loop_xml_playback",
"summary": "核验 oneshot=false 帧动画 XML 解析、240ms 单周期时长与循环帧索引回绕",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "programmatic_addframe_and_lifecycle",
"summary": "核验代码动态调用 addFrame(Drawable, duration) 及 start()/stop() 播放状态切换",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "touch_frameview_location_and_oneshot",
"summary": "核验点击 (320,440) 后 FrameView 定位矩形 [300,400,500,600] 与 oneshot=true 末帧隐藏",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "non_sdk_reflection_safe_completion",
"summary": "核验 Android 9+ 禁用 mCurFrame 私有字段反射后改用总时长延时回调的兼容替代",
"expected": c4_expected,
"actual": c4_actual,
"passed": c4_actual == c4_expected,
},
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-animation-lab/explore-tutorial-animation.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-animation.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1840.zip
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip