"""Offline verification lab for Android Ch 115: tutorial-xfermode-porterduff3."""
import json
import xml.etree.ElementTree as ET
VERSION = "CPython 3.12.14 offline Android socket/graphics/ColorMatrix verifier (Android SDK/emulator absent)"
ATTRS_XML = """<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CircleImageView">
<attr name="Radius" format="dimension"/>
<attr name="type">
<enum name="circle" value="0"/>
<enum name="round" value="1"/>
</attr>
</declare-styleable>
</resources>"""
LAYOUT_XML = """<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<com.jay.xfermodedemo1.CircleImageView android:layout_width="160dp" android:layout_height="240dp"
android:layout_margin="10dp" android:src="@mipmap/ic_bg_2" app:type="circle" />
<com.jay.xfermodedemo1.CircleImageView android:layout_width="160dp" android:layout_height="280dp"
android:layout_margin="10dp" android:src="@mipmap/ic_bg_1" app:Radius="30dp" app:type="round" />
</LinearLayout>"""
def dst_in(src_a: float, dst_argb):
da, dr, dg, db = [c / 255.0 for c in dst_argb]
out_a = da * src_a
if out_a <= 1e-9:
return [0, 0, 0, 0]
# Premultiplied dst_c * src_a, unpremultiplied = (da*c*src_a)/(da*src_a) = c
return [round(out_a * 255), dst_argb[1], dst_argb[2], dst_argb[3]]
def case_dst_in_circle_and_roundrect_mask_pixels():
img_px = [255, 64, 180, 220]
inside = dst_in(1.0, img_px)
outside = dst_in(0.0, img_px)
antialiased_edge = dst_in(0.5, img_px)
actual = {
"inside_mask_keeps_dst": inside,
"outside_mask_cleared": outside,
"edge_half_alpha_preserves_rgb": antialiased_edge,
}
expected = {
"inside_mask_keeps_dst": [255, 64, 180, 220],
"outside_mask_cleared": [0, 0, 0, 0],
"edge_half_alpha_preserves_rgb": [128, 64, 180, 220],
}
return {
"name": "dst_in_circle_and_roundrect_mask_pixels",
"summary": "Verify PorterDuff.Mode.DST_IN keeps DST image inside mask and clears exterior",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_circle_imageview_measure_and_scale_math():
# Circle: 160x240 -> min(160, 240) = 160x160; drawable 320x480 -> scale = 160 / min(320, 480) = 0.5
circle_side = min(160, 240)
circle_scale = circle_side / min(320, 480)
circle_bounds = [0, 0, int(circle_scale * 320), int(circle_scale * 480)]
# Round: 160x280, drawable 400x200 -> max(160/400, 280/200) = max(0.4, 1.4) = 1.4
round_scale = max(160.0 / 400.0, 280.0 / 200.0)
round_bounds = [0, 0, int(round_scale * 400), int(round_scale * 200)]
actual = {
"circle_measured_size": [circle_side, circle_side],
"circle_scale": circle_scale,
"circle_drawable_bounds": circle_bounds,
"round_scale": round_scale,
"round_drawable_bounds": round_bounds,
}
expected = {
"circle_measured_size": [160, 160],
"circle_scale": 0.5,
"circle_drawable_bounds": [0, 0, 160, 240],
"round_scale": 1.4,
"round_drawable_bounds": [0, 0, 560, 280],
}
return {
"name": "circle_imageview_measure_and_scale_math",
"summary": "Verify CircleImageView onMeasure min clamping and circle/round scale-to-fill bounds",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_custom_attrs_and_layout_xml_parsing():
aroot = ET.fromstring(ATTRS_XML)
styleable = aroot.find("declare-styleable")
enums = {e.attrib["name"]: int(e.attrib["value"]) for e in styleable.findall(".//enum")}
lroot = ET.fromstring(LAYOUT_XML)
app_ns = "{http://schemas.android.com/apk/res-auto}"
views = list(lroot)
actual = {
"styleable_name": styleable.attrib["name"],
"type_enums": enums,
"view_types": [v.attrib[app_ns + "type"] for v in views],
"round_radius_attr": views[1].attrib[app_ns + "Radius"],
}
expected = {
"styleable_name": "CircleImageView",
"type_enums": {"circle": 0, "round": 1},
"view_types": ["circle", "round"],
"round_radius_attr": "30dp",
}
return {
"name": "custom_attrs_and_layout_xml_parsing",
"summary": "Verify declare-styleable CircleImageView custom attrs and layout XML bindings",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_weakreference_cache_and_invalidate_bug_check():
# Source bug: mWeakBitmap = null; if (mWeakBitmap != null) { mMaskBitmap.recycle(); }
mask_recycled_buggy = False
weak_ref = "cached_bitmap"
weak_ref = None
if weak_ref is not None:
mask_recycled_buggy = True
# Fixed order: check mMaskBitmap != null directly
mask_bitmap = "mask_bmp"
mask_recycled_fixed = False
if mask_bitmap is not None:
mask_recycled_fixed = True
mask_bitmap = None
actual = {
"source_invalidate_dead_branch_skips_recycle": not mask_recycled_buggy,
"fixed_invalidate_recycles_mask": mask_recycled_fixed and mask_bitmap is None,
}
expected = {
"source_invalidate_dead_branch_skips_recycle": True,
"fixed_invalidate_recycles_mask": True,
}
return {
"name": "weakreference_cache_and_invalidate_bug_check",
"summary": "Detect dead null-check bug in source invalidate() and verify corrected mMaskBitmap recycle",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_dst_in_circle_and_roundrect_mask_pixels(),
case_circle_imageview_measure_and_scale_math(),
case_custom_attrs_and_layout_xml_parsing(),
case_weakreference_cache_and_invalidate_bug_check(),
]
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-porterduff3-lab/explore-tutorial-xfermode-porterduff3.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-xfermode-porterduff3.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1982.zip