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

验证 Bitmap/Inset/Clip/Rotate/Animation 五类 Drawable XML 属性与 level 步进计算

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-drawable2.py
explore-tutorial-drawable2.py199 行
"""Offline verification lab for Android Ch 106: tutorial-drawable2."""
import json
import xml.etree.ElementTree as ET

VERSION = "CPython 3.12.14 offline Android socket/graphics/ColorMatrix verifier (Android SDK/emulator absent)"
NS = "{http://schemas.android.com/apk/res/android}"

BITMAP_XML = """<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:dither="true"
    android:src="@drawable/ic_launcher"
    android:tileMode="mirror" />"""

INSET_XML = """<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/test1"
    android:insetBottom="10dp"
    android:insetLeft="10dp"
    android:insetRight="10dp"
    android:insetTop="10dp" />"""

CLIP_XML = """<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:clipOrientation="horizontal"
    android:drawable="@mipmap/ic_bg_demo"
    android:gravity="left" />"""

ROTATE_XML = """<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/ic_launcher"
    android:fromDegrees="-180"
    android:pivotX="50%"
    android:pivotY="50%" />"""

ANIM_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/ic_pull_to_refresh_loading01" android:duration="100" />
    <item android:drawable="@mipmap/ic_pull_to_refresh_loading02" android:duration="100" />
    <item android:drawable="@mipmap/ic_pull_to_refresh_loading03" android:duration="100" />
    <item android:drawable="@mipmap/ic_pull_to_refresh_loading04" android:duration="100" />
    <item android:drawable="@mipmap/ic_pull_to_refresh_loading05" android:duration="100" />
    <item android:drawable="@mipmap/ic_pull_to_refresh_loading06" android:duration="100" />
</animation-list>"""


def case_bitmap_drawable_xml_and_tilemode():
    root = ET.fromstring(BITMAP_XML)
    valid_modes = {"disabled", "clamp", "repeat", "mirror"}
    tm = root.attrib[NS + "tileMode"]
    actual = {
        "tag": root.tag,
        "dither": root.attrib[NS + "dither"] == "true",
        "tileMode": tm,
        "tileMode_valid": tm in valid_modes,
        "gravity_ignored_when_tileMode_set": tm != "disabled",
    }
    expected = {
        "tag": "bitmap",
        "dither": True,
        "tileMode": "mirror",
        "tileMode_valid": True,
        "gravity_ignored_when_tileMode_set": True,
    }
    return {
        "name": "bitmap_drawable_xml_and_tilemode",
        "summary": "Verify <bitmap> XML attributes and tileMode gravity override behavior",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_inset_drawable_bounds_calculation():
    root = ET.fromstring(INSET_XML)
    left = int(root.attrib[NS + "insetLeft"].replace("dp", ""))
    top = int(root.attrib[NS + "insetTop"].replace("dp", ""))
    right = int(root.attrib[NS + "insetRight"].replace("dp", ""))
    bottom = int(root.attrib[NS + "insetBottom"].replace("dp", ""))
    outer = (0, 0, 200, 120)
    inner = (outer[0] + left, outer[1] + top, outer[2] - right, outer[3] - bottom)
    actual = {
        "tag": root.tag,
        "insets_dp": [left, top, right, bottom],
        "inner_bounds": list(inner),
    }
    expected = {
        "tag": "inset",
        "insets_dp": [10, 10, 10, 10],
        "inner_bounds": [10, 10, 190, 110],
    }
    return {
        "name": "inset_drawable_bounds_calculation",
        "summary": "Verify <inset> XML attributes and inner drawable rectangle calculation",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_clip_drawable_level_progression():
    root = ET.fromstring(CLIP_XML)
    level = 0
    steps = 0
    while level < 10000:
        level += 500
        steps += 1
    full_width = 400
    clipped_widths = [full_width * lv // 10000 for lv in (0, 2500, 5000, 10000)]
    actual = {
        "orientation": root.attrib[NS + "clipOrientation"],
        "gravity": root.attrib[NS + "gravity"],
        "steps_to_10000": steps,
        "clipped_widths": clipped_widths,
        "imageview_requires_src_for_getDrawable": True,
    }
    expected = {
        "orientation": "horizontal",
        "gravity": "left",
        "steps_to_10000": 20,
        "clipped_widths": [0, 100, 200, 400],
        "imageview_requires_src_for_getDrawable": True,
    }
    return {
        "name": "clip_drawable_level_progression",
        "summary": "Verify <clip> XML orientation/gravity and 0..10000 level clipping progression",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_rotate_drawable_angle_interpolation():
    root = ET.fromstring(ROTATE_XML)
    from_deg = float(root.attrib[NS + "fromDegrees"])
    to_deg = float(root.attrib.get(NS + "toDegrees", "360"))
    angles = [round(from_deg + (to_deg - from_deg) * (lv / 10000.0), 2) for lv in (0, 2500, 5000, 10000)]
    steps = 10000 // 400
    actual = {
        "fromDegrees": from_deg,
        "default_toDegrees": to_deg,
        "pivot": [root.attrib[NS + "pivotX"], root.attrib[NS + "pivotY"]],
        "angles_at_levels": angles,
        "steps_at_400_increment": steps,
    }
    expected = {
        "fromDegrees": -180.0,
        "default_toDegrees": 360.0,
        "pivot": ["50%", "50%"],
        "angles_at_levels": [-180.0, -45.0, 90.0, 360.0],
        "steps_at_400_increment": 25,
    }
    return {
        "name": "rotate_drawable_angle_interpolation",
        "summary": "Verify <rotate> XML pivot/degrees and linear level-to-angle interpolation",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_animation_drawable_frame_schedule():
    root = ET.fromstring(ANIM_XML)
    items = root.findall("item")
    durations = [int(it.attrib[NS + "duration"]) for it in items]
    actual = {
        "oneshot": root.attrib[NS + "oneshot"] == "true",
        "frame_count": len(items),
        "cycle_duration_ms": sum(durations),
        "loop_enabled": root.attrib[NS + "oneshot"] == "false",
    }
    expected = {
        "oneshot": False,
        "frame_count": 6,
        "cycle_duration_ms": 600,
        "loop_enabled": True,
    }
    return {
        "name": "animation_drawable_frame_schedule",
        "summary": "Verify <animation-list> oneshot flag, frame count, and 600ms total cycle duration",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_bitmap_drawable_xml_and_tilemode(),
        case_inset_drawable_bounds_calculation(),
        case_clip_drawable_level_progression(),
        case_rotate_drawable_angle_interpolation(),
        case_animation_drawable_frame_schedule(),
    ]
    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-drawable2-lab/explore-tutorial-drawable2.py

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

下载课程关联工程包:Android

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

下载课程关联工程包:Android

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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