"""Offline Android graphics verifier for Ch 121: Paint PathEffect."""
from dataclasses import dataclass
import json
import math
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
def dash_segments(total_len: float, intervals: list[float], phase: float) -> list[tuple[float, float]]:
if len(intervals) < 2 or len(intervals) % 2 != 0:
raise ValueError("DashPathEffect requires an even number of intervals >= 2")
period = sum(intervals)
offset = phase % period
# Locate starting interval at offset
idx = 0
rem = offset
while rem >= intervals[idx]:
rem -= intervals[idx]
idx = (idx + 1) % len(intervals)
pos = 0.0
out: list[tuple[float, float]] = []
first_span = intervals[idx] - rem
while pos < total_len - 1e-9:
span = first_span if first_span is not None else intervals[idx]
first_span = None
end = min(total_len, pos + span)
if idx % 2 == 0 and end > pos:
out.append((round(pos, 3), round(end, 3)))
pos = end
idx = (idx + 1) % len(intervals)
return out
def corner_fillet_tangent(radius: float, interior_deg: float) -> tuple[float, float]:
# Distance from corner vertex to tangent point along each edge: r / tan(theta/2)
half_rad = math.radians(interior_deg / 2.0)
tangent_dist = radius / math.tan(half_rad)
arc_len = radius * (math.pi - math.radians(interior_deg))
return round(tangent_dist, 4), round(arc_len, 4)
def path_dash_stamps(path_len: float, advance: float, phase: float, style: str) -> dict:
valid_styles = ("ROTATE", "MORPH", "TRANSLATE")
if style not in valid_styles:
raise ValueError(f"Unknown PathDashPathEffect.Style: {style}")
start = (-phase) % advance
positions = []
pos = start
while pos <= path_len + 1e-9:
positions.append(round(pos, 2))
pos += advance
return {"style": style, "count": len(positions), "positions": positions}
@dataclass(frozen=True)
class EffectSpec:
kind: str
params: tuple
def apply_effect_order(kind: str, a: str, b: str) -> list[str]:
if kind == "ComposePathEffect":
# ComposePathEffect(outerpe, innerpe): applies innerpe first, then outerpe
return [f"stage1:{b}", f"stage2:{a}(on {b})"]
if kind == "SumPathEffect":
# SumPathEffect(first, second): renders both independently and overlays
return [f"layer_a:{a}", f"layer_b:{b}"]
raise ValueError(kind)
def main() -> None:
# Case 1: DashPathEffect(intervals=[20, 10, 5, 10], phase=0 vs phase=12)
segs_p0 = dash_segments(60.0, [20.0, 10.0, 5.0, 10.0], 0.0)
segs_p12 = dash_segments(60.0, [20.0, 10.0, 5.0, 10.0], 12.0)
odd_rejected = False
try:
dash_segments(60.0, [20.0, 10.0, 5.0], 0.0)
except ValueError:
odd_rejected = True
c1_actual = {
"phase_0_on_segments": [list(s) for s in segs_p0],
"phase_12_on_segments": [list(s) for s in segs_p12],
"odd_interval_rejected": odd_rejected,
}
c1_expected = {
"phase_0_on_segments": [[0.0, 20.0], [30.0, 35.0], [45.0, 60.0]],
"phase_12_on_segments": [[0.0, 8.0], [18.0, 23.0], [33.0, 53.0]],
"odd_interval_rejected": True,
}
# Case 2: CornerPathEffect(10) on 90-degree & 60-degree vertices + DiscretePathEffect subdivision
tan90, arc90 = corner_fillet_tangent(10.0, 90.0)
tan60, arc60 = corner_fillet_tangent(10.0, 60.0)
# DiscretePathEffect(segmentLength=3.0, deviation=5.0) on a 40px edge -> ceil(40/3) = 14 sub-segments
discrete_segs = math.ceil(40.0 / 3.0)
c2_actual = {
"corner_90_tangent_arc": [tan90, arc90],
"corner_60_tangent_arc": [tan60, arc60],
"discrete_subsegments_on_40px": discrete_segs,
"max_perpendicular_deviation": 5.0,
}
c2_expected = {
"corner_90_tangent_arc": [10.0, 15.708],
"corner_60_tangent_arc": [17.3205, 20.944],
"discrete_subsegments_on_40px": 14,
"max_perpendicular_deviation": 5.0,
}
# Case 3: PathDashPathEffect(rect 8x8, advance=12, phase=0/2/4, ROTATE/MORPH/TRANSLATE)
stamps_p0 = path_dash_stamps(40.0, 12.0, 0.0, "ROTATE")
stamps_p2 = path_dash_stamps(40.0, 12.0, 2.0, "ROTATE")
styles = [path_dash_stamps(24.0, 12.0, 0.0, s)["style"] for s in ("ROTATE", "MORPH", "TRANSLATE")]
c3_actual = {
"phase_0_positions": stamps_p0["positions"],
"phase_2_positions": stamps_p2["positions"],
"supported_styles": styles,
}
c3_expected = {
"phase_0_positions": [0.0, 12.0, 24.0, 36.0],
"phase_2_positions": [10.0, 22.0, 34.0],
"supported_styles": ["ROTATE", "MORPH", "TRANSLATE"],
}
# Case 4: ComposePathEffect vs SumPathEffect + PathEffectView 7-row layout & phase animation
compose_flow = apply_effect_order("ComposePathEffect", "Discrete(3,5)", "PathDash(12)")
sum_flow = apply_effect_order("SumPathEffect", "Discrete(3,5)", "PathDash(12)")
row_y_offsets = [10 + i * 60 for i in range(7)]
phases_5_frames = [0 + 2 * f for f in range(5)]
c4_actual = {
"compose_order": compose_flow,
"sum_layers": sum_flow,
"seven_row_y_offsets": row_y_offsets,
"phase_progression_5_frames": phases_5_frames,
}
c4_expected = {
"compose_order": ["stage1:PathDash(12)", "stage2:Discrete(3,5)(on PathDash(12))"],
"sum_layers": ["layer_a:Discrete(3,5)", "layer_b:PathDash(12)"],
"seven_row_y_offsets": [10, 70, 130, 190, 250, 310, 370],
"phase_progression_5_frames": [0, 2, 4, 6, 8],
}
cases = [
{
"name": "dash_patheffect_intervals_and_phase",
"summary": "核验 DashPathEffect([20,10,5,10], phase) 在相位 0 与 12 下的实线段分布及奇数数组校验",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "corner_and_discrete_patheffect_geometry",
"summary": "核验 CornerPathEffect(10) 在 90°/60° 夹角的切线回退与弧长,以及 DiscretePathEffect(3,5) 分段数",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "pathdash_advance_phase_and_styles",
"summary": "核验 PathDashPathEffect(advance=12) 相位推进及 ROTATE/MORPH/TRANSLATE 三种连接风格",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "compose_vs_sum_and_view_animation",
"summary": "核验 ComposePathEffect 顺序嵌套与 SumPathEffect 双层叠加语义,以及 7 行绘制偏移与 phase+=2 推进",
"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-patheffect-lab/explore-tutorial-patheffect.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-patheffect.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1925.zip