HarmonyOS 使用绘制模块的drawImage未生效

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-graphics-drawing-V5#drawimage

我在使用这个接口,绘制图片,在真机上图片未显示。绘制图形是ok的。

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

参考demo

import { image } from '@kit.ImageKit';
import { FrameNode, NodeController, RenderNode } from '@kit.ArkUI';

let resManager = getContext().resourceManager;
let pixmap: DrawableDescriptor = (resManager.getDrawableDescriptor($r('app.media.img').id)) as DrawableDescriptor;
let pixmapNew: image.PixelMap = pixmap.getPixelMap();
class MyRenderNode extends RenderNode {
  async draw(context: DrawContext): Promise<void> {
    const canvas = context.canvas;
    if(pixmapNew){
      console.info('test_test',`pixmapNew: ${JSON.stringify(pixmapNew)}`);
      canvas.drawImage(pixmapNew, 0, 0);
    }else{
      console.info('test_test',`pixmapNew undefined.`);
    }
  }
}

const newNode = new MyRenderNode();
newNode.frame = { x: 0, y: 0, width: 500, height: 500 };
newNode.borderRadius = { topLeft: 32, topRight: 32, bottomLeft: 32, bottomRight: 32 };

class MyNodeController extends NodeController {
  private rootNode: FrameNode | null = null;

  makeNode(uiContext: UIContext): FrameNode {
    this.rootNode = new FrameNode(uiContext);
    if (this.rootNode === null) {
      return this.rootNode;
    }
    const renderNode = this.rootNode.getRenderNode();
    if (renderNode !== null) {
      renderNode.frame = { x: 0, y: 0, width: 10, height: 500 };
      renderNode.pivot = { x: 50, y: 50 };
    }
    return this.rootNode;
  }

  addNode(node: RenderNode): void {
    if (this.rootNode === null) {
      return;
    }
    const renderNode = this.rootNode.getRenderNode();
    if (renderNode !== null) {
      renderNode.appendChild(node);
    }
  }

  clearNodes(): void {
    if (this.rootNode === null) {
      return;
    }
    const renderNode = this.rootNode.getRenderNode();
    if (renderNode !== null) {
      renderNode.clearChildren();
    }
  }
}

@Entry
@Component
struct DrawIconTest {
  private myNodeController: MyNodeController = new MyNodeController();
  onPageShow(): void {
  }
  build() {
    Row(){
      Column(){
        NodeContainer(this.myNodeController)
          .width('100%')
        Button('click to draw')
          .onClick(() => {
            this.myNodeController.clearNodes();
            this.myNodeController.addNode(newNode);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
使用List lazyForeach时,reuseId生效
454浏览 • 1回复 待解决
HarmonyOS clearHistory生效
29浏览 • 1回复 待解决
HarmonyOS 文字fontFamily生效
208浏览 • 1回复 待解决
HarmonyOS SDK har包代码混淆生效
201浏览 • 1回复 待解决
点击事件,@State 页面生效,在线等
3045浏览 • 0回复 待解决
设置子窗口透明度生效
1808浏览 • 1回复 待解决
HarmonyOS 如何使用canvas绘制虚线
142浏览 • 1回复 待解决