feat: 大幅增强原生行为 — 修复P0 Bug + 补全核心API

P0 Bug 修复:
- Canvas.drawBitmap 重复定义导致重载丢失 → 合并为*args重载
- String.hashCode() 不可达的第二return → 修复为单return
- File.compareTo() 调用不存在的__cmp__ → 改为显式比较
- HashMap.remove() 逻辑错误 → 区分单参(返回旧值)/双参(返回boolean)

P1 核心行为增强:
- Handler: sendMessage/sendMessageDelayed 现在真正入队异步执行
- Looper: loop() 实现真正的消息循环(按when时间取出到期消息)
- AsyncTask: execute() 在后台线程池执行,完成后主线程回调
- Bundle: 新增12个数组类型put/get方法
- Math.round: 改为Java语义(floor(a+0.5))而非Python银行家舍入
- Thread: currentThread()缓存对象, 新增join/getState/getId/getPriority
- String: 新增strip/repeat/isBlank/lines/subSequence/getChars/chars/formatted/translateEscapes

P2 缺失API补全:
- 新增21个Java标准Exception类 (NullPointerException等)
- View: 新增39个事件处理/监听器/位置尺寸方法
- Activity: 新增33个生命周期/权限/UI方法
- Context: 新增22个文件流/权限/资源/主题方法
- Canvas: 支持 Canvas(Bitmap) 构造函数重载
- __init__.py: __all__ 补全Exception体系和Activity类
This commit is contained in:
shuiyu1123 committed 2026-06-24 15:04:17 +00:00
1 parent 4645015004
commit 8c3bd3c415
9 files changed
+1017 -52

No files matched your search

+54 -15
View File
@@ -2404,8 +2404,25 @@ class Canvas(Stub):
ALL_SAVE_FLAG = 0x1F
def __init__(self, width: int = 0, height: int = 0):
def __init__(self, width=0, height=0):
super().__init__()
# 支持 Canvas(Bitmap) 重载
if isinstance(width, Bitmap):
bitmap = width
self._bitmap = bitmap
self._width = bitmap.getWidth()
self._height = bitmap.getHeight()
if HAS_PIL:
from PIL import Image, ImageDraw
self._image = bitmap._image if hasattr(bitmap, '_image') and bitmap._image else None
self._draw = ImageDraw.Draw(self._image) if self._image else None
else:
self._pixels = None
self._save_count = 0
self._matrix_stack = [Matrix()]
self._clip_stack = [Rect(0, 0, self._width, self._height)]
self._matrix = Matrix()
return
if HAS_PIL:
from PIL import Image, ImageDraw
self._width = width
@@ -2685,21 +2702,43 @@ class Canvas(Stub):
except Exception:
pass
def drawBitmap(self, bitmap: Bitmap, left: float, top: float, paint: Paint = None):
if HAS_PIL and self._draw is not None and hasattr(bitmap, "_image") and bitmap._image:
self._image.paste(bitmap._image, (int(left), int(top)))
elif HAS_PIL and self._image is not None:
pass
def drawBitmap(self, bitmap: Bitmap, src: Rect, dst: RectF, paint: Paint = None):
if HAS_PIL and self._image is not None and hasattr(bitmap, "_image") and bitmap._image:
if src:
cropped = bitmap._image.crop((src.left, src.top, src.right, src.bottom))
resized = cropped.resize((int(dst.width()), int(dst.height())))
self._image.paste(resized, (int(dst.left), int(dst.top)))
def drawBitmap(self, *args):
"""drawBitmap 重载: (bitmap,left,top,paint) / (bitmap,src,dst,paint) / (bitmap,matrix,paint)"""
if len(args) == 4:
bitmap, a, b, paint = args
if isinstance(a, (int, float)) and isinstance(b, (int, float)):
if HAS_PIL and self._draw is not None and hasattr(bitmap, "_image") and bitmap._image:
self._image.paste(bitmap._image, (int(a), int(b)))
elif HAS_PIL and self._image is not None:
pass
else:
resized = bitmap._image.resize((int(dst.width()), int(dst.height())))
self._image.paste(resized, (int(dst.left), int(dst.top)))
src, dst = a, b
if HAS_PIL and self._image is not None and hasattr(bitmap, "_image") and bitmap._image:
if src:
cropped = bitmap._image.crop((src.left, src.top, src.right, src.bottom))
resized = cropped.resize((int(dst.width()), int(dst.height())))
self._image.paste(resized, (int(dst.left), int(dst.top)))
else:
resized = bitmap._image.resize((int(dst.width()), int(dst.height())))
self._image.paste(resized, (int(dst.left), int(dst.top)))
elif len(args) == 3:
bitmap, matrix, paint = args
if HAS_PIL and self._image is not None and hasattr(bitmap, "_image") and bitmap._image:
try:
from PIL import Image
inv = Matrix()
if hasattr(matrix, "invert") and matrix.invert(inv):
v = inv.getValues()
elif hasattr(matrix, "getValues"):
v = matrix.getValues()
else:
v = [1, 0, 0, 0, 1, 0, 0, 0, 1]
transformed = bitmap._image.transform(
bitmap._image.size, Image.AFFINE,
(v[0], v[1], v[2], v[3], v[4], v[5]))
self._image.paste(transformed, (0, 0))
except Exception:
pass
def drawText(self, text: str, x: float, y: float, paint: Paint):
if HAS_PIL and self._draw is not None: