介紹
本示例使用drawing庫的Pen和Path結合NodeContainer組件實現手寫繪制功能。手寫板上完成繪制后,通過調用image庫的packToFile和packing接口將手寫板的繪制內容保存為圖片,并將圖片文件保存在應用沙箱路徑中。
效果圖預覽
使用說明
- 在虛線區域手寫繪制,點擊撤銷按鈕撤銷前一筆繪制,點擊重置按鈕清空繪制。
- 點擊packToFile保存圖片按鈕和packing保存圖片按鈕可以將繪制內容保存為圖片寫入文件,顯示圖片的沙箱路徑。
實現思路
- 創建NodeController的子類MyNodeController,用于獲取根節點的RenderNode和綁定的NodeContainer組件寬高。
export class MyNodeController extends NodeController {
private rootNode: FrameNode | null = null; // 根節點
rootRenderNode: RenderNode | null = null; // 從NodeController根節點獲取的RenderNode,用于添加和刪除新創建的MyRenderNode實例
width: number = 0; // 實例綁定的NodeContainer組件的寬,單位px
height: number = 0; // 實例綁定的NodeContainer組件的寬,單位px
// MyNodeController實例綁定的NodeContainer創建時觸發,創建根節點rootNode并將其掛載至NodeContainer
makeNode(uiContext: UIContext): FrameNode {
this.rootNode = new FrameNode(uiContext);
if (this.rootNode !== null) {
this.rootRenderNode = this.rootNode.getRenderNode();
}
return this.rootNode;
}
// 綁定的NodeContainer布局時觸發,獲取NodeContainer的寬高
aboutToResize(size: Size): void {
this.width = size.width;
this.height = size.height;
// 設置畫布底色為白色
if (this.rootRenderNode !== null) {
// NodeContainer布局完成后設置rootRenderNode的背景色為白色
this.rootRenderNode.backgroundColor = 0XFFFFFFFF;
// rootRenderNode的位置從組件NodeContainer的左上角(0,0)坐標開始,大小為NodeContainer的寬高
this.rootRenderNode.frame = { x: 0, y: 0, width: this.width, height: this.height };
}
}
}
- 創建RenderNode的子類MyRenderNode,初始化畫筆和繪制path路徑。
export class MyRenderNode extends RenderNode {
path: drawing.Path = new drawing.Path(); // 新建路徑對象,用于繪制手指移動軌跡
// RenderNode進行繪制時會調用draw方法,初始化畫筆和繪制路徑
draw(context: DrawContext): void {
const canvas = context.canvas;
// 創建一個畫筆Pen對象,Pen對象用于形狀的邊框線繪制
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);
// 設置畫筆的線寬為5px
pen.setStrokeWidth(5);
// 將Pen畫筆設置到canvas中
canvas.attachPen(pen);
// 繪制path
canvas.drawPath(this.path);
}
}
- 創建變量currentNode用于存儲當前正在繪制的節點,變量nodeCount用來記錄已掛載的節點數量。
private currentNode: MyRenderNode | null = null; // 當前正在繪制的節點
private nodeCount: number = 0; // 已掛載到根節點的子節點數量
- 創建自定義節點容器組件NodeContainer,接收MyNodeController的實例,將自定義的渲染節點掛載到組件上,實現自定義繪制。
NodeContainer(this.myNodeController)
.width('100%')
.height($r('app.integer.hand_writing_canvas_height'))
.onTouch((event: TouchEvent) => {
this.onTouchEvent(event);
})
.id(NODE_CONTAINER_ID)
- 在NodeContainer組件的onTouch回調函數中,手指按下創建新的節點并掛載到rootRenderNode,nodeCount加一,手指移動更新節點中的path對象,繪制移動軌跡,并將節點重新渲染。
onTouchEvent(event: TouchEvent): void {
// TODO:知識點:在手指按下時創建新的MyRenderNode對象,掛載到rootRenderNode上,手指移動時根據觸摸點坐標繪制線條,并重新渲染節點
// 獲取手指觸摸位置的坐標點
const positionX: number = vp2px(event.touches[0].x);
const positionY: number = vp2px(event.touches[0].y);
logger.info(TAG, `Touch positionX: ${positionX}, Touch positionY: ${positionY}`);
switch (event.type) {
case TouchType.Down: {
// 每次手指按下,創建一個MyRenderNode對象,用于記錄和繪制手指移動的軌跡
const newNode = new MyRenderNode();
// 定義newNode的大小和位置,位置從組件NodeContainer的左上角(0,0)坐標開始,大小為NodeContainer的寬高
newNode.frame = { x: 0, y: 0, width: this.myNodeController.width, height: this.myNodeController.height };
this.currentNode = newNode;
// 移動新節點中的路徑path到手指按下的坐標點
this.currentNode.path.moveTo(positionX, positionY);
if (this.myNodeController.rootRenderNode !== null) {
// appendChild在renderNode最后一個子節點后添加新的子節點
this.myNodeController.rootRenderNode.appendChild(this.currentNode);
// 已掛載的節點數量加一
this.nodeCount++;
}
break;
}
case TouchType.Move: {
if (this.currentNode !== null) {
// 手指移動,繪制移動軌跡
this.currentNode.path.lineTo(positionX, positionY);
// 節點的path更新后需要調用invalidate()方法觸發重新渲染
this.currentNode.invalidate();
}
break;
}
case TouchType.Up: {
// 手指抬起,釋放this.currentNode
this.currentNode = null;
}
default: {
break;
}
}
}
- rootRenderNode調用getChild方法獲取最后一個掛載的子節點,再使用removeChild方法移除,實現撤銷上一筆的效果。
goBack() {
if (this.myNodeController.rootRenderNode !== null && this.nodeCount > 0) {
// getChild獲取最后掛載的子節點
const node = this.myNodeController.rootRenderNode.getChild(this.nodeCount - 1);
// removeChild移除指定子節點
this.myNodeController.rootRenderNode.removeChild(node);
this.nodeCount--;
}
}
- 使用clearChildren清除當前rootRenderNode的所有子節點,實現畫布重置,nodeCount清零。
resetCanvas() {
if (this.myNodeController.rootRenderNode !== null && this.nodeCount > 0) {
// 清除當前rootRenderNode的所有子節點
this.myNodeController.rootRenderNode.clearChildren();
this.nodeCount = 0;
}
}
- 使用componentSnapshot.get獲取組件NodeContainer的PixelMap對象,用于保存圖片
componentSnapshot.get(NODE_CONTAINER_ID, async (error: Error, pixelMap: image.PixelMap) => {
if (pixelMap !== null) {
// 圖片寫入文件
this.filePath = await this.saveFile(getContext(), pixelMap);
logger.info(TAG, `Images saved using the packing method are located in : ${this.filePath}`);
}
})
- 使用image庫的packToFile()和packing()將獲取的PixelMap對象保存為圖片,并將圖片文件保存在應用沙箱路徑中。
- ImagePacker.packToFile()可直接將PixelMap對象寫入為圖片。
async packToFile(context: Context, pixelMap: PixelMap): Promise<string> {
// 創建圖像編碼ImagePacker對象
const imagePackerApi = image.createImagePacker();
// 設置編碼輸出流和編碼參數。format為圖像的編碼格式;quality為圖像質量,范圍從0-100,100為最佳質量
const options: image.PackingOption = { format: "image/jpeg", quality: 100 };
// 圖片寫入的沙箱路徑
const filePath: string = `${context.filesDir}/${getTimeStr()}.jpg`;
const file: fs.File = await fs.open(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
// 使用packToFile直接將pixelMap寫入文件
await imagePackerApi.packToFile(pixelMap, file.fd, options);
fs.closeSync(file);
return filePath;
}
- ImagePacker.packing()可獲取圖片的ArrayBuffer數據,再使用fs將數據寫入為圖片。
async saveFile(context: Context, pixelMap: PixelMap): Promise<string> {
// 創建圖像編碼ImagePacker對象
const imagePackerApi = image.createImagePacker();
// 設置編碼輸出流和編碼參數。format為圖像的編碼格式;quality為圖像質量,范圍從0-100,100為最佳質量
const options: image.PackingOption = { format: "image/jpeg", quality: 100 };
// 圖片寫入的沙箱路徑
const filePath: string = `${context.filesDir}/${getTimeStr()}.jpg`;
const file: fs.File = await fs.open(filePath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 使用packing打包獲取圖片的ArrayBuffer
const data: ArrayBuffer = await imagePackerApi.packing(pixelMap, options);
// 將圖片的ArrayBuffer數據寫入文件
fs.writeSync(file.fd, data);
fs.closeSync(file);
return filePath;
}
高性能知識點
不涉及
工程結構&模塊類型
handwritingtoimage // har類型
|---/src/main/ets/model
| |---RenderNodeModel.ets // 模型層-節點數據模型
|---/src/main/ets/view
| |---HandWritingToImage.ets // 視圖層-手寫板場景頁面
寫在最后
如果你覺得這篇內容對你還蠻有幫助,我想邀請你幫我三個小忙:
- 點贊,轉發,有你們的 『點贊和評論』,才是我創造的動力。
- 關注小編,同時可以期待后續文章ing??,不定期分享原創知識。
- 想要獲取更多完整鴻蒙最新學習知識點,請移步前往小編:
https://gitee.com/MNxiaona/733GH/blob/master/jianshu