"""Offline Android graphics verifier for Ch 127: Matrix 3x3 Affine & drawBitmapMesh."""
import json
import math
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
LAYOUT_XML = """<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/ly_bar"
android:layout_width="match_parent"
android:layout_height="64dp"
android:layout_alignParentBottom="true">
<Button android:id="@+id/btn_reset" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="重置" />
<Button android:id="@+id/btn_left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="左倾" />
<Button android:id="@+id/btn_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="右倾" />
<Button android:id="@+id/btn_zoomin" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="放大" />
<Button android:id="@+id/btn_zoomout" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:text="缩小" />
</LinearLayout>
<com.jay.canvasdemo3.MyView
android:id="@+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/ly_bar" />
</RelativeLayout>
"""
def mat_mul(a: list[list[float]], b: list[list[float]]) -> list[list[float]]:
return [[sum(a[i][k] * b[k][j] for k in range(3)) for j in range(3)] for i in range(3)]
def mat_translate(dx: float, dy: float) -> list[list[float]]:
return [[1.0, 0.0, dx], [0.0, 1.0, dy], [0.0, 0.0, 1.0]]
def mat_scale(sx: float, sy: float, px: float = 0.0, py: float = 0.0) -> list[list[float]]:
m = [[sx, 0.0, 0.0], [0.0, sy, 0.0], [0.0, 0.0, 1.0]]
return mat_mul(mat_translate(px, py), mat_mul(m, mat_translate(-px, -py)))
def mat_rotate(deg: float, px: float = 0.0, py: float = 0.0) -> list[list[float]]:
rad = math.radians(deg)
c, s = math.cos(rad), math.sin(rad)
m = [[c, -s, 0.0], [s, c, 0.0], [0.0, 0.0, 1.0]]
return mat_mul(mat_translate(px, py), mat_mul(m, mat_translate(-px, -py)))
def mat_skew(kx: float, ky: float) -> list[list[float]]:
return [[1.0, kx, 0.0], [ky, 1.0, 0.0], [0.0, 0.0, 1.0]]
def map_pt(m: list[list[float]], x: float, y: float) -> list[float]:
return [
round(m[0][0] * x + m[0][1] * y + m[0][2], 3),
round(m[1][0] * x + m[1][1] * y + m[1][2], 3),
]
def build_mesh(mesh_w: int, mesh_h: int, bmp_w: float, bmp_h: float) -> list[float]:
verts: list[float] = []
for y in range(mesh_h + 1):
fy = bmp_h * y / mesh_h
for x in range(mesh_w + 1):
fx = bmp_w * x / mesh_w
verts.extend([fx, fy])
return verts
def warp_mesh(orig: list[float], cx: float, cy: float) -> list[float]:
out = list(orig)
for i in range(0, len(orig), 2):
dx = cx - orig[i]
dy = cy - orig[i + 1]
dd = dx * dx + dy * dy
d = math.sqrt(dd)
if dd == 0.0:
out[i], out[i + 1] = cx, cy
continue
pull = 80000.0 / (dd * d)
if pull >= 1.0:
out[i], out[i + 1] = cx, cy
else:
out[i] = orig[i] + dx * pull
out[i + 1] = orig[i + 1] + dy * pull
return out
def main() -> None:
# Case 1: 3x3 Matrix setTranslate, setRotate(90, 50, 50), setScale(2, 2, 50, 50), setSkew(0.5, 0)
m_tr = mat_translate(30, 40)
m_rot = mat_rotate(90, 50, 50)
m_scl = mat_scale(2.0, 2.0, 50, 50)
m_skw = mat_skew(0.5, 0.0)
c1_actual = {
"translate_10_20": map_pt(m_tr, 10, 20),
"rotate90_pivot50_pt100_50": map_pt(m_rot, 100, 50),
"scale2_pivot50_pt75_60": map_pt(m_scl, 75, 60),
"skew05_0_pt20_40": map_pt(m_skw, 20, 40),
}
c1_expected = {
"translate_10_20": [40.0, 60.0],
"rotate90_pivot50_pt100_50": [50.0, 100.0],
"scale2_pivot50_pt75_60": [100.0, 70.0],
"skew05_0_pt20_40": [40.0, 40.0],
}
# Case 2: MyView button state transitions (reset/skew/scale bounds) + layout XML verification
root = ET.fromstring(LAYOUT_XML)
buttons = root.findall(".//Button")
scale = 1.0
for _ in range(15):
if scale < 2.0:
scale = round(scale + 0.1, 2)
scale_min = 1.0
for _ in range(10):
if scale_min > 0.5:
scale_min = round(scale_min - 0.1, 2)
# Double-apply bug: applying scale=1.5 twice yields 2.25x instead of 1.5x
double_applied_pt = map_pt(mat_mul(mat_scale(1.5, 1.5), mat_scale(1.5, 1.5)), 100, 100)
c2_actual = {
"button_count": len(buttons),
"scale_upper_bound": scale,
"scale_lower_bound": scale_min,
"double_matrix_bug_scale15_pt100": double_applied_pt,
}
c2_expected = {
"button_count": 5,
"scale_upper_bound": 2.0,
"scale_lower_bound": 0.5,
"double_matrix_bug_scale15_pt100": [225.0, 225.0],
}
# Case 3: drawBitmapMesh 20x20 grid vertex count & exact array length (21*21*2 = 882)
orig = build_mesh(20, 20, 400.0, 400.0)
c3_actual = {
"vertex_count": (20 + 1) * (20 + 1),
"verts_array_length": len(orig),
"first_vertex": [orig[0], orig[1]],
"center_vertex_idx220": [orig[220 * 2], orig[220 * 2 + 1]],
"last_vertex": [orig[-2], orig[-1]],
}
c3_expected = {
"vertex_count": 441,
"verts_array_length": 882,
"first_vertex": [0.0, 0.0],
"center_vertex_idx220": [200.0, 200.0],
"last_vertex": [400.0, 400.0],
}
# Case 4: warp(200, 200) pull deformation (d=20 -> pull=10 >= 1 snaps to touch; d=100 -> pull=0.08 pulls 8px)
warped = warp_mesh(orig, 200.0, 200.0)
# Vertex at (220, 200) is row 10, col 11 -> idx = 10*21 + 11 = 221 (d=20, pull=80000/8000=10 >= 1 -> snapped to (200,200))
# Vertex at (300, 200) is row 10, col 15 -> idx = 10*21 + 15 = 225 (d=100, pull=80000/1e6=0.08 -> 300 + (-100)*0.08 = 292)
# Vertex at (0, 0) is idx 0 (d=200*sqrt(2)=282.84 -> pull=0.003536 -> 0.707)
snapped_count = sum(
1 for i in range(0, len(warped), 2)
if warped[i] == 200.0 and warped[i + 1] == 200.0
)
c4_actual = {
"near_vertex_220_200_snapped": [round(warped[221 * 2], 3), round(warped[221 * 2 + 1], 3)],
"mid_vertex_300_200_pulled": [round(warped[225 * 2], 3), round(warped[225 * 2 + 1], 3)],
"corner_vertex_0_0_pulled": [round(warped[0], 3), round(warped[1], 3)],
"snapped_vertices_count": snapped_count,
}
c4_expected = {
"near_vertex_220_200_snapped": [200.0, 200.0],
"mid_vertex_300_200_pulled": [292.0, 200.0],
"corner_vertex_0_0_pulled": [0.707, 0.707],
"snapped_vertices_count": 13,
}
cases = [
{
"name": "matrix_3x3_affine_transforms",
"summary": "核验 3x3 Matrix 的 setTranslate、围绕轴心旋转/缩放与 setSkew 仿射坐标变换",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "matrix_view_layout_and_double_apply_bug",
"summary": "核验五按钮控制栏 XML、缩放上下界 [0.5, 2.0] 及 createBitmap 与 drawBitmap 重复乘矩阵缺陷",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "bitmap_mesh_grid_initialization",
"summary": "核验 drawBitmapMesh 20x20 网格的 441 顶点与 882 浮点长度(纠偏 +1 而非 +2)",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "bitmap_mesh_warp_pull_deformation",
"summary": "核验触摸中心 (200,200) 下 80000/(d^3) 扭曲度公式的吸附顶点与远距衰减偏移",
"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-api3-lab/explore-tutorial-canvas-api3.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-canvas-api3.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1855.zip
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip