"""Offline Android UI verifier for Ch 28: ProgressBar."""
import json
import xml.etree.ElementTree as ET
ANDROID_NS = "{http://schemas.android.com/apk/res/android}"
PROGRESSBAR_XML = """<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<ProgressBar style="@android:style/Widget.ProgressBar.Small" />
<ProgressBar />
<ProgressBar style="@android:style/Widget.ProgressBar.Large" />
<ProgressBar style="@android:style/Widget.ProgressBar.Horizontal" android:max="100" android:progress="18" />
<ProgressBar style="@android:style/Widget.ProgressBar.Horizontal" android:indeterminate="true" />
</LinearLayout>
"""
ANIM_LIST_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="@drawable/loading_01" android:duration="200"/>
<item android:drawable="@drawable/loading_02" android:duration="200"/>
<item android:drawable="@drawable/loading_03" android:duration="200"/>
<item android:drawable="@drawable/loading_04" android:duration="200"/>
<item android:drawable="@drawable/loading_05" android:duration="200"/>
<item android:drawable="@drawable/loading_06" android:duration="200"/>
<item android:drawable="@drawable/loading_07" android:duration="200"/>
<item android:drawable="@drawable/loading_08" android:duration="200"/>
<item android:drawable="@drawable/loading_09" android:duration="200"/>
<item android:drawable="@drawable/loading_10" android:duration="200"/>
<item android:drawable="@drawable/loading_11" android:duration="200"/>
<item android:drawable="@drawable/loading_12" android:duration="200"/>
</animation-list>
"""
class ProgressBarModel:
def __init__(self, max_val: int = 100, progress: int = 0, secondary: int = 0, indeterminate: bool = False):
self.max_val = max_val
self.progress = progress
self.secondary = secondary
self.indeterminate = indeterminate
def increment_progress_by(self, diff: int) -> int:
self.progress = max(0, min(self.max_val, self.progress + diff))
return self.progress
def main() -> None:
root = ET.fromstring(PROGRESSBAR_XML)
bars = list(root)
c1_actual = {
"widget_count": len(bars),
"horizontal_progress": int(bars[3].attrib[f"{ANDROID_NS}progress"]),
"horizontal_indeterminate": bars[4].attrib[f"{ANDROID_NS}indeterminate"] == "true",
"hierarchy": ["View", "ProgressBar", "AbsSeekBar", "SeekBar/RatingBar"],
}
c1_expected = {
"widget_count": 5,
"horizontal_progress": 18,
"horizontal_indeterminate": True,
"hierarchy": ["View", "ProgressBar", "AbsSeekBar", "SeekBar/RatingBar"],
}
# Case 2: ProgressBar clamping & secondary progress
pb = ProgressBarModel(max_val=100, progress=18, secondary=45)
p1 = pb.increment_progress_by(30) # 48
p2 = pb.increment_progress_by(80) # clamped to 100
p3 = pb.increment_progress_by(-150) # clamped to 0
pb.indeterminate = True
c2_actual = {
"after_plus_30": p1,
"after_overflow": p2,
"after_underflow": p3,
"secondary": pb.secondary,
"indeterminate": pb.indeterminate,
}
c2_expected = {
"after_plus_30": 48,
"after_overflow": 100,
"after_underflow": 0,
"secondary": 45,
"indeterminate": True,
}
# Case 3: AnimationDrawable 12-frame loop cycle
anim = ET.fromstring(ANIM_LIST_XML)
items = anim.findall("item")
durations = [int(it.attrib[f"{ANDROID_NS}duration"]) for it in items]
total_ms = sum(durations)
# Frame at t=500ms (frame 2 -> loading_03) and t=2650ms (wraps 2650 % 2400 = 250 -> frame 1 -> loading_02)
def frame_at(ms: int) -> str:
rem = ms % total_ms
acc = 0
for it, dur in zip(items, durations):
acc += dur
if rem < acc:
return it.attrib[f"{ANDROID_NS}drawable"]
return items[-1].attrib[f"{ANDROID_NS}drawable"]
c3_actual = {
"oneshot": anim.attrib[f"{ANDROID_NS}oneshot"] == "true",
"frame_count": len(items),
"cycle_ms": total_ms,
"at_500ms": frame_at(500),
"at_2650ms": frame_at(2650),
}
c3_expected = {
"oneshot": False,
"frame_count": 12,
"cycle_ms": 2400,
"at_500ms": "@drawable/loading_03",
"at_2650ms": "@drawable/loading_02",
}
# Case 4: Custom CirclePgBar onMeasure & drawArc sweep angle
radius, stroke, max_val, target = 200.0, 50.0, 100, 90
def real_size(mode: str, spec_size: int) -> int:
if mode in ("AT_MOST", "UNSPECIFIED"):
return int(radius * 2 + stroke)
return spec_size
w = real_size("AT_MOST", 600)
h = real_size("EXACTLY", 500)
view_size = int(radius * 2)
left = (w - view_size) // 2
top = (h - view_size) // 2
target_angle = round(target / float(max_val) * 360.0, 1)
c4_actual = {
"measured_wh": [w, h],
"arc_rect": [left, top, left + view_size, top + view_size],
"start_angle_deg": -90,
"target_sweep_deg": target_angle,
}
c4_expected = {
"measured_wh": [450, 500],
"arc_rect": [25, 50, 425, 450],
"start_angle_deg": -90,
"target_sweep_deg": 324.0,
}
cases = [
{
"name": "progressbar_hierarchy_and_xml_styles",
"summary": "解析 5 种系统 ProgressBar 样式并核对 View->ProgressBar->AbsSeekBar 类继承层级",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "progress_clamping_and_increment",
"summary": "验证 incrementProgressBy 上下界钳位、[0, max] 边界及不确定模式切换",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "animation_list_frame_cycle",
"summary": "解析 12 帧 AnimationDrawable animation-list XML 并核算循环周期与任意时刻帧索引",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "circle_pgbar_measure_and_arc_sweep",
"summary": "验证自定义圆形进度条 CirclePgBar 的 MeasureSpec 测量、圆弧外接矩形及 90% 对应 324° 扫掠角",
"expected": c4_expected,
"actual": c4_actual,
"passed": c4_actual == c4_expected,
},
]
print(json.dumps({
"version": "CPython 3.12.14 offline Android UI/XML/Adapter verifier (Android SDK/emulator absent)",
"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-progressbar-lab/explore-tutorial-progressbar.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-progressbar.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1928.zip