""" Android graphics.drawable 扩展 — ShapeDrawable, GradientDrawable, LayerDrawable, StateListDrawable, AnimatedVectorDrawable, RippleDrawable 等 方法签名完全对齐 Android SDK """ from __future__ import annotations import sys from typing import Any, Dict, List, Optional, Tuple from .._core import Stub, AndroidOnlyError from ..java.lang import RuntimeException IllegalArgumentException = type("IllegalArgumentException", (RuntimeException,), {}) __all__ = [ "ShapeDrawable", "PaintDrawable", "GradientDrawable", "StateListDrawable", "LayerDrawable", "LevelListDrawable", "ScaleDrawable", "ClipDrawable", "RotateDrawable", "TransitionDrawable", "AnimatedStateListDrawable", "AnimatedImageDrawable", "AnimatedVectorDrawable", "RippleDrawable", "AnimatedRotateDrawable", "InsetDrawable", "NinePatchDrawable", ] # ═══════════════════════════════════════════════════════════════ # ShapeDrawable # ═══════════════════════════════════════════════════════════════ class ShapeDrawable(Stub): """ShapeDrawable — 形状可绘制对象 (桌面存根)""" def __init__(self, shape: Any = None): super().__init__() self._shape = shape self._paint = None self._shader_factory = None self._bounds = (0, 0, 0, 0) self._alpha = 255 def getShape(self) -> Any: """获取形状""" return self._shape def setShape(self, shape: Any) -> None: """设置形状""" self._shape = shape def getPaint(self) -> Any: """获取画笔""" return self._paint def setShaderFactory(self, factory: Any) -> None: """设置着色器工厂""" self._shader_factory = factory def getShaderFactory(self) -> Any: """获取着色器工厂""" return self._shader_factory def inflate(self, context: Any, attrs: Any) -> None: """从 XML 填充""" pass def setAlpha(self, alpha: int) -> None: """设置透明度""" self._alpha = alpha def getAlpha(self) -> int: """获取透明度""" return self._alpha def setBounds(self, left: int, top: int, right: int, bottom: int) -> None: """设置边界""" self._bounds = (left, top, right, bottom) def getIntrinsicWidth(self) -> int: """获取内部宽度""" if self._shape and hasattr(self._shape, 'getWidth'): return self._shape.getWidth() return 0 def getIntrinsicHeight(self) -> int: """获取内部高度""" if self._shape and hasattr(self._shape, 'getHeight'): return self._shape.getHeight() return 0 class PaintDrawable(ShapeDrawable): """PaintDrawable — 画笔可绘制对象""" def __init__(self, color: int = 0): super().__init__() self._color = color def setColor(self, color: int) -> None: """设置颜色""" self._color = color # ═══════════════════════════════════════════════════════════════ # GradientDrawable # ═══════════════════════════════════════════════════════════════ class GradientDrawable(ShapeDrawable): """GradientDrawable — 渐变可绘制对象""" RECTANGLE = 0 OVAL = 1 LINE = 2 RING = 3 LINEAR_GRADIENT = 0 RADIAL_GRADIENT = 1 SWEEP_GRADIENT = 2 def __init__(self, orientation: Any = None, colors: Optional[List[int]] = None): super().__init__() self._shape_type = GradientDrawable.RECTANGLE self._gradient_type = GradientDrawable.LINEAR_GRADIENT self._gradient_center = (0.5, 0.5) self._corner_radius = 0.0 self._corner_radii: Optional[List[float]] = None self._stroke_width = 0 self._stroke_color = 0 self._color = 0 self._colors = colors or [] self._orientation = orientation self._size = (0, 0) def setShape(self, shape_type: int) -> None: """设置形状类型""" self._shape_type = shape_type def setSize(self, width: int, height: int) -> None: """设置尺寸""" self._size = (width, height) def setGradientType(self, gradient_type: int) -> None: """设置渐变类型""" self._gradient_type = gradient_type def setGradientCenter(self, x: float, y: float) -> None: """设置渐变中心""" self._gradient_center = (x, y) def setCornerRadius(self, radius: float) -> None: """设置圆角半径""" self._corner_radius = radius def setCornerRadii(self, radii: List[float]) -> None: """设置圆角半径组""" self._corner_radii = radii def setStroke(self, width: int, color: int, dash_width: float = 0.0, dash_gap: float = 0.0) -> None: """设置描边""" self._stroke_width = width self._stroke_color = color def setColor(self, color: int) -> None: """设置颜色""" self._color = color def getColor(self) -> int: """获取颜色""" return self._color def setColors(self, colors: List[int]) -> None: """设置颜色数组""" self._colors = colors def getOrientation(self) -> Any: """获取方向""" return self._orientation def getIntrinsicWidth(self) -> int: return self._size[0] def getIntrinsicHeight(self) -> int: return self._size[1] # ═══════════════════════════════════════════════════════════════ # StateListDrawable # ═══════════════════════════════════════════════════════════════ class StateListDrawable(ShapeDrawable): """StateListDrawable — 状态列表可绘制对象""" def __init__(self): super().__init__() self._states: List[Tuple[List[int], Any]] = [] self._enter_fade_duration = 0 self._exit_fade_duration = 0 def addState(self, state_set: List[int], drawable: Any) -> None: """添加状态""" self._states.append((state_set, drawable)) def setState(self, state_set: List[int]) -> bool: """设置当前状态""" return True def getStateCount(self) -> int: """获取状态数量""" return len(self._states) def getStateSet(self, index: int) -> List[int]: """获取指定索引的状态集""" if 0 <= index < len(self._states): return self._states[index][0] return [] def getStateDrawable(self, index: int) -> Any: """获取指定索引的可绘制对象""" if 0 <= index < len(self._states): return self._states[index][1] return None def setEnterFadeDuration(self, duration: int) -> None: """设置进入淡出时间""" self._enter_fade_duration = duration def setExitFadeDuration(self, duration: int) -> None: """设置退出淡出时间""" self._exit_fade_duration = duration # ═══════════════════════════════════════════════════════════════ # LayerDrawable # ═══════════════════════════════════════════════════════════════ class LayerDrawable(ShapeDrawable): """LayerDrawable — 图层可绘制对象""" def __init__(self, layers: Optional[List[Any]] = None): super().__init__() self._layers: List[Any] = layers or [] self._layer_insets: Dict[int, Tuple[int, int, int, int]] = {} self._padding = (0, 0, 0, 0) self._id_map: Dict[int, int] = {} def setLayerInset(self, index: int, left: int, top: int, right: int, bottom: int) -> None: """设置图层嵌入""" self._layer_insets[index] = (left, top, right, bottom) def setLayerInsetRelative(self, index: int, start: int, top: int, end: int, bottom: int) -> None: """设置相对图层嵌入""" self._layer_insets[index] = (start, top, end, bottom) def setDrawable(self, index: int, drawable: Any) -> None: """设置可绘制对象""" while len(self._layers) <= index: self._layers.append(None) self._layers[index] = drawable def setDrawableByLayerId(self, id_: int, drawable: Any) -> bool: """按图层 ID 设置可绘制对象""" if id_ in self._id_map: idx = self._id_map[id_] self._layers[idx] = drawable return True return False def findDrawableByLayerId(self, id_: int) -> Any: """按图层 ID 查找可绘制对象""" if id_ in self._id_map: idx = self._id_map[id_] if 0 <= idx < len(self._layers): return self._layers[idx] return None def getLayerCount(self) -> int: """获取图层数量""" return len(self._layers) def getNumberOfLayers(self) -> int: """获取图层数量""" return len(self._layers) def getId(self, index: int) -> int: """获取图层 ID""" for id_, idx in self._id_map.items(): if idx == index: return id_ return 0 def setPadding(self, left: int, top: int, right: int, bottom: int) -> None: """设置内边距""" self._padding = (left, top, right, bottom) def setLayerGravity(self, index: int, gravity: int) -> None: """设置图层重力""" pass def setLayerWidth(self, index: int, width: int) -> None: """设置图层宽度""" pass def setLayerHeight(self, index: int, height: int) -> None: """设置图层高度""" pass def setLayerInsetLeft(self, index: int, left: int) -> None: if index in self._layer_insets: l, t, r, b = self._layer_insets[index] self._layer_insets[index] = (left, t, r, b) else: self._layer_insets[index] = (left, 0, 0, 0) def setLayerInsetRight(self, index: int, right: int) -> None: if index in self._layer_insets: l, t, r, b = self._layer_insets[index] self._layer_insets[index] = (l, t, right, b) else: self._layer_insets[index] = (0, 0, right, 0) def setLayerInsetTop(self, index: int, top: int) -> None: if index in self._layer_insets: l, t, r, b = self._layer_insets[index] self._layer_insets[index] = (l, top, r, b) else: self._layer_insets[index] = (0, top, 0, 0) def setLayerInsetBottom(self, index: int, bottom: int) -> None: if index in self._layer_insets: l, t, r, b = self._layer_insets[index] self._layer_insets[index] = (l, t, r, bottom) else: self._layer_insets[index] = (0, 0, 0, bottom) def setLayerInsetStart(self, index: int, start: int) -> None: self.setLayerInsetLeft(index, start) def setLayerInsetEnd(self, index: int, end: int) -> None: self.setLayerInsetRight(index, end) def mutate(self) -> "LayerDrawable": """变异""" return self class LevelListDrawable(StateListDrawable): """LevelListDrawable — 级别列表可绘制对象""" def __init__(self): super().__init__() # ═══════════════════════════════════════════════════════════════ # ScaleDrawable / ClipDrawable / RotateDrawable # ═══════════════════════════════════════════════════════════════ class ScaleDrawable(ShapeDrawable): """ScaleDrawable — 缩放可绘制对象""" def __init__(self, drawable: Any = None, gravity: int = 0, scale_width: float = 1.0, scale_height: float = 1.0): super().__init__() self._drawable = drawable self._gravity = gravity self._scale_width = scale_width self._scale_height = scale_height self._level = 0 def setScaleWidth(self, scale_width: float) -> None: self._scale_width = scale_width def setScaleHeight(self, scale_height: float) -> None: self._scale_height = scale_height def getScale(self) -> Tuple[float, float]: return (self._scale_width, self._scale_height) def getLevel(self) -> int: return self._level class ClipDrawable(ShapeDrawable): """ClipDrawable — 裁剪可绘制对象""" HORIZONTAL = 1 VERTICAL = 2 def __init__(self, drawable: Any = None, gravity: int = 0, orientation: int = HORIZONTAL): super().__init__() self._drawable = drawable self._gravity = gravity self._orientation = orientation class RotateDrawable(ShapeDrawable): """RotateDrawable — 旋转可绘制对象""" def __init__(self, drawable: Any = None, gravity: int = 0): super().__init__() self._drawable = drawable self._gravity = gravity self._rotation = 0.0 self._pivot_x = 0.5 self._pivot_y = 0.5 def setRotation(self, rotation: float) -> None: self._rotation = rotation # ═══════════════════════════════════════════════════════════════ # TransitionDrawable # ═══════════════════════════════════════════════════════════════ class TransitionDrawable(LayerDrawable): """TransitionDrawable — 过渡可绘制对象""" def __init__(self, layers: Optional[List[Any]] = None): super().__init__(layers) self._cross_fade_enabled = False def startTransition(self, duration_ms: int) -> None: """开始过渡""" pass def resetTransition(self) -> None: """重置过渡""" pass def reverseTransition(self, duration_ms: int) -> None: """反向过渡""" pass def crossFadeEnabled(self) -> bool: """检查是否启用交叉淡入""" return self._cross_fade_enabled def setCrossFadeEnabled(self, enabled: bool) -> None: """设置交叉淡入""" self._cross_fade_enabled = enabled # ═══════════════════════════════════════════════════════════════ # AnimatedStateListDrawable # ═══════════════════════════════════════════════════════════════ class AnimatedStateListDrawable(StateListDrawable): """AnimatedStateListDrawable — 动画状态列表可绘制对象""" def __init__(self): super().__init__() self._transitions: Dict[Tuple[int, int], Any] = {} def addState(self, state_set: List[int], drawable: Any, state_id: int) -> None: """添加状态""" self._states.append((state_set, drawable)) def addTransition(self, from_id: int, to_id: int, transition: Any) -> None: """添加过渡""" self._transitions[(from_id, to_id)] = transition # ═══════════════════════════════════════════════════════════════ # AnimatedImageDrawable # ═══════════════════════════════════════════════════════════════ class AnimatedImageDrawable(ShapeDrawable): """AnimatedImageDrawable — 动画图像可绘制对象""" LOOP_FINITE = 0 LOOP_INFINITE = -1 INFINITE = -1 def __init__(self): super().__init__() self._running = False self._repeat_count = 0 self._loop_behavior = AnimatedImageDrawable.LOOP_FINITE def start(self) -> None: """开始动画""" self._running = True def stop(self) -> None: """停止动画""" self._running = False def isRunning(self) -> bool: """检查是否正在运行""" return self._running def setRepeatCount(self, count: int) -> None: """设置重复次数""" self._repeat_count = count def getRepeatCount(self) -> int: """获取重复次数""" return self._repeat_count def setLoopBehavior(self, behavior: int) -> None: """设置循环行为""" self._loop_behavior = behavior def getLoopBehavior(self) -> int: """获取循环行为""" return self._loop_behavior # ═══════════════════════════════════════════════════════════════ # AnimatedVectorDrawable # ═══════════════════════════════════════════════════════════════ class AnimatedVectorDrawable(ShapeDrawable): """AnimatedVectorDrawable — 动画矢量可绘制对象""" def __init__(self): super().__init__() self._running = False self._alpha = 255 self._hotspot = (0, 0) self._hotspot_bounds = (0, 0, 0, 0) def start(self) -> None: """开始动画""" self._running = True def stop(self) -> None: """停止动画""" self._running = False def reset(self) -> None: """重置动画""" self._running = False def isRunning(self) -> bool: """检查是否正在运行""" return self._running def setVisible(self, visible: bool, restart: bool) -> bool: """设置可见性""" return True def setAlpha(self, alpha: int) -> None: """设置透明度""" self._alpha = alpha def getAlpha(self) -> int: """获取透明度""" return self._alpha def setHotspot(self, x: float, y: float) -> None: """设置热点""" self._hotspot = (x, y) def setHotspotBounds(self, left: int, top: int, right: int, bottom: int) -> None: """设置热点边界""" self._hotspot_bounds = (left, top, right, bottom) def getIntrinsicWidth(self) -> int: return 48 def getIntrinsicHeight(self) -> int: return 48 # ═══════════════════════════════════════════════════════════════ # RippleDrawable # ═══════════════════════════════════════════════════════════════ class RippleDrawable(ShapeDrawable): """RippleDrawable — 水波纹可绘制对象""" RIPPLE_ENTERING = 0 RIPPLE_EXITING = 1 def __init__(self, color: int = 0, content: Any = None, mask: Any = None): super().__init__() self._radius = 0 self._color = color self._effect_color = color self._content = content self._mask = mask self._ripple_state = RippleDrawable.RIPPLE_ENTERING def setRadius(self, radius: int) -> None: """设置半径""" self._radius = radius def getRadius(self) -> int: """获取半径""" return self._radius def setColor(self, color: int) -> None: """设置颜色""" self._color = color def getColor(self) -> int: """获取颜色""" return self._color def setEffectColor(self, color: int) -> None: """设置效果颜色""" self._effect_color = color def getEffectColor(self) -> int: """获取效果颜色""" return self._effect_color # ═══════════════════════════════════════════════════════════════ # AnimatedRotateDrawable / InsetDrawable / NinePatchDrawable # ═══════════════════════════════════════════════════════════════ class AnimatedRotateDrawable(ShapeDrawable): """AnimatedRotateDrawable — 动画旋转可绘制对象""" def __init__(self, drawable: Any = None, gravity: int = 0): super().__init__() self._drawable = drawable self._gravity = gravity self._running = False def start(self) -> None: self._running = True def stop(self) -> None: self._running = False class InsetDrawable(ShapeDrawable): """InsetDrawable — 嵌入可绘制对象""" def __init__(self, drawable: Any = None, inset: int = 0): super().__init__() self._drawable = drawable self._inset = inset self._outset = inset def getInset(self) -> int: """获取嵌入""" return self._inset def getOutset(self) -> int: """获取外延""" return self._outset class NinePatchDrawable(ShapeDrawable): """NinePatchDrawable — 九宫格可绘制对象""" def __init__(self, nine_patch: Any = None): super().__init__() self._nine_patch = nine_patch def getIntrinsicWidth(self) -> int: return 0 def getIntrinsicHeight(self) -> int: return 0