蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行原型页面跳转图可达性、360x640dp 线框边界审核、XML 导出与 3x 设计稿 dp/sp 令牌换算

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-interface-design.py
explore-tutorial-interface-design.py180 行
"""Offline deterministic verifier for Android CH11 (tutorial-interface-design)."""
from __future__ import annotations

import json
import xml.etree.ElementTree as ET
from collections import deque


def case_prototype_screen_graph() -> dict:
    graph = {
        "news_home": ["category_tech", "category_sports", "search"],
        "category_tech": ["article_detail", "news_home"],
        "category_sports": ["article_detail", "news_home"],
        "search": ["article_detail", "news_home"],
        "article_detail": ["comments", "news_home"],
        "comments": ["article_detail"],
    }
    visited = set()
    q = deque(["news_home"])
    while q:
        node = q.popleft()
        if node in visited:
            continue
        visited.add(node)
        for nxt in graph[node]:
            if nxt not in visited:
                q.append(nxt)
    dead_ends = [k for k, v in graph.items() if not v]
    actual = {
        "screen_count": len(graph),
        "all_reachable_from_home": visited == set(graph),
        "dead_end_screens": dead_ends,
        "transition_count": sum(len(v) for v in graph.values()),
    }
    expected = {
        "screen_count": 6,
        "all_reachable_from_home": True,
        "dead_end_screens": [],
        "transition_count": 12,
    }
    return {
        "name": "prototype_screen_graph_reachability",
        "summary": "验证新闻类 App 原型页面交互跳转图的全局可达性与零死胡同闭环",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_wireframe_bounds_audit() -> dict:
    canvas_w, canvas_h = 360, 640
    widgets = [
        {"id": "top_bar", "x": 0, "y": 0, "w": 360, "h": 56, "interactive": True},
        {"id": "category_tabs", "x": 0, "y": 56, "w": 360, "h": 48, "interactive": True},
        {"id": "news_card_1", "x": 16, "y": 116, "w": 328, "h": 96, "interactive": True},
        {"id": "news_card_2", "x": 16, "y": 224, "w": 328, "h": 96, "interactive": True},
        {"id": "bottom_nav", "x": 0, "y": 584, "w": 360, "h": 56, "interactive": True},
    ]
    in_bounds = all(
        0 <= w["x"] and w["x"] + w["w"] <= canvas_w and 0 <= w["y"] and w["y"] + w["h"] <= canvas_h
        for w in widgets
    )
    touch_ok = all(w["h"] >= 48 for w in widgets if w["interactive"])
    overlaps = 0
    for i in range(len(widgets)):
        for j in range(i + 1, len(widgets)):
            a, b = widgets[i], widgets[j]
            if not (a["x"] + a["w"] <= b["x"] or b["x"] + b["w"] <= a["x"] or a["y"] + a["h"] <= b["y"] or b["y"] + b["h"] <= a["y"]):
                overlaps += 1
    actual = {
        "widget_count": len(widgets),
        "all_in_bounds": in_bounds,
        "all_touch_targets_ge_48dp": touch_ok,
        "overlap_pairs": overlaps,
    }
    expected = {
        "widget_count": 5,
        "all_in_bounds": True,
        "all_touch_targets_ge_48dp": True,
        "overlap_pairs": 0,
    }
    return {
        "name": "wireframe_component_bounds_audit",
        "summary": "审核 360x640dp 原型线框图画布边界、零重叠与 >=48dp 可点击区域规范",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_droiddraw_xml_emitter() -> dict:
    spec = [
        {"tag": "TextView", "id": "@+id/title", "text": "News Reader"},
        {"tag": "EditText", "id": "@+id/keyword", "hint": "Search"},
        {"tag": "Button", "id": "@+id/btn_go", "text": "Go"},
    ]
    android_uri = "http://schemas.android.com/apk/res/android"
    ET.register_namespace("android", android_uri)
    ns = f"{{{android_uri}}}"
    root = ET.Element(
        "LinearLayout",
        {
            f"{ns}layout_width": "match_parent",
            f"{ns}layout_height": "match_parent",
            f"{ns}orientation": "vertical",
        },
    )
    for item in spec:
        attrs = {
            f"{ns}id": item["id"],
            f"{ns}layout_width": "wrap_content",
            f"{ns}layout_height": "wrap_content",
        }
        if "text" in item:
            attrs[f"{ns}text"] = item["text"]
        if "hint" in item:
            attrs[f"{ns}hint"] = item["hint"]
        ET.SubElement(root, item["tag"], attrs)
    xml_text = ET.tostring(root, encoding="unicode")
    parsed = ET.fromstring(xml_text)
    actual = {
        "root_tag": parsed.tag,
        "child_tags": [c.tag for c in parsed],
        "child_ids": [c.attrib[f"{ns}id"] for c in parsed],
    }
    expected = {
        "root_tag": "LinearLayout",
        "child_tags": ["TextView", "EditText", "Button"],
        "child_ids": ["@+id/title", "@+id/keyword", "@+id/btn_go"],
    }
    return {
        "name": "droiddraw_to_android_xml_emitter",
        "summary": "模拟可视化原型工具将组件清单导出为合法 Android XML 布局并回读校验",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_design_handoff_tokens() -> dict:
    scale_xxhdpi = 3.0
    design_px = {"page_padding": 48, "card_radius": 24, "title_text": 54, "body_text": 42}
    dimens = ET.Element("resources")
    for k, px in design_px.items():
        unit = "sp" if "text" in k else "dp"
        el = ET.SubElement(dimens, "dimen", {"name": k})
        el.text = f"{int(px / scale_xxhdpi)}{unit}"
    parsed = ET.fromstring(ET.tostring(dimens, encoding="unicode"))
    actual = {el.attrib["name"]: el.text for el in parsed.findall("dimen")}
    expected = {
        "page_padding": "16dp",
        "card_radius": "8dp",
        "title_text": "18sp",
        "body_text": "14sp",
    }
    return {
        "name": "design_handoff_dp_sp_spec",
        "summary": "将 3x(xxhdpi) 视觉稿像素标注换算为 dimens.xml 的 dp/sp 设计令牌",
        "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_prototype_screen_graph(),
            case_wireframe_bounds_audit(),
            case_droiddraw_xml_emitter(),
            case_design_handoff_tokens(),
        ],
    }
    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-interface-design-lab/explore-tutorial-interface-design.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-tutorial-interface-design.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Android

课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip

下载课程关联工程包:Android

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1909.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习