"""Offline Android graphics verifier for Ch 126: Canvas Clipping & Region.Op."""
from dataclasses import dataclass
import json
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
@dataclass(frozen=True)
class Rect:
left: int
top: int
right: int
bottom: int
def contains(self, x: int, y: int) -> bool:
return self.left <= x < self.right and self.top <= y < self.bottom
def eval_region_op(a: Rect, b: Rect, op: str, grid_size: int = 200) -> int:
# Count integer pixel area inside [0, grid_size) x [0, grid_size)
area = 0
for x in range(grid_size):
for y in range(grid_size):
in_a = a.contains(x, y)
in_b = b.contains(x, y)
if op == "DIFFERENCE":
keep = in_a and not in_b
elif op == "INTERSECT":
keep = in_a and in_b
elif op == "UNION":
keep = in_a or in_b
elif op == "XOR":
keep = in_a != in_b
elif op == "REVERSE_DIFFERENCE":
keep = in_b and not in_a
elif op == "REPLACE":
keep = in_b
else:
raise ValueError(op)
if keep:
area += 1
return area
def blinds_region_area(width: int, height: int, clip_height: int, limit_len: int) -> tuple[int, int]:
strips = 0
area = 0
i = 0
while i * clip_height <= height:
top = i * clip_height
bottom = min((i + 1) * clip_height, height)
if bottom > top:
strips += 1
area += max(0, min(width, limit_len)) * (bottom - top)
i += 1
return strips, area
def main() -> None:
# Case 1: All 6 Region.Op set operations on A=(10,10,110,110) and B=(50,50,150,150)
# |A|=10000, |B|=10000, |A∩B|=60*60=3600
a = Rect(10, 10, 110, 110)
b = Rect(50, 50, 150, 150)
ops = ("DIFFERENCE", "INTERSECT", "UNION", "XOR", "REVERSE_DIFFERENCE", "REPLACE")
c1_actual = {op: eval_region_op(a, b, op, 200) for op in ops}
c1_expected = {
"DIFFERENCE": 6400,
"INTERSECT": 3600,
"UNION": 16400,
"XOR": 12800,
"REVERSE_DIFFERENCE": 6400,
"REPLACE": 10000,
}
# Case 2: Blinds effect (CLIP_HEIGHT=50, 200x200 bitmap): HIDE (-10/step) -> SHOW (+5/step)
strips_full, area_full = blinds_region_area(200, 200, 50, 200)
strips_half, area_half = blinds_region_area(200, 200, 50, 100)
# Transition steps from 200 -> 0 by -10 takes 20 steps; 0 -> 200 by +5 takes 40 steps
c2_actual = {
"strip_count": strips_full,
"area_at_limit_200": area_full,
"area_at_limit_100": area_half,
"hide_steps": 200 // 10,
"show_steps": 200 // 5,
}
c2_expected = {
"strip_count": 4,
"area_at_limit_200": 40000,
"area_at_limit_100": 20000,
"hide_steps": 20,
"show_steps": 40,
}
# Case 3: clipRect affected by Canvas.translate(300,300) vs clipRegion unaffected
# Local clipRect(100,100,300,300) after translate(300,300) -> screen [400,400,600,600]
tx, ty = 300, 300
clip_rect_screen = [100 + tx, 100 + ty, 300 + tx, 300 + ty]
clip_region_screen = [100, 100, 300, 300]
c3_actual = {
"clipRect_screen_bounds": clip_rect_screen,
"clipRegion_screen_bounds": clip_region_screen,
"clipRect_affected_by_matrix": clip_rect_screen != clip_region_screen,
}
c3_expected = {
"clipRect_screen_bounds": [400, 400, 600, 600],
"clipRegion_screen_bounds": [100, 100, 300, 300],
"clipRect_affected_by_matrix": True,
}
# Case 4: RoundImageView clipPath geometry fix on 300x200 view & API 28 Region.Op restriction
w, h = 300, 200
buggy_circle = [w // 2, w // 2, h // 2]
fixed_circle = [w // 2, h // 2, min(w, h) // 2]
api28_allowed_ops = ["INTERSECT", "DIFFERENCE"]
c4_actual = {
"buggy_non_square_circle_cx_cy_r": buggy_circle,
"fixed_non_square_circle_cx_cy_r": fixed_circle,
"api28_supported_clip_ops": api28_allowed_ops,
}
c4_expected = {
"buggy_non_square_circle_cx_cy_r": [150, 150, 100],
"fixed_non_square_circle_cx_cy_r": [150, 100, 100],
"api28_supported_clip_ops": ["INTERSECT", "DIFFERENCE"],
}
cases = [
{
"name": "region_op_six_set_operations",
"summary": "核验 Region.Op 六种集合裁剪运算在 A(10,10,110,110) 与 B(50,50,150,150) 上的精确像素面积",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "region_union_blinds_animation",
"summary": "核验百叶窗 Region.union 交替条带数量、面积变化及 HIDE(-10)/SHOW(+5) 状态步数",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "cliprect_vs_clipregion_transform_matrix",
"summary": "核验 translate(300,300) 对 clipRect 生效而对 clipRegion 不生效的屏幕坐标差异",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "round_imageview_clippath_and_api28_ops",
"summary": "核验 RoundImageView 非正方形圆心偏移纠偏及 API 28 后仅保留 INTERSECT/DIFFERENCE 的限制",
"expected": c4_expected,
"actual": c4_actual,
"passed": c4_actual == c4_expected,
},
]
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-canvas-api2-lab/explore-tutorial-canvas-api2.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-canvas-api2.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1854.zip