"""Offline verification lab for Android Ch 108: tutorial-bitmap1."""
import json
VERSION = "CPython 3.12.14 offline Android socket/graphics/ColorMatrix verifier (Android SDK/emulator absent)"
def case_bitmap_config_memory_and_density_scaling():
w, h = 1080, 1920
bpp = {"ARGB_8888": 4, "RGB_565": 2, "ARGB_4444": 2, "ALPHA_8": 1}
mem_bytes = {k: w * h * v for k, v in bpp.items()}
# inDensity=320 (xhdpi) -> inTargetDensity=480 (xxhdpi)
in_density, target_density = 320, 480
scaled_w = round(200 * target_density / in_density)
scaled_h = round(100 * target_density / in_density)
actual = {
"byte_counts_1080x1920": mem_bytes,
"density_scaled_dims": [scaled_w, scaled_h],
"insamplesize_4_area_ratio": 1.0 / (4 * 4),
}
expected = {
"byte_counts_1080x1920": {
"ARGB_8888": 8294400,
"RGB_565": 4147200,
"ARGB_4444": 4147200,
"ALPHA_8": 2073600,
},
"density_scaled_dims": [300, 150],
"insamplesize_4_area_ratio": 0.0625,
}
return {
"name": "bitmap_config_memory_and_density_scaling",
"summary": "Verify ARGB_8888/RGB_565/ARGB_4444/ALPHA_8 byte counts and inDensity->inTargetDensity scaling",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_bitmap_drawable_and_factory_decode_paths():
def bytes_to_bitmap(buf: bytes):
if len(buf) == 0:
return None
return {"decoded_bytes": len(buf)}
actual = {
"factory_methods": ["decodeResource", "decodeFile", "decodeByteArray", "decodeStream"],
"empty_bytes_returns_null": bytes_to_bitmap(b"") is None,
"nonempty_bytes_decoded": bytes_to_bitmap(b"\x89PNG\r\n\x1a\n"),
"private_constructor_jni_created": True,
}
expected = {
"factory_methods": ["decodeResource", "decodeFile", "decodeByteArray", "decodeStream"],
"empty_bytes_returns_null": True,
"nonempty_bytes_decoded": {"decoded_bytes": 8},
"private_constructor_jni_created": True,
}
return {
"name": "bitmap_drawable_and_factory_decode_paths",
"summary": "Verify BitmapFactory decode entrypoints and empty byte-array guard",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_bitmap_crop_and_scale_bounds():
src_w, src_h = 600, 800
crop_x, crop_y, crop_w, crop_h = 100, 100, 200, 200
crop_valid = (crop_x >= 0 and crop_y >= 0 and crop_x + crop_w <= src_w and crop_y + crop_h <= src_h)
invalid_crop = (500 + 200 <= src_w)
scaled_w, scaled_h = 300, 400
actual = {
"crop_valid": crop_valid,
"crop_rect": [crop_x, crop_y, crop_x + crop_w, crop_y + crop_h],
"out_of_bounds_crop_rejected": not invalid_crop,
"scaled_dims": [scaled_w, scaled_h],
"aspect_preserved": (scaled_w * src_h) == (scaled_h * src_w),
}
expected = {
"crop_valid": True,
"crop_rect": [100, 100, 300, 300],
"out_of_bounds_crop_rejected": True,
"scaled_dims": [300, 400],
"aspect_preserved": True,
}
return {
"name": "bitmap_crop_and_scale_bounds",
"summary": "Verify createBitmap(src, 100, 100, 200, 200) crop bounds and createScaledBitmap dimensions",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_decorview_screenshot_and_compress_contract():
decor_w, decor_h = 720, 1280
recycled = False
is_recycled_before = recycled
recycled = True
is_recycled_after = recycled
actual = {
"screenshot_argb4444_bytes": decor_w * decor_h * 2,
"screenshot_argb8888_bytes": decor_w * decor_h * 4,
"png_ignores_quality_setting": True,
"jpeg_quality_range": [0, 100],
"recycled_transition": [is_recycled_before, is_recycled_after],
}
expected = {
"screenshot_argb4444_bytes": 1843200,
"screenshot_argb8888_bytes": 3686400,
"png_ignores_quality_setting": True,
"jpeg_quality_range": [0, 100],
"recycled_transition": [False, True],
}
return {
"name": "decorview_screenshot_and_compress_contract",
"summary": "Verify DecorView screenshot Bitmap sizing, PNG/JPEG compress quality rules, and recycle state",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_bitmap_config_memory_and_density_scaling(),
case_bitmap_drawable_and_factory_decode_paths(),
case_bitmap_crop_and_scale_bounds(),
case_decorview_screenshot_and_compress_contract(),
]
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-bitmap1-lab/explore-tutorial-bitmap1.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-bitmap1.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1846.zip
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip