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

验证双缓冲画板触摸抖动过滤、21 项适配器传参及 40x40 像素擦除边界裁剪

PYTHONWindows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。explore-tutorial-bitmap-demo.py
explore-tutorial-bitmap-demo.py162 行
"""Offline verification lab for Android Ch 111: tutorial-bitmap-demo."""
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}"

SELECT_LAYOUT_XML = """<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <ImageView android:id="@+id/img_choose" android:layout_width="320dp" android:layout_height="320dp" />
    <Button android:id="@+id/btn_choose" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确认选择" />
    <Gallery android:id="@+id/gay_choose" android:layout_width="match_parent" android:layout_height="wrap_content"
        android:layout_marginTop="25dp" android:spacing="1pt" android:unselectedAlpha="0.6" />
</LinearLayout>"""

ERASE_LAYOUT_XML = """<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">
    <ImageView android:id="@+id/img_after" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    <ImageView android:id="@+id/img_before" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</FrameLayout>"""


def case_double_buffer_sketchpad_touch_path():
    # Simulate MyView onTouchEvent jitter filter: dx > 3 or dy > 3
    events = [
        ("DOWN", 10, 10),
        ("MOVE", 12, 11),  # dx=2, dy=1 -> filtered
        ("MOVE", 18, 12),  # dx=6, dy=1 -> accepted
        ("MOVE", 19, 13),  # dx=1, dy=1 -> filtered
        ("MOVE", 30, 40),  # dx=11, dy=27 -> accepted
    ]
    path_ops = []
    last_x = last_y = 0
    for act, x, y in events:
        if act == "DOWN":
            last_x, last_y = x, y
            path_ops.append(("moveTo", x, y))
        elif act == "MOVE":
            if abs(x - last_x) > 3 or abs(y - last_y) > 3:
                path_ops.append(("lineTo", x, y))
            last_x, last_y = x, y
    w, h = 480, 800
    actual = {
        "offscreen_bitmap_bytes": w * h * 4,
        "path_ops": [list(op) for op in path_ops],
        "accepted_segments": sum(1 for op in path_ops if op[0] == "lineTo"),
    }
    expected = {
        "offscreen_bitmap_bytes": 1536000,
        "path_ops": [["moveTo", 10, 10], ["lineTo", 18, 12], ["lineTo", 30, 40]],
        "accepted_segments": 2,
    }
    return {
        "name": "double_buffer_sketchpad_touch_path",
        "summary": "Verify double-buffered Bitmap Canvas allocation and >3px touch jitter filter",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_gallery_adapter_and_bundle_selection():
    root = ET.fromstring(SELECT_LAYOUT_XML)
    ids = [c.attrib[NS + "id"] for c in root if (NS + "id") in c.attrib]
    image_ids = [f"pre{i}" for i in range(1, 22)]
    selected_index = 4
    bundle = {"num": str(selected_index)}
    parsed_pos = int(bundle["num"])
    actual = {
        "select_layout_ids": ids,
        "adapter_count": len(image_ids),
        "selected_resource": image_ids[parsed_pos],
        "parsed_bundle_index": parsed_pos,
    }
    expected = {
        "select_layout_ids": ["@+id/img_choose", "@+id/btn_choose", "@+id/gay_choose"],
        "adapter_count": 21,
        "selected_resource": "pre5",
        "parsed_bundle_index": 4,
    }
    return {
        "name": "gallery_adapter_and_bundle_selection",
        "summary": "Verify Gallery selector layout, 21-item BaseAdapter, and Bundle index passing",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_pixel_erasure_block_bounds_and_clipping():
    root = ET.fromstring(ERASE_LAYOUT_XML)
    layer_ids = [c.attrib[NS + "id"] for c in root]
    img_w, img_h = 200, 200

    def erase_count(cx: int, cy: int):
        erased = 0
        for i in range(-20, 20):
            for j in range(-20, 20):
                nx, ny = i + cx, j + cy
                if 0 <= nx < img_w and 0 <= ny < img_h:
                    erased += 1
        return erased

    actual = {
        "frame_layers_bottom_to_top": layer_ids,
        "center_touch_erased_pixels": erase_count(100, 100),
        "corner_touch_clipped_pixels": erase_count(5, 5),
        "loop_span_is_40x40_not_20x20": erase_count(100, 100) == 40 * 40,
    }
    expected = {
        "frame_layers_bottom_to_top": ["@+id/img_after", "@+id/img_before"],
        "center_touch_erased_pixels": 1600,
        "corner_touch_clipped_pixels": 625,
        "loop_span_is_40x40_not_20x20": True,
    }
    return {
        "name": "pixel_erasure_block_bounds_and_clipping",
        "summary": "Verify FrameLayout dual-layer order and -20..19 (40x40) setPixel bounds clipping",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_pixel_block_vs_smooth_path_erasure_comparison():
    # Compare 40x40 square area vs radius-20 circle area
    square_px = 40 * 40
    circle_px = sum(1 for i in range(-20, 20) for j in range(-20, 20) if i * i + j * j <= 20 * 20)
    actual = {
        "square_setpixel_calls_per_move": square_px,
        "circle_radius20_pixels": circle_px,
        "square_has_blocky_corners": square_px > circle_px,
    }
    expected = {
        "square_setpixel_calls_per_move": 1600,
        "circle_radius20_pixels": 1255,
        "square_has_blocky_corners": True,
    }
    return {
        "name": "pixel_block_vs_smooth_path_erasure_comparison",
        "summary": "Verify why 40x40 square setPixel loops create blocky edges compared to round Path erasure",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_double_buffer_sketchpad_touch_path(),
        case_gallery_adapter_and_bundle_selection(),
        case_pixel_erasure_block_bounds_and_clipping(),
        case_pixel_block_vs_smooth_path_erasure_comparison(),
    ]
    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-bitmap-demo-lab/explore-tutorial-bitmap-demo.py

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

下载课程关联工程包:Android

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

下载课程关联工程包:Android

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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