HarmonyOS Path 是否支持矩阵变换

path: drawing.Path = new drawing.Path(); 是否支持矩阵变换?

其他平台中可以通过以下代码将 matrix 应用于 Path,我目前看文档HarmonyOS matrix4 只支持 Image? 能否支持 Path?

fun transformPath(path: Path, rectF: RectF, direction: Float) {
  val matrix = Matrix()
  matrix.setRotate(direction, rectF.centerX(), rectF.centerY())
  path.transform(matrix)
}
HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

已经有方案参考demo:

import matrix4 from '@ohos.matrix4'
import { RenderNode, DrawContext, FrameNode, NodeController } from "@ohos.arkui.node"
import drawing from "@ohos.graphics.drawing"

class MyFrameNode extends FrameNode {

  onDraw(context: DrawContext) {
    const canvas = context.canvas;
    const pen = new drawing.Pen();
    pen.setStrokeWidth(5);
    pen.setColor({alpha: 255, red: 255, green: 0, blue: 0});
    let path = new drawing.Path();
    path.arcTo(10, 10, 200, 200, 0, 270);
    canvas.attachPen(pen);
    canvas.drawPath(path);
    canvas.detachPen();
  }

}

class TextRenderNode extends RenderNode {
  async draw(context: DrawContext) {
    const canvas = context.canvas;
    const pen = new drawing.Pen();
    pen.setStrokeWidth(5);
    pen.setColor({alpha: 255, red: 255, green: 0, blue: 0});
    let path = new drawing.Path();
    path.arcTo(10, 10, 200, 200, 0, 270);
    canvas.attachPen(pen);
    canvas.drawPath(path);
    canvas.detachPen();
  }
}

class MyNodeController extends NodeController {
  private rootNode: FrameNode | null = null;
  private nodeArray: Array<RenderNode> = []
  addMyNode(item:RenderNode){
    this.nodeArray.push(item)
  }

  makeNode(uiContext: UIContext): FrameNode | null {
    this.rootNode = new FrameNode(uiContext);
    const renderNode = this.rootNode.getRenderNode();
    if (renderNode === null) {
      return this.rootNode;
    }
    if (renderNode) {
      renderNode.clearChildren()
      this.nodeArray.forEach((item) => {
        renderNode.appendChild(item)
      })
    }
    return this.rootNode;
  }
}

@Entry
@Component
struct Index {
  @State newNode:TextRenderNode = new TextRenderNode();
  @State nodeCV:MyNodeController = new MyNodeController()
  // frameNodeArr:Array<FrameNode> = []
  private matrix1 = matrix4.identity().setPolyToPoly({ src: [{x:0, y:0}, {x:vp2px(200), y:0}, {x:0, y:vp2px(200)}, {x:vp2px(200), y:vp2px(200)} ],
    dst:[{x:0, y:vp2px(50)}, {x:vp2px(200), y:0}, {x:0, y:vp2px(200)}, {x:vp2px(200), y:vp2px(200)}], pointCount:4})
  tempY = 0
  build() {
    Column() {
      Column() {
        NodeContainer(this.nodeCV)
          .width('100%')
          .height(200)
        // .transform(this.matrix1)
        Button('Invalidate')
          .onClick(() => {
            let frameNode = new MyFrameNode(this.getUIContext());
            // this.frameNodeArr.push(frameNode)
            let newNode = frameNode.getRenderNode()
            newNode!.frame = { x: 0, y: this.tempY, width: 200, height: 100 }
            newNode!.backgroundColor = Color.Red
            frameNode.commonAttribute.transform(this.matrix1)
            this.nodeCV.addMyNode(newNode!)
            this.nodeCV.rebuild()
            this.tempY += 100
          })
      }
      .width('100%')
      .height('100%')
    }
    .height('100%')
  }
}

可以使用自定义FrameNode,对每个FrameNode设置不同的矩阵变换

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS 矩阵变换问题demo
22浏览 • 1回复 待解决
仿射变换后列表滑动问题
315浏览 • 1回复 待解决
HarmonyOS 是否支持webrtc
419浏览 • 1回复 待解决
HarmonyOS websocket是否支持
50浏览 • 1回复 待解决
HarmonyOS 是否支持webrtc
491浏览 • 1回复 待解决
HarmonyOS 下载报错Download File Path Valid
882浏览 • 1回复 待解决
HarmonyOS window.setUIContent 参数path问题
31浏览 • 1回复 待解决
HarmonyOS 开发是否支持ProtoBuf
31浏览 • 1回复 待解决
HarmonyOS是否支持图表组件?
284浏览 • 1回复 待解决
HarmonyOS attributeModifier是否支持继承?
257浏览 • 1回复 待解决
HarmonyOS是否支持ping命令?
497浏览 • 1回复 待解决
HarmonyOS是否支持RTSP直播
410浏览 • 1回复 待解决
HarmonyOS ts是否支持反射?
199浏览 • 1回复 待解决
HarmonyOS 是否支持双录?
276浏览 • 1回复 待解决
HarmonyOS 目前是否支持cordova
794浏览 • 2回复 待解决
HarmonyOS是否支持多线程?
175浏览 • 0回复 待解决