HarmonyOS ohos.graphics.text文本模块示例demo

HarmonyOS
2024-12-20 16:14:15
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang
NativeRender.ets 文件

import { NodeController, FrameNode, RenderNode, DrawContext, Size } from "@ohos.ArkUI.node"
import { UIContext } from '@ohos.ArkUI.UIContext';

const DefaultDrawFunction = (context: DrawContext) => {};
type DrawFunction = (context: DrawContext) => void;

class NativeRenderNode extends RenderNode {
  private drawFunction: DrawFunction;

  constructor(drawFunction: DrawFunction) {
    super();
    this.drawFunction = drawFunction;
  }

  draw(context: DrawContext) {
    this.drawFunction(context);
  }
}

class NativeNodeController extends NodeController {
  private rootNode: FrameNode | null = null;
  private renderNode: NativeRenderNode | null = null;
  private drawFunction: DrawFunction;

  constructor(drawFunction: DrawFunction) {
    super();
    this.drawFunction = drawFunction;
  }

  makeNode(uiContext: UIContext): FrameNode {
    this.renderNode = new NativeRenderNode(this.drawFunction);
    this.rootNode = new FrameNode(uiContext);
    const renderNode = this.rootNode?.getRenderNode();
    if (renderNode) {
      renderNode.frame = { x: 0, y: 0, width: 1000, height: 1000 };
      this.renderNode.frame = { x: 0, y: 0, width: 1000, height: 1000 };
      renderNode.appendChild(this.renderNode);
    }
    return this.rootNode;
  }

  aboutToResize(size: Size): void {
    const renderNode = this.rootNode?.getRenderNode();
    if (renderNode) {
      renderNode.frame = { x: 0, y: 0, width: size.width, height: size.height };
      if (this.renderNode) {
        this.renderNode.frame = { x: 0, y: 0, width: size.width, height: size.height };
      }
    }
    this.invalidate();
  }

  invalidate(): void {
    this.renderNode?.invalidate();
  }
}

@Component
struct NativeRenderComponent {
  drawFunction: DrawFunction = DefaultDrawFunction;
  @State controller: NativeNodeController | null = null;

  aboutToAppear(): void {
    this.controller = new NativeNodeController(this.drawFunction);
  }

  build() {
    NodeContainer(this.controller)
      .onTouch((event: TouchEvent) => {
        if (event.type === TouchType.Up) {
          this.controller?.invalidate();
        }
      })
  }
}

export default NativeRenderComponent;
 
Index.ets 文件
import { NodeController, FrameNode, RenderNode, DrawContext, Size, LengthMetrics } from "@ohos.ArkUI.node"
import NativeRenderComponent from './NativeRender';
import drawing from '@ohos.graphics.drawing';
import { common2D, text } from '@kit.ArkGraphics2D';

import { NodeController, FrameNode, RenderNode, DrawContext, Size, LengthMetrics } from "@ohos.ArkUI.node"
import NativeRenderComponent from './NativeRender';
import drawing from '@ohos.graphics.drawing';
import { common2D, text } from '@kit.ArkGraphics2D';

@Entry
@Component
struct Index {
  static fontName: string = "myFont";
  static fontCollection: text.FontCollection = LoadFont(Index.fontName, $rawfile('font/Yarndings20-Regular.ttf'));

  build() {
    Column({ space: 20 }) {
      NativeRenderComponent({drawFunction: (context: DrawContext) => {
        let style: text.TextStyle = {color: {alpha: 255, red: 255, green: 0, blue: 0}, fontFamilies: [Index.fontName], fontSize: 100};
        // DrawParagraph(context.canvas, 'ABCD', Index.fontCollection, style, 50, 50);

        const x = 50;
        const y = 200;
        DrawParagraph(context.canvas, '你好', Index.fontCollection, style, x, y);

      }})
        .backgroundColor(0x00ff00)
        .width(300)
        .height(400)
    }
    .height('100%')
  }
}

function LoadFont(name: string, fontSrc: string | Resource) {
  const fc = new text.FontCollection();
  fc.loadFontSync(name, fontSrc);
  return fc;
}

function DrawParagraph(canvas: drawing.Canvas, str: string, fontCollection: text.FontCollection, style: text.TextStyle, x: number, y: number): text.Paragraph {
  const builder = new text.ParagraphBuilder({}, fontCollection);
  builder.pushStyle(style);
  builder.addText(str);
  builder.popStyle();

  let paragraph = builder.build();
  paragraph.layoutSync(Number.POSITIVE_INFINITY);

  let width = paragraph.getLongestLine();
  let height = paragraph.getHeight();
  DrawRect(canvas, {alpha: 255, red: 255, green: 255, blue: 0}, {left: x, top: y, right: x + width, bottom: y + height});
  paragraph.paint(canvas, x, y);

  return paragraph;
}
  • 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.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
分享
微博
QQ
微信
回复
2024-12-20 19:22:45
相关问题
HarmonyOS 消息通知使用示例demo
1202浏览 • 1回复 待解决
HarmonyOS 请提供AVRecorder demo示例
898浏览 • 1回复 待解决
HarmonyOS 有没有FrameNode的示例demo
765浏览 • 1回复 待解决
HarmonyOS Text文本描边实现
774浏览 • 1回复 待解决
Text怎么设置文本渐变?
1215浏览 • 0回复 待解决
HarmonyOS Text组件富文本解析问题
812浏览 • 1回复 待解决
HarmonyOS 申请NFC读写卡的demo示例
832浏览 • 1回复 待解决
HarmonyOS Text获取文本显示的行数
1413浏览 • 1回复 待解决
HarmonyOS Text组件中,文本怎么换行
1219浏览 • 1回复 待解决
获取文本Text组件的宽度
1231浏览 • 1回复 待解决
ArkTS实现Text文本的【...展开】
2554浏览 • 3回复 待解决
HarmonyOS Text组件如何计算文本行数
731浏览 • 1回复 待解决
HarmonyOS @ohos.PiPWindow开启画中画示例
656浏览 • 1回复 待解决
HarmonyOS @ohos.zlib(Zip模块)相关问题
742浏览 • 1回复 待解决
HarmonyOS Text多行文本不能居中对齐
1870浏览 • 1回复 待解决
导入ohos相关模块都找不到
7423浏览 • 1回复 待解决