"""Offline verification lab for Android Ch 116: tutorial-xfermode-porterduff4."""
import json
import math
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Android socket/graphics/ColorMatrix verifier (Android SDK/emulator absent)"
LAYOUT_XML = """<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.jay.xfermodedemo2.StripMeiZi
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>"""
def dst_out(src_a: float, dst_argb):
da = dst_argb[0] / 255.0
out_a = da * (1.0 - src_a)
if out_a <= 1e-9:
return [0, 0, 0, 0]
return [round(out_a * 255), dst_argb[1], dst_argb[2], dst_argb[3]]
def src_over_blend(fg_argb, bg_argb):
fa = fg_argb[0] / 255.0
return [
255,
round(fg_argb[1] * fa + bg_argb[1] * (1.0 - fa)),
round(fg_argb[2] * fa + bg_argb[2] * (1.0 - fa)),
round(fg_argb[3] * fa + bg_argb[3] * (1.0 - fa)),
]
def case_dst_out_scratch_overlay_pixel_math():
fg = [255, 200, 100, 80]
bg = [255, 40, 160, 220]
erased_fg = dst_out(1.0, fg)
untouched_fg = dst_out(0.0, fg)
final_at_erased = src_over_blend(erased_fg, bg)
final_at_untouched = src_over_blend(untouched_fg, bg)
actual = {
"dst_out_opaque_stroke_clears_fg": erased_fg,
"dst_out_no_stroke_keeps_fg": untouched_fg,
"composite_reveals_bg": final_at_erased,
"composite_keeps_fg": final_at_untouched,
}
expected = {
"dst_out_opaque_stroke_clears_fg": [0, 0, 0, 0],
"dst_out_no_stroke_keeps_fg": [255, 200, 100, 80],
"composite_reveals_bg": [255, 40, 160, 220],
"composite_keeps_fg": [255, 200, 100, 80],
}
return {
"name": "dst_out_scratch_overlay_pixel_math",
"summary": "Verify PorterDuff.Mode.DST_OUT clears foreground overlay along stroke to reveal background layer",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_smooth_stroke_path_touch_and_antialiased_edge():
fg = [255, 200, 100, 80]
bg = [255, 40, 160, 220]
# Half-alpha antialiased stroke edge (Sa=0.5) leaves DST at alpha 0.5 -> smooth 50/50 blend
edge_fg = dst_out(0.5, fg)
blended_edge = src_over_blend(edge_fg, bg)
events = [("DOWN", 50, 50), ("MOVE", 52, 51), ("MOVE", 80, 90)]
segments = 0
lx, ly = 50, 50
for act, x, y in events[1:]:
if abs(x - lx) > 3 or abs(y - ly) > 3:
segments += 1
lx, ly = x, y
actual = {
"stroke_width_px": 80,
"stroke_cap_join": ["ROUND", "ROUND"],
"edge_fg_after_dst_out": edge_fg,
"blended_edge_over_bg": blended_edge,
"accepted_move_segments": segments,
}
expected = {
"stroke_width_px": 80,
"stroke_cap_join": ["ROUND", "ROUND"],
"edge_fg_after_dst_out": [128, 200, 100, 80],
"blended_edge_over_bg": [255, 120, 130, 150],
"accepted_move_segments": 1,
}
return {
"name": "smooth_stroke_path_touch_and_antialiased_edge",
"summary": "Verify round-cap 80px stroke DST_OUT antialiased edge blending and touch jitter filter",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_three_layer_draw_order_verification():
root = ET.fromstring(LAYOUT_XML)
child_tags = [c.tag for c in root]
draw_steps = [
"1:canvas.drawBitmap(mBackBitmap)",
"2:mCanvas.drawPath(mPath,mPaint_DST_OUT)",
"3:canvas.drawBitmap(mBeforeBitmap)",
]
actual = {
"root_layout": root.tag,
"custom_view_tag": child_tags[0],
"onDraw_sequence": draw_steps,
}
expected = {
"root_layout": "RelativeLayout",
"custom_view_tag": "com.jay.xfermodedemo2.StripMeiZi",
"onDraw_sequence": draw_steps,
}
return {
"name": "three_layer_draw_order_verification",
"summary": "Verify background -> offscreen DST_OUT path -> foreground onDraw sequence and layout XML",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_dst_out_vs_pixel_loop_comparison():
stroke_radius = 40.0 # strokeWidth=80 -> radius 40
circular_cap_area = round(math.pi * stroke_radius * stroke_radius, 2)
square_loop_area = 40 * 40
actual = {
"round_cap_area_px2": circular_cap_area,
"legacy_square_loop_px2": square_loop_area,
"uses_vector_path_instead_of_setPixel": True,
}
expected = {
"round_cap_area_px2": 5026.55,
"legacy_square_loop_px2": 1600,
"uses_vector_path_instead_of_setPixel": True,
}
return {
"name": "dst_out_vs_pixel_loop_comparison",
"summary": "Compare round Path+DST_OUT brush footprint against legacy square setPixel loop",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_dst_out_scratch_overlay_pixel_math(),
case_smooth_stroke_path_touch_and_antialiased_edge(),
case_three_layer_draw_order_verification(),
case_dst_out_vs_pixel_loop_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-xfermode-porterduff4-lab/explore-tutorial-xfermode-porterduff4.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-xfermode-porterduff4.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1983.zip