#鸿蒙通关秘籍#如何实现鸿蒙手写绘制功能?

HarmonyOS
8h前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
IDE红尘旧梦

在鸿蒙开发中,通过自定义NodeControllerRenderNode实现手写绘制功能。NodeController负责管理组件的节点和布局,而RenderNode负责自定义绘制内容。使用drawing.Path来记录手指移动路径,并在onTouch事件中根据手势更新路径:

ts export class MyNodeController extends NodeController { private rootNode: FrameNode | null = null; rootRenderNode: RenderNode | null = null; width: number = 0; height: number = 0;

makeNode(uiContext: UIContext): FrameNode { this.rootNode = new FrameNode(uiContext); if (this.rootNode !== null) { this.rootRenderNode = this.rootNode.getRenderNode(); } return this.rootNode; }

aboutToResize(size: Size): void { this.width = size.width; this.height = size.height; if (this.rootRenderNode !== null) { this.rootRenderNode.backgroundColor = 0XFFFFFFFF; this.rootRenderNode.frame = { x: 0, y: 0, width: this.width, height: this.height }; } } }

export class MyRenderNode extends RenderNode { path: drawing.Path = new drawing.Path();

draw(context: DrawContext): void { const canvas = context.canvas; const pen = new drawing.Pen(); pen.setAntiAlias(true); const pen_color: common2D.Color = { alpha: 0xFF, red: 0x00, green: 0x00, blue: 0x00 }; pen.setColor(pen_color); pen.setDither(true); pen.setStrokeWidth(5); canvas.attachPen(pen); canvas.drawPath(this.path); } }

分享
微博
QQ
微信
回复
5h前
相关问题
鸿蒙如何实现位图绘制
9919浏览 • 1回复 待解决