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

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

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

HarmonyOS
2025-01-09 13:59:45
451浏览
收藏 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%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
分享
微博
QQ
微信
回复
2025-01-09 16:07:02


相关问题
使用List lazyForeach时,reuseId生效
816浏览 • 1回复 待解决
HarmonyOS clearHistory生效
441浏览 • 1回复 待解决
HarmonyOS 文字fontFamily生效
701浏览 • 1回复 待解决
HarmonyOS SDK har包代码混淆生效
785浏览 • 1回复 待解决
点击事件,@State 页面生效,在线等
3622浏览 • 0回复 待解决
设置子窗口透明度生效
2220浏览 • 1回复 待解决
HarmonyOS 如何使用canvas绘制虚线
602浏览 • 1回复 待解决