HarmonyOS pixelMap值怎么在父子组件之间同步

HarmonyOS
2024-12-20 16:48:06
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu
@Observed
class Params {
  imagePixelMap: image.PixelMap | undefined;

  constructor(imaagePixelMap: image.PixelMap | undefined) {
    this.imagePixelMap = imaagePixelMap;
  }
}
@Component
struct ObjectLinkChild {
  @ObjectLink pixelMapParams: Params;
  build() {
    Column() {
      Image(this.pixelMapParams.imagePixelMap).width(100).height(100)
    }
  }
}
@Builder
function imageBuilder(params: Params) {
  ObjectLinkChild({ pixelMapParams: params })
}
@Entry
@Component
struct ImageExample {
  @State imagePixelMap: image.PixelMap | undefined = undefined;
  @State pixelMapParams: Params = new Params(this.imagePixelMap);
  private baseNode: MyNodeController = new MyNodeController(this.imagePixelMap);
  wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder<[Params]>(imageBuilder);
  async aboutToAppear() {
    this.imagePixelMap = await this.getPixmapFromMedia($r('app.media.app_icon'))
    let node = new BuilderNode<[Params]>(this.getUIContext());
    node.build(this.wrapBuilder, this.pixelMapParams);
    this.baseNode.setNode(node);
  }
  build() {
    Column() {
      Image(this.imagePixelMap).enableAnalyzer(true).width(200).height(200)
    }
  }
  private async getPixmapFromMedia(resource: Resource) {
    let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({
      bundleName: resource.bundleName,
      moduleName: resource.moduleName,
      id: resource.id
    })
    let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength))
    let createPixelMap: image.PixelMap =
      await imageSource.createPixelMap({ desiredPixelFormat: image.PixelMapFormat.RGBA_8888 })
    await imageSource.release()
    return createPixelMap
  }
}
class MyNodeController extends NodeController {
  private rootNode: BuilderNode<[Params]> | null = null;
  private pixelMap: image.PixelMap | undefined = undefined;
  private wrapBuilder: WrappedBuilder<[Params]> = wrapBuilder(imageBuilder);
  constructor(pixelMap: image.PixelMap | undefined) {
    super();
    this.pixelMap = pixelMap;
  }
  setNode(node: BuilderNode<[Params]> | null) {
    this.rootNode = node;
  }
  makeNode(uiContext: UIContext): FrameNode | null {
    if (this.rootNode === null) {
      this.rootNode = new BuilderNode(uiContext);
      this.rootNode.build(this.wrapBuilder, new Params(this.pixelMap));
    }
    return this.rootNode.getFrameNode();
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 20:17:37
相关问题
HarmonyOS PixelMap如何实现父子组件同步
769浏览 • 1回复 待解决
arkts父子组件组件怎么通信传啊?
6563浏览 • 1回复 待解决
HarmonyOS 父子组件问题
749浏览 • 1回复 待解决
HarmonyOS 父子组件之间的传参传递数组
1143浏览 • 1回复 待解决
ArkTS自定义组件如何父子间传
1386浏览 • 1回复 待解决
HarmonyOS 父子组件滑动冲突
756浏览 • 1回复 待解决
HarmonyOS 父子组件状态问题
661浏览 • 1回复 待解决
ArkTS中如何进行页面之间的传
727浏览 • 0回复 待解决
HarmonyOS page之间的相互传
474浏览 • 1回复 待解决
HarmonyOS navigation页面之间回传
823浏览 • 1回复 待解决