在线代码实验室
运行离线 PointEvaluator/ColorEvaluator 估值器、四类 TimeInterpolator 数学模型与 ViewPropertyAnimator 核验
"""Offline Android animation verifier for Ch 131: TypeEvaluator, TimeInterpolator & ViewPropertyAnimator."""
from dataclasses import dataclass
import json
import math
VERSION = "CPython 3.12.14 offline Android graphics/animation/media/service verifier (Android SDK/emulator absent)"
@dataclass(frozen=True)
class Point:
x: float
y: float
def eval_point(fraction: float, start: Point, end: Point) -> list[float]:
return [
round(start.x + fraction * (end.x - start.x), 3),
round(start.y + fraction * (end.y - start.y), 3),
]
def eval_color_argb(fraction: float, start_argb: int, end_argb: int) -> str:
sa, sr, sg, sb = (start_argb >> 24) & 0xFF, (start_argb >> 16) & 0xFF, (start_argb >> 8) & 0xFF, start_argb & 0xFF
ea, er, eg, eb = (end_argb >> 24) & 0xFF, (end_argb >> 16) & 0xFF, (end_argb >> 8) & 0xFF, end_argb & 0xFF
a = int(sa + fraction * (ea - sa))
r = int(sr + fraction * (er - sr))
g = int(sg + fraction * (eg - sg))
b = int(sb + fraction * (eb - sb))
return f"#{a:02X}{r:02X}{g:02X}{b:02X}"
def bounce(t: float) -> float:
return t * t * 8.0
def bounce_interpolator(t: float) -> float:
# AOSP android.view.animation.BounceInterpolator
t *= 1.1226
if t < 0.3535:
return bounce(t)
if t < 0.7408:
return bounce(t - 0.54719) + 0.7
if t < 0.9644:
return bounce(t - 0.8526) + 0.9
return bounce(t - 1.0435) + 0.95
def accel_decel(t: float) -> float:
return math.cos((t + 1.0) * math.pi) / 2.0 + 0.5
def decel_accel_custom(t: float) -> float:
if t < 0.5:
return math.sin(t * math.pi) / 2.0
return 1.0 - math.sin(t * math.pi) / 2.0
class ViewPropertyAnimatorSim:
def __init__(self) -> None:
self.props: dict[str, float] = {}
self.duration_ms: int = 300
self.interpolator: str = "AccelerateDecelerateInterpolator"
self.auto_start_scheduled: bool = False
def alpha(self, val: float) -> "ViewPropertyAnimatorSim":
self.props["alpha"] = val
self.auto_start_scheduled = True
return self
def x(self, val: float) -> "ViewPropertyAnimatorSim":
self.props["x"] = val
self.auto_start_scheduled = True
return self
def y(self, val: float) -> "ViewPropertyAnimatorSim":
self.props["y"] = val
self.auto_start_scheduled = True
return self
def set_duration(self, ms: int) -> "ViewPropertyAnimatorSim":
self.duration_ms = ms
return self
def set_interpolator(self, name: str) -> "ViewPropertyAnimatorSim":
self.interpolator = name
return self
def main() -> None:
# Case 1: PointEvaluator on AnimView (RADIUS=80, 1080x1920 -> start(80,80), end(1000,1840))
start_pt = Point(80.0, 80.0)
end_pt = Point(1000.0, 1840.0)
c1_actual = {
"f_0_0": eval_point(0.0, start_pt, end_pt),
"f_0_25": eval_point(0.25, start_pt, end_pt),
"f_0_5": eval_point(0.5, start_pt, end_pt),
"f_1_0": eval_point(1.0, start_pt, end_pt),
}
c1_expected = {
"f_0_0": [80.0, 80.0],
"f_0_25": [310.0, 520.0],
"f_0_5": [540.0, 960.0],
"f_1_0": [1000.0, 1840.0],
}
# Case 2: ColorEvaluator ARGB linear interpolation from Color.BLUE (#FF0000FF) to Color.RED (#FFFF0000)
blue = 0xFF0000FF
red = 0xFFFF0000
c2_actual = {
"f_0_0": eval_color_argb(0.0, blue, red),
"f_0_5": eval_color_argb(0.5, blue, red),
"f_1_0": eval_color_argb(1.0, blue, red),
}
c2_expected = {
"f_0_0": "#FF0000FF",
"f_0_5": "#FF7F007F",
"f_1_0": "#FFFF0000",
}
# Case 3: TimeInterpolator formulas: Linear, Accelerate(2f), AccelerateDecelerate, Bounce, custom DecelerateAccelerate
c3_actual = {
"linear_0_5": 0.5,
"accel_factor2_0_5": round(0.5 ** (2 * 2.0), 4),
"accel_decel_samples": [round(accel_decel(t), 4) for t in (0.0, 0.25, 0.5, 0.75, 1.0)],
"bounce_samples": [round(bounce_interpolator(t), 4) for t in (0.0, 0.3, 0.5, 0.8, 1.0)],
"custom_decel_accel_samples": [round(decel_accel_custom(t), 4) for t in (0.0, 0.25, 0.5, 0.75, 1.0)],
}
c3_expected = {
"linear_0_5": 0.5,
"accel_factor2_0_5": 0.0625,
"accel_decel_samples": [0.0, 0.1464, 0.5, 0.8536, 1.0],
"bounce_samples": [0.0, 0.9074, 0.7016, 0.9165, 1.0001],
"custom_decel_accel_samples": [0.0, 0.3536, 0.5, 0.6464, 1.0],
}
# Case 4: ViewPropertyAnimator fluent chaining & auto-start
vpa = ViewPropertyAnimatorSim().x(500).y(500).set_duration(5000).set_interpolator("BounceInterpolator")
c4_actual = {
"props": vpa.props,
"duration_ms": vpa.duration_ms,
"interpolator": vpa.interpolator,
"auto_start_scheduled": vpa.auto_start_scheduled,
}
c4_expected = {
"props": {"x": 500, "y": 500},
"duration_ms": 5000,
"interpolator": "BounceInterpolator",
"auto_start_scheduled": True,
}
cases = [
{
"name": "point_type_evaluator_interpolation",
"summary": "核验自定义 PointEvaluator 在 RADIUS=80 起点 (80,80) 到终点 (1000,1840) 的线性坐标插值",
"expected": c1_expected,
"actual": c1_actual,
"passed": c1_actual == c1_expected,
},
{
"name": "color_type_evaluator_argb_blend",
"summary": "核验 ColorEvaluator 按 ARGB 四通道从 Color.BLUE 到 Color.RED 的整型截断插值",
"expected": c2_expected,
"actual": c2_actual,
"passed": c2_actual == c2_expected,
},
{
"name": "time_interpolator_math_models",
"summary": "核验 Accelerate(2f)、AccelerateDecelerate、Bounce 及自定义 DecelerateAccelerate 插值曲线",
"expected": c3_expected,
"actual": c3_actual,
"passed": c3_actual == c3_expected,
},
{
"name": "view_property_animator_fluent_chain",
"summary": "核验 ViewPropertyAnimator 连缀调用 x(500).y(500).setDuration(5000) 及自动启动标记",
"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-valueanimator2-lab/explore-tutorial-valueanimator2.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-tutorial-valueanimator2.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联156章) · 含当前章节或实例 · mbskycom202612a1829.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1966.zip