"""Offline deterministic verifier for Android CH19 (tutorial-framelayout)."""
from __future__ import annotations
import json
import re
import xml.etree.ElementTree as ET
ANDROID_NS = "{http://schemas.android.com/apk/res/android}"
def parse_dp(v: str) -> int:
return int(v.replace("dp", ""))
def case_z_order_and_foreground() -> dict:
xml_text = """<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/FrameLayout1" android:layout_width="match_parent" android:layout_height="match_parent"
android:foreground="@drawable/logo" android:foregroundGravity="right|bottom">
<TextView android:layout_width="200dp" android:layout_height="200dp" android:background="#FF6143" />
<TextView android:layout_width="150dp" android:layout_height="150dp" android:background="#7BFE00" />
<TextView android:layout_width="100dp" android:layout_height="100dp" android:background="#FFFF00" />
</FrameLayout>"""
root = ET.fromstring(xml_text)
layers = [
{
"w": parse_dp(c.attrib[f"{ANDROID_NS}layout_width"]),
"h": parse_dp(c.attrib[f"{ANDROID_NS}layout_height"]),
"bg": c.attrib[f"{ANDROID_NS}background"],
}
for c in root
]
def hit_test(x: int, y: int) -> str | None:
# Later children sit higher in z-order
for item in reversed(layers):
if 0 <= x < item["w"] and 0 <= y < item["h"]:
return item["bg"]
return None
actual = {
"wrap_bounds_dp": [max(l["w"] for l in layers), max(l["h"] for l in layers)],
"hit_50_50": hit_test(50, 50),
"hit_125_125": hit_test(125, 125),
"hit_175_175": hit_test(175, 175),
"foreground": root.attrib[f"{ANDROID_NS}foreground"],
"foreground_gravity": root.attrib[f"{ANDROID_NS}foregroundGravity"],
}
expected = {
"wrap_bounds_dp": [200, 200],
"hit_50_50": "#FFFF00",
"hit_125_125": "#7BFE00",
"hit_175_175": "#FF6143",
"foreground": "@drawable/logo",
"foreground_gravity": "right|bottom",
}
return {
"name": "framelayout_z_order_and_foreground",
"summary": "解析三层重叠 TextView 帧布局的 Z 轴后添加覆盖顺序、最大子视图尺寸及前景图定位",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_touch_move_custom_view() -> dict:
layout_xml = """<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mylayout" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@drawable/back" />"""
mezi_java = """public class MeziView extends View {
public float bitmapX = 0;
public float bitmapY = 200;
protected void onDraw(Canvas canvas) {
if(bitmap.isRecycled()) { bitmap.recycle(); }
}
}"""
main_java = """mezi.bitmapX = event.getX() - 150;
mezi.bitmapY = event.getY() - 150;
mezi.invalidate();"""
root = ET.fromstring(layout_xml)
pos = [0, 200]
touch_x, touch_y = 320, 410
offset = int(re.search(r"event\.getX\(\)\s*-\s*(\d+)", main_java).group(1))
pos = [touch_x - offset, touch_y - offset]
inverted_recycle_bug = "if(bitmap.isRecycled())" in mezi_java
actual = {
"frame_id": root.attrib[f"{ANDROID_NS}id"],
"initial_pos": [0, 200],
"after_touch_320_410": pos,
"detects_inverted_isRecycled_bug": inverted_recycle_bug,
}
expected = {
"frame_id": "@+id/mylayout",
"initial_pos": [0, 200],
"after_touch_320_410": [170, 260],
"detects_inverted_isRecycled_bug": True,
}
return {
"name": "framelayout_touch_move_custom_view",
"summary": "验证自定义 View 触摸重绘坐标偏移(-150)并检出源示例 if(bitmap.isRecycled()) 逻辑反转缺陷",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_timer_foreground_animation() -> dict:
layout_xml = """<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myframe" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:foregroundGravity="center" />"""
root = ET.fromstring(layout_xml)
frames = [f"s_{k}" for k in range(1, 9)]
# Handler starts i=0, increments i++ then calls move(i % 8) on each 0x123 tick
i = 0
sequence = []
for _ in range(9):
i += 1
sequence.append(frames[i % 8])
actual = {
"frame_id": root.attrib[f"{ANDROID_NS}id"],
"foreground_gravity": root.attrib[f"{ANDROID_NS}foregroundGravity"],
"tick_interval_ms": 170,
"msg_what_hex": "0x123",
"nine_tick_frame_sequence": sequence,
}
expected = {
"frame_id": "@+id/myframe",
"foreground_gravity": "center",
"tick_interval_ms": 170,
"msg_what_hex": "0x123",
"nine_tick_frame_sequence": ["s_2", "s_3", "s_4", "s_5", "s_6", "s_7", "s_8", "s_1", "s_2"],
}
return {
"name": "framelayout_timer_foreground_animation",
"summary": "模拟 170ms Timer + Handler(0x123) 驱动 FrameLayout.setForeground 的 8 帧循环序列",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_layout_gravity_positioning() -> dict:
pw, ph = 400, 600
cw, ch = 100, 80
def place(gravity: str) -> list[int]:
flags = set(gravity.split("|"))
x = 0
y = 0
if "right" in flags:
x = pw - cw
elif "center" in flags or "center_horizontal" in flags:
x = (pw - cw) // 2
if "bottom" in flags:
y = ph - ch
elif "center" in flags or "center_vertical" in flags:
y = (ph - ch) // 2
return [x, y, x + cw, y + ch]
actual = {
"left_top": place("left|top"),
"center": place("center"),
"right_bottom": place("right|bottom"),
"center_horizontal_bottom": place("center_horizontal|bottom"),
}
expected = {
"left_top": [0, 0, 100, 80],
"center": [150, 260, 250, 340],
"right_bottom": [300, 520, 400, 600],
"center_horizontal_bottom": [150, 520, 250, 600],
}
return {
"name": "framelayout_layout_gravity_positioning",
"summary": "计算 400x600dp 帧布局内子控件在不同 layout_gravity 组合下的准确绘制矩形",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main() -> None:
payload = {
"version": "CPython 3.12.14 offline Android XML/layout/manifest verifier (Android SDK/emulator absent)",
"cases": [
case_z_order_and_foreground(),
case_touch_move_custom_view(),
case_timer_foreground_animation(),
case_layout_gravity_positioning(),
],
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/android-tutorial-framelayout-lab/explore-tutorial-framelayout.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-framelayout.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1893.zip