"""Offline Android UI verifier for Ch 25: ImageView."""
import json
import xml.etree.ElementTree as ET
ANDROID_NS = "{http://schemas.android.com/apk/res/android}"
BITMAP_BG_XML = """<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@id/pen_bg"
android:gravity="top"
android:src="@drawable/pen"
android:tileMode="disabled" />
"""
def adjust_view_bounds(img_w: int, img_h: int, max_w: int, max_h: int) -> list:
scale = min(1.0, max_w / img_w, max_h / img_h)
return [int(round(img_w * scale)), int(round(img_h * scale))]
def compute_scale_type(mode: str, img_w: float, img_h: float, view_w: float, view_h: float) -> dict:
if mode == "fitXY":
return {"rect": [0.0, 0.0, view_w, view_h], "sx": round(view_w / img_w, 4), "sy": round(view_h / img_h, 4)}
if mode in ("fitStart", "fitCenter", "fitEnd"):
s = min(view_w / img_w, view_h / img_h)
dw, dh = img_w * s, img_h * s
if mode == "fitStart":
x, y = 0.0, 0.0
elif mode == "fitCenter":
x, y = (view_w - dw) / 2.0, (view_h - dh) / 2.0
else:
x, y = view_w - dw, view_h - dh
return {"rect": [round(x, 2), round(y, 2), round(x + dw, 2), round(y + dh, 2)], "sx": round(s, 4), "sy": round(s, 4)}
if mode == "center":
x, y = (view_w - img_w) / 2.0, (view_h - img_h) / 2.0
return {"rect": [round(x, 2), round(y, 2), round(x + img_w, 2), round(y + img_h, 2)], "sx": 1.0, "sy": 1.0}
if mode == "centerCrop":
s = max(view_w / img_w, view_h / img_h)
dw, dh = img_w * s, img_h * s
x, y = (view_w - dw) / 2.0, (view_h - dh) / 2.0
return {"rect": [round(x, 2), round(y, 2), round(x + dw, 2), round(y + dh, 2)], "sx": round(s, 4), "sy": round(s, 4)}
if mode == "centerInside":
s = 1.0 if (img_w <= view_w and img_h <= view_h) else min(view_w / img_w, view_h / img_h)
dw, dh = img_w * s, img_h * s
x, y = (view_w - dw) / 2.0, (view_h - dh) / 2.0
return {"rect": [round(x, 2), round(y, 2), round(x + dw, 2), round(y + dh, 2)], "sx": round(s, 4), "sy": round(s, 4)}
if mode == "matrix":
return {"rect": [0.0, 0.0, img_w, img_h], "sx": 1.0, "sy": 1.0}
raise ValueError(mode)
def main() -> None:
bmp = ET.fromstring(BITMAP_BG_XML)
c1_actual = {
"bitmap_tile_mode": bmp.attrib[f"{ANDROID_NS}tileMode"],
"bitmap_gravity": bmp.attrib[f"{ANDROID_NS}gravity"],
"set_alpha_affects": "src_only",
"background_fills_bounds": True,
"default_xml_scaletype": "fitCenter",
}
c1_expected = {
"bitmap_tile_mode": "disabled",
"bitmap_gravity": "top",
"set_alpha_affects": "src_only",
"background_fills_bounds": True,
"default_xml_scaletype": "fitCenter",
}
# Case 2: adjustViewBounds with maxWidth=200, maxHeight=200
c2_actual = {
"landscape_541x374": adjust_view_bounds(541, 374, 200, 200),
"portrait_374x541": adjust_view_bounds(374, 541, 200, 200),
"small_100x80_no_upscale": adjust_view_bounds(100, 80, 200, 200),
}
c2_expected = {
"landscape_541x374": [200, 138],
"portrait_374x541": [138, 200],
"small_100x80_no_upscale": [100, 80],
}
# Case 3: All 8 ScaleType matrix transforms on 400x200 image in 300x300 view + small 100x50 comparison
modes = ("fitXY", "fitStart", "fitCenter", "fitEnd", "center", "centerCrop", "centerInside", "matrix")
c3_actual = {m: compute_scale_type(m, 400.0, 200.0, 300.0, 300.0) for m in modes}
c3_actual["small_100x50_fitCenter_vs_centerInside"] = {
"fitCenter_scale": compute_scale_type("fitCenter", 100.0, 50.0, 300.0, 300.0)["sx"],
"centerInside_scale": compute_scale_type("centerInside", 100.0, 50.0, 300.0, 300.0)["sx"],
}
c3_expected = {
"fitXY": {"rect": [0.0, 0.0, 300.0, 300.0], "sx": 0.75, "sy": 1.5},
"fitStart": {"rect": [0.0, 0.0, 300.0, 150.0], "sx": 0.75, "sy": 0.75},
"fitCenter": {"rect": [0.0, 75.0, 300.0, 225.0], "sx": 0.75, "sy": 0.75},
"fitEnd": {"rect": [0.0, 150.0, 300.0, 300.0], "sx": 0.75, "sy": 0.75},
"center": {"rect": [-50.0, 50.0, 350.0, 250.0], "sx": 1.0, "sy": 1.0},
"centerCrop": {"rect": [-150.0, 0.0, 450.0, 300.0], "sx": 1.5, "sy": 1.5},
"centerInside": {"rect": [0.0, 75.0, 300.0, 225.0], "sx": 0.75, "sy": 0.75},
"matrix": {"rect": [0.0, 0.0, 400.0, 200.0], "sx": 1.0, "sy": 1.0},
"small_100x50_fitCenter_vs_centerInside": {"fitCenter_scale": 3.0, "centerInside_scale": 1.0},
}
# Case 4: RoundImageView clip circle geometry (square vs non-square)
def safe_circle(w: int, h: int) -> dict:
return {"cx": w / 2.0, "cy": h / 2.0, "r": min(w, h) / 2.0}
c4_actual = {
"square_200x200": safe_circle(200, 200),
"rect_200x100_safe": safe_circle(200, 100),
"rect_200x100_naive_cy_outside_center": 200 / 2.0 != 100 / 2.0,
}
c4_expected = {
"square_200x200": {"cx": 100.0, "cy": 100.0, "r": 100.0},
"rect_200x100_safe": {"cx": 100.0, "cy": 50.0, "r": 50.0},
"rect_200x100_naive_cy_outside_center": True,
}
cases = [
{
"name": "src_vs_background_and_bitmap_tilemode",
"summary": "核对 ImageView 的 src/background 渲染差异、bitmap tileMode=disabled 与 setAlpha 作用范围",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "adjust_view_bounds_aspect_clamp",
"summary": "计算 adjustViewBounds=true 配合 maxWidth/maxHeight=200px 时的等比约束尺寸",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "scaletype_eight_modes_matrix_math",
"summary": "精确求解 ImageView 全部 8 种 ScaleType 的缩放系数与目标绘制矩形,并对比小图下 fitCenter 与 centerInside",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "round_imageview_clip_geometry",
"summary": "验证自定义圆形 ImageView 的圆心与半径几何计算及非正方形容器的边界保护",
"expected": c4_expected,
"actual": c4_actual,
"passed": c4_actual == c4_expected,
},
]
print(json.dumps({
"version": "CPython 3.12.14 offline Android UI/XML/Adapter verifier (Android SDK/emulator absent)",
"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-imageview-lab/explore-tutorial-imageview.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-imageview.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1906.zip