1# XComponent 2 3**XComponent** provides a surface for graphics rendering and media data input into your view. You can customize the position and size of the surface as needed. For details, see [Native XComponent](../../../ui/napi-xcomponent-guidelines.md). 4 5> **NOTE** 6> 7> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version. 8 9 10## Child Components 11Not supported 12 13## APIs 14 15### XComponent<sup>12+</sup> 16 17XComponent(options: XComponentOptions) 18 19**Atomic service API**: This API can be used in atomic services since API version 12. 20 21**System capability**: SystemCapability.ArkUI.ArkUI.Full 22 23**Parameters** 24 25| Name | Type | Mandatory| Description | 26| ------- | --------------------------------------- | ---- | ------------------------------ | 27| options | [XComponentOptions](#xcomponentoptions12) | Yes | Options of the **XComponent**.| 28 29### XComponent<sup>10+</sup> 30 31XComponent(value: {id: string, type: XComponentType, libraryname?: string, controller?: XComponentController}) 32 33This API is deprecated since API version 12. You are advised to use [XComponent(options: XComponentOptions)](#xcomponent12) instead. 34 35**Atomic service API**: This API can be used in atomic services since API version 12. 36 37**System capability**: SystemCapability.ArkUI.ArkUI.Full 38 39**Parameters** 40 41| Name | Type | Mandatory| Description | 42| ----------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 43| id | string | Yes | Unique ID of the component. The value can contain a maximum of 128 characters. | 44| type | [XComponentType](ts-appendix-enums.md#xcomponenttype10) | Yes | Type of the component. | 45| libraryname | string | No | Name of the dynamic library compiled and output by the native layer (the corresponding dynamic library does not support cross-module loading). This parameter is effective only when **type** is **SURFACE** or **TEXTURE**.| 46| controller | [XComponentController](#xcomponentcontroller) | No | Controller bound to the component, which can be used to invoke methods of the component. This parameter is effective only when **type** is **SURFACE** or **TEXTURE**.| 47 48### XComponent<sup>(deprecated)</sup> 49 50XComponent(value: {id: string, type: string, libraryname?: string, controller?: XComponentController}) 51 52> **NOTE** 53> 54> This API is deprecated since API version 12. You are advised to use [XComponent(options: XComponentOptions)](#xcomponent12) instead. 55 56**System capability**: SystemCapability.ArkUI.ArkUI.Full 57 58**Parameters** 59 60| Name | Type | Mandatory| Description | 61| ----------- | --------------------------------------------- | ---- | ------------------------------------------------------------ | 62| id | string | Yes | Unique ID of the component. The value can contain a maximum of 128 characters. | 63| type | string | Yes | Type of the component. The options are as follows:<br>- **"surface"**: The custom content is displayed individually on the screen. This option is used for displaying EGL/OpenGL ES and media data.<br>- **"component"**<sup>9+</sup>: The component acts a container where non-UI logic can be executed to dynamically load and display content.<br>Any other value is handled as **"surface"**.| 64| libraryname | string | No | Name of the dynamic library compiled and output by the native layer (the corresponding dynamic library does not support cross-module loading). This parameter is effective only when **type** is **"surface"**.| 65| controller | [XComponentcontroller](#xcomponentcontroller) | No | Controller bound to the component, which can be used to invoke methods of the component. This parameter is valid only when the component type is **"surface"**.| 66 67## XComponentOptions<sup>12+</sup> 68 69Defines the options of the **XComponent**. 70 71**Atomic service API**: This API can be used in atomic services since API version 12. 72 73**System capability**: SystemCapability.ArkUI.ArkUI.Full 74 75| Name| Type| Mandatory| Description| 76| -------- | -------- | -------- | -------- | 77| type | [XComponentType](ts-appendix-enums.md#xcomponenttype10) | Yes | Type of the component.| 78| controller | [XComponentController](#xcomponentcontroller) | Yes| Controller bound to the component, which can be used to invoke methods of the component. This parameter is valid only when **type** is **SURFACE** or **TEXTURE**.| 79| imageAIOptions | [ImageAIOptions](ts-image-common.md#imageaioptions) | No| AI image analysis options. You can configure the analysis type or bind an analyzer controller through this parameter.| 80 81 82## Attributes 83In addition to universal attributes, the following attributes are supported. 84 > 85 > **NOTE** 86 > 87 > The **foregroundColor**, **obscured**, and **pixelStretchEffect** attributes are not supported. When **type** is set to **SURFACE**, the following are not supported either: attribute modifier, custom drawing, background options (except **backgroundColor**), image effects (except **shadow**), **maskShape**, and **foregroundEffect** attributes. 88 > 89 > For the **XComponent** component of the TEXTURE or SURFACE type, if the [renderFit](./ts-universal-attributes-renderfit.md) attribute is not set, it defaults to **RenderFit.RESIZE_FILL**. 90 > 91 > For the **XComponent** component of the SURFACE type with an opaque black background, the [renderFit](./ts-universal-attributes-renderfit.md) attribute can only be set to **RenderFit.RESIZE_FILL**. Other **RenderFit** enum values are not recommended. 92 > 93 > For the **XComponent** component created using the [ArkUI NDK API](../../../ui/ndk-access-the-arkts-page.md), the [getAttribute](../_ark_u_i___native_node_a_p_i__1.md#getattribute) function is not supported for obtaining the **renderFit** attribute value. 94 95### enableAnalyzer<sup>12+</sup> 96 97enableAnalyzer(enable: boolean) 98 99Sets whether to enable the AI image analyzer, which supports subject recognition, text recognition, and object lookup. 100For the settings to take effect, this attribute must be used together with [StartImageAnalyzer](#startimageanalyzer12) and [StopImageAnalyzer](#stopimageanalyzer12) of **XComponentController**. 101This feature cannot be used together with the [overlay](ts-universal-attributes-overlay.md) attribute. If both are set, the **CustomBuilder** attribute in **overlay** has no effect. This feature also depends on device capabilities. 102 103**Atomic service API**: This API can be used in atomic services since API version 12. 104 105**System capability**: SystemCapability.ArkUI.ArkUI.Full 106 107**Parameters** 108 109| Name| Type| Mandatory| Description| 110| -------- | -------- | -------- | -------- | 111| enable | boolean | Yes| Whether to enable the AI image analyzer.| 112 113 > **NOTE** 114 > 115 > This API has effect only when **type** is set to **SURFACE** or **TEXTURE**. 116 117### enableSecure<sup>13+</sup> 118 119enableSecure(isSecure: boolean) 120 121Sets whether to enable the secure surface to protect the content rendered within the component from being captured or recorded. 122 123**Atomic service API**: This API can be used in atomic services since API version 13. 124 125**System capability**: SystemCapability.ArkUI.ArkUI.Full 126 127**Parameters** 128 129| Name | Type | Mandatory| Description | 130| -------- | ------- | ---- | ---------------------- | 131| isSecure | boolean | Yes | Whether to enable the secure surface.| 132 133 > **NOTE** 134 > 135 > This attribute is effective only when **type** is set to **SURFACE**. 136 > 137 > The **XComponent** created using the[ArkUI NDK APIs](../../../ui/ndk-build-ui-overview.md) is not supported. 138 139## Events 140 141Since API version 12, the [universal events](ts-universal-events-click.md) are supported when **type** is set to **SURFACE** or **TEXTURE**. 142 143> **NOTE** 144> 145> When the **libraryname** parameter is set, [click events](ts-universal-events-click.md), [touch events](ts-universal-events-touch.md), [show/hide events](ts-universal-events-show-hide.md), [key events](ts-universal-events-key.md), [focus events](ts-universal-focus-event.md), and [mouse events](ts-universal-mouse-key.md) only respond to event APIs on the C-API side. 146 147**Atomic service API**: This API can be used in atomic services since API version 12. 148 149The following events are effective only when **type** is set to **SURFACE** or **TEXTURE**. 150 151### onLoad 152 153onLoad(callback: OnNativeLoadCallback ) 154 155Triggered when the plug-in is loaded. 156 157**Atomic service API**: This API can be used in atomic services since API version 14. 158 159**System capability**: SystemCapability.ArkUI.ArkUI.Full 160 161**Parameters** 162 163| Name | Type | Mandatory | Description | 164| ----- | ------ | ---- | ---------------------------------------- | 165| callback | [OnNativeLoadCallback](#onnativeloadcallback14) | Yes | Callback after the surface held by the **XComponent** is created.| 166 167### onDestroy 168 169onDestroy(event: VoidCallback ) 170 171Triggered when the plug-in is destroyed. 172 173**Atomic service API**: This API can be used in atomic services since API version 14. 174 175**System capability**: SystemCapability.ArkUI.ArkUI.Full 176 177**Parameters** 178 179| Name | Type | Mandatory | Description | 180| ----- | ------ | ---- | ---------------------------------------- | 181| event | [VoidCallback](ts-types.md#voidcallback12) | Yes | Callback after the **XComponent** is destroyed.| 182 183## OnNativeLoadCallback<sup>14+</sup> 184 185type OnNativeLoadCallback = (event?: object) =\> void 186 187Triggered after the surface held by the **XComponent** is created. 188 189**Atomic service API**: This API can be used in atomic services since API version 14. 190 191**System capability**: SystemCapability.ArkUI.ArkUI.Full 192 193**Parameters** 194 195| Name | Type | Mandatory | Description | 196| ----- | ------ | ---- | ---------------------------------------- | 197| event | object | No | Context of the **XComponent** object. The APIs contained in the context are defined at the native layer by developers.| 198 199## XComponentController 200 201Defines the controller of the **XComponent**. You can bind the controller to the **XComponent** to call the component APIs through the controller. 202 203**Atomic service API**: This API can be used in atomic services since API version 12. 204 205**System capability**: SystemCapability.ArkUI.ArkUI.Full 206 207### Creating an Object 208 209``` 210xcomponentController: XComponentController = new XComponentController() 211``` 212 213**Atomic service API**: This API can be used in atomic services since API version 12. 214 215### constructor 216 217constructor() 218 219A constructor used to create a **XComponentController** instance. 220 221**Atomic service API**: This API can be used in atomic services since API version 12. 222 223**System capability**: SystemCapability.ArkUI.ArkUI.Full 224 225### getXComponentSurfaceId<sup>9+</sup> 226 227getXComponentSurfaceId(): string 228 229Obtains the ID of the surface held by the **XComponent**, which can then be used for @ohos APIs. For details, see [Camera Management](../../apis-camera-kit/js-apis-camera.md). This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. 230 231**Atomic service API**: This API can be used in atomic services since API version 12. 232 233**System capability**: SystemCapability.ArkUI.ArkUI.Full 234 235**Return value** 236 237| Type | Description | 238| ------ | ----------------------- | 239| string | ID of the surface held by the **XComponent**.| 240 241 242### setXComponentSurfaceSize<sup>(deprecated)</sup> 243 244setXComponentSurfaceSize(value: {surfaceWidth: number, surfaceHeight: number}): void 245 246Sets the width and height of the surface held by the **XComponent**. This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. 247 248This API is deprecated since API version 12. You are advised to use [setXComponentSurfaceRect](#setxcomponentsurfacerect12) instead. 249 250**System capability**: SystemCapability.ArkUI.ArkUI.Full 251 252**Parameters** 253 254| Name | Type | Mandatory | Description | 255| ------------- | ------ | ---- | ----------------------- | 256| surfaceWidth | number | Yes | Width of the surface held by the **XComponent**.| 257| surfaceHeight | number | Yes | Height of the surface held by the **XComponent**.| 258 259 260### getXComponentContext 261 262getXComponentContext(): Object 263 264Obtains the context of an **XComponent** object. This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. 265 266**Atomic service API**: This API can be used in atomic services since API version 12. 267 268**System capability**: SystemCapability.ArkUI.ArkUI.Full 269 270**Return value** 271 272| Type | Description | 273| ------ | ------------------------------------------------------------ | 274| Object | Context of the **XComponent** object. The APIs contained in the context are defined by developers. The context is passed in as the first parameter of the **onLoad** callback.| 275 276### setXComponentSurfaceRect<sup>12+</sup> 277 278setXComponentSurfaceRect(rect: SurfaceRect): void 279 280Sets the rectangle for the surface held by the **XComponent**. This API works only when type of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. 281 282**Atomic service API**: This API can be used in atomic services since API version 12. 283 284**System capability**: SystemCapability.ArkUI.ArkUI.Full 285 286**Parameters** 287 288| Name| Type | Mandatory| Description | 289| ------ | ------------------------------------ | ---- | --------------------------------- | 290| rect | [SurfaceRect](#surfacerect12) | Yes | Rectangle of the surface held by the **XComponent**.| 291 292> **NOTE** 293> 294> If **offsetX** or **offsetY** in the **rect** parameter is not set, the surface rectangle is centered along the x-axis or y-axis relative to the upper left corner of the **XComponent**. 295> 296> If **surfaceWidth** and **surfaceHeight** in the **rect** parameter are set to **0** or a negative value, this API has no effect. 297> 298> This API has a higher priority than attributes that can change the content offset and size, such as [border](ts-universal-attributes-border.md#border) and [padding](ts-universal-attributes-size.md#padding). 299 300### getXComponentSurfaceRect<sup>12+</sup> 301 302getXComponentSurfaceRect(): SurfaceRect 303 304Obtains the rectangle of the surface held by the **XComponent**. This API works only when type of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. 305 306**Atomic service API**: This API can be used in atomic services since API version 12. 307 308**System capability**: SystemCapability.ArkUI.ArkUI.Full 309 310**Return value** 311 312| Type | Description | 313| ------------------------------------ | ------------------------------------- | 314| [SurfaceRect](#surfacerect12) | Rectangle of the surface held by the **XComponent**.| 315 316### onSurfaceCreated<sup>12+</sup> 317 318onSurfaceCreated(surfaceId: string): void 319 320Triggered when the surface held by the **XComponent** is created. This API works only when **type** of the **XComponent** is set to **SURFACE("surface")** or **TEXTURE**. 321 322**Atomic service API**: This API can be used in atomic services since API version 12. 323 324**System capability**: SystemCapability.ArkUI.ArkUI.Full 325 326**Parameters** 327 328| Name | Type| Mandatory| Description | 329| --------- | -------- | ---- | ------------------------------------------------- | 330| surfaceId | string | Yes | ID of the surface held by the **XComponent**.| 331 332> **NOTE** 333> 334> The callback is triggered only when the **libraryname** parameter is not set for the **XComponent**. 335 336### onSurfaceChanged<sup>12+</sup> 337 338onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void 339 340Triggered when the surface held by the **XComponent** has its size changed (including the time when the **XComponent** is created with the specified size). This API works only when **type** of the **XComponent** is set to **SURFACE** (**"surface"**) or **TEXTURE**. 341 342**Atomic service API**: This API can be used in atomic services since API version 12. 343 344**System capability**: SystemCapability.ArkUI.ArkUI.Full 345 346**Parameters** 347 348| Name | Type | Mandatory| Description | 349| --------- | ------------------------------------- | ---- | ------------------------------------------------------- | 350| surfaceId | string | Yes | ID of the surface held by the **XComponent**. | 351| rect | [SurfaceRect](#surfacerect12) | Yes | Rectangle for displaying the surface held by the **XComponent**.| 352 353> **NOTE** 354> 355> The callback is triggered only when the **libraryname** parameter is not set for the **XComponent**. 356 357### onSurfaceDestroyed<sup>12+</sup> 358 359onSurfaceDestroyed(surfaceId: string): void 360 361Triggered when the surface held by the **XComponent** is destroyed. This API works only when **type** of the **XComponent** is set to **SURFACE** (**"surface"**) or **TEXTURE**. 362 363**Atomic service API**: This API can be used in atomic services since API version 12. 364 365**System capability**: SystemCapability.ArkUI.ArkUI.Full 366 367**Parameters** 368 369| Name | Type| Mandatory| Description | 370| --------- | -------- | ---- | ------------------------------------------------- | 371| surfaceId | string | Yes | ID of the surface held by the **XComponent**.| 372 373> **NOTE** 374> 375> The callback is triggered only when the **libraryname** parameter is not set for the **XComponent**. 376 377### startImageAnalyzer<sup>12+</sup> 378 379startImageAnalyzer(config: ImageAnalyzerConfig): Promise\<void> 380 381Starts AI image analysis in the given settings. Before calling this API, make sure the AI image analyzer is [enabled](#enableanalyzer12).<br>Because the image frame used for analysis is the one captured when this API is called, pay attention to the invoking time of this API.<br>If this API is repeatedly called before the execution is complete, an error callback is triggered. 382 383> **NOTE** 384> 385> The image analysis type cannot be dynamically modified. 386> 387> This API depends on device capabilities. If it is called on an incompatible device, an error code is returned. 388 389**Atomic service API**: This API can be used in atomic services since API version 12. 390 391**System capability**: SystemCapability.ArkUI.ArkUI.Full 392 393**Parameters** 394 395| Name| Type | Mandatory| Description | 396| ------ | --------- | ---- | ---------------------------------------------------------------------- | 397| config | [ImageAnalyzerConfig](ts-image-common.md#imageanalyzerconfig) | Yes | Settings of the AI image analyzer.| 398 399**Return value** 400 401| Type | Description | 402| ----------------- | ------------------------------------ | 403| Promise\<void> | Promise used to return the result.| 404 405**Error codes** 406 407For details about the error codes, see [AI Image Analyzer Error Codes](../errorcode-image-analyzer.md). 408 409| ID| Error Message | 410| -------- | -------------------------------------------- | 411| 110001 | Image analysis feature is unsupported. | 412| 110002 | Image analysis is currently being executed. | 413| 110003 | Image analysis is stopped. | 414 415### stopImageAnalyzer<sup>12+</sup> 416 417stopImageAnalyzer(): void 418 419Stops AI image analysis. The content displayed by the AI image analyzer will be destroyed. 420 421> **NOTE** 422> 423> If this API is called when the **startImageAnalyzer** API has not yet returned any result, an error callback is triggered. 424> 425> This feature depends on device capabilities. 426 427**Atomic service API**: This API can be used in atomic services since API version 12. 428 429**System capability**: SystemCapability.ArkUI.ArkUI.Full 430 431### setXComponentSurfaceRotation<sup>12+</sup> 432 433setXComponentSurfaceRotation(rotationOptions: SurfaceRotationOptions): void 434 435Sets whether to lock the orientation of the surface held by this **XComponent** when the screen rotates. This API is effective only when the **XComponent** type is **SURFACE** (**"surface"**). 436 437**Atomic service API**: This API can be used in atomic services since API version 12. 438 439**System capability**: SystemCapability.ArkUI.ArkUI.Full 440 441**Parameters** 442 443| Name| Type | Mandatory| Description | 444| ------ | ------------------------------------ | ---- | --------------------------------- | 445| rotationOptions | [SurfaceRotationOptions](#surfacerotationoptions12) | Yes| Whether to lock the orientation of the surface held by the current **XComponent** when the screen rotates.| 446 447> **NOTE** 448> 449> If **rotationOptions** is not set, the surface held by this **XComponent** rotates with the screen by default. 450> 451> The orientation lock is only applied during the rotation process and is released once the rotation is complete. 452> 453> The setting takes effect only when the screen is rotated by 90°, that is, when it switches between landscape and portrait modes. 454> 455> Make sure the Buffer's width and height remain constant after locking the orientation to prevent distortion. 456 457### getXComponentSurfaceRotation<sup>12+</sup> 458 459getXComponentSurfaceRotation(): Required\<SurfaceRotationOptions> 460 461Obtains whether the orientation of the surface held by this **XComponent** is locked when the screen rotates. This API is effective only when the **XComponent** type is **SURFACE** (**"surface"**). 462 463**Atomic service API**: This API can be used in atomic services since API version 12. 464 465**System capability**: SystemCapability.ArkUI.ArkUI.Full 466 467**Return value** 468 469| Type | Description | 470| ------------------------------------ | ------------------------------------- | 471| [SurfaceRotationOptions](#surfacerotationoptions12) | Whether the orientation of the surface held by the current **XComponent** is locked when the screen rotates.| 472 473## SurfaceRotationOptions<sup>12+</sup> 474 475Defines whether the orientation of the surface held by the current **XComponent** is locked when the screen rotates. 476 477**Atomic service API**: This API can be used in atomic services since API version 12. 478 479**System capability**: SystemCapability.ArkUI.ArkUI.Full 480 481| Name | Type | Mandatory| Description | 482| ------------- | ------ | ---- | ------------------------------------------------------------ | 483| lock | boolean | No | Whether the orientation of the surface is locked when the screen rotates. If this parameter is not set, the default value **false** is used, indicating that the orientation is not locked.| 484 485## SurfaceRect<sup>12+</sup> 486 487Describes the rectangle of the surface held by the **XComponent**. 488 489**Atomic service API**: This API can be used in atomic services since API version 12. 490 491**System capability**: SystemCapability.ArkUI.ArkUI.Full 492 493| Name | Type | Mandatory| Description | 494| ------------- | ------ | ---- | ------------------------------------------------------------ | 495| offsetX | number | No | X coordinate of the surface rectangle relative to the upper left corner of the **XComponent**.<br>Unit: px| 496| offsetY | number | No | Y coordinate of the surface rectangle relative to the upper left corner of the **XComponent**.<br>Unit: px| 497| surfaceWidth | number | Yes | Width of the surface rectangle.<br>Unit: px | 498| surfaceHeight | number | Yes | Height of the surface rectangle.<br>Unit: px | 499 500> **NOTE** 501> 502> The **surfaceWidth** and **surfaceHeight** attributes default to the size of the **XComponent** if the [setXComponentSurfaceRect](ts-basic-components-xcomponent.md#setxcomponentsurfacerect12) API is not called and neither [border](ts-universal-attributes-border.md#border) nor [padding](ts-universal-attributes-size.md#padding) is set. 503> 504> Make sure the values of **surfaceWidth** and **surfaceHeight** do not exceed 8192 px. Exceeding this limit may lead to rendering issues. 505 506## Example 507 508You can preview how this component looks on a real device, but not in DevEco Studio Previewer. 509 510 511### Example 1: Enabling AI Image Analyzer 512 513This example shows how to use the **enableAnalyzer** attribute to enable AI image analyzer. You can use **XComponentController** to start or stop AI analysis on images. 514 515```ts 516// xxx.ets 517import { BusinessError } from '@kit.BasicServicesKit'; 518import nativeRender from 'libnativerender.so'; 519 520class CustomXComponentController extends XComponentController { 521 onSurfaceCreated(surfaceId: string): void { 522 console.log(`onSurfaceCreated surfaceId: ${surfaceId}`); 523 nativeRender.SetSurfaceId(BigInt(surfaceId)); 524 } 525 526 onSurfaceChanged(surfaceId: string, rect: SurfaceRect): void { 527 console.log(`onSurfaceChanged surfaceId: ${surfaceId}, rect: ${JSON.stringify(rect)}}`); 528 nativeRender.ChangeSurface(BigInt(surfaceId), rect.surfaceWidth, rect.surfaceHeight); 529 } 530 531 onSurfaceDestroyed(surfaceId: string): void { 532 console.log(`onSurfaceDestroyed surfaceId: ${surfaceId}`); 533 nativeRender.DestroySurface(BigInt(surfaceId)); 534 } 535} 536 537@Entry 538@Component 539struct XComponentExample { 540 xComponentController: XComponentController = new CustomXComponentController(); 541 private config: ImageAnalyzerConfig = { 542 types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT] 543 }; 544 private aiController: ImageAnalyzerController = new ImageAnalyzerController(); 545 private options: ImageAIOptions = { 546 types: [ImageAnalyzerType.SUBJECT, ImageAnalyzerType.TEXT], 547 aiController: this.aiController 548 }; 549 @State xcWidth: string = "320px"; 550 @State xcHeight: string = "480px"; 551 @State currentStatus: string = "index"; 552 553 build() { 554 Column({ space: 5 }) { 555 Button("change size") 556 .onClick(() => { 557 this.xcWidth = "640px"; 558 this.xcHeight = "720px"; 559 }) 560 Button('start AI analyze') 561 .onClick(() => { 562 this.xComponentController.startImageAnalyzer(this.config) 563 .then(() => { 564 console.log("analysis complete"); 565 }) 566 .catch((error: BusinessError) => { 567 console.log("error code: " + error.code); 568 }) 569 }) 570 Button('stop AI analyze') 571 .onClick(() => { 572 this.xComponentController.stopImageAnalyzer(); 573 }) 574 Button('get analyzer types') 575 .onClick(() => { 576 this.aiController.getImageAnalyzerSupportTypes(); 577 }) 578 Button('Draw Star') 579 .fontSize('16fp') 580 .fontWeight(500) 581 .margin({ bottom: 24 }) 582 .onClick(() => { 583 let surfaceId = this.xComponentController.getXComponentSurfaceId(); 584 this.xComponentController.getXComponentSurfaceRect(); 585 nativeRender.DrawPattern(BigInt(surfaceId)); 586 let hasDraw: boolean = false; 587 if (nativeRender.GetXComponentStatus(BigInt(surfaceId))) { 588 hasDraw = nativeRender.GetXComponentStatus(BigInt(surfaceId)).hasDraw; 589 } 590 if (hasDraw) { 591 this.currentStatus = "draw star"; 592 } 593 }) 594 XComponent({ 595 type: XComponentType.SURFACE, 596 controller: this.xComponentController, 597 imageAIOptions: this.options 598 }) 599 .width(this.xcWidth) 600 .height(this.xcHeight) 601 .enableAnalyzer(true) 602 Text(this.currentStatus) 603 .fontSize('24fp') 604 .fontWeight(500) 605 } 606 .width("100%") 607 } 608} 609``` 610<!--RP1--><!--RP1End--> 611 612### Example 2: Locking the Surface Orientation During Screen Rotation 613 614This example shows how to use **setXComponentSurfaceRotation** to lock the surface orientation during screen rotation so that the surface does not rotate with the screen. 615 616```ts 617// xxx.ets 618@Entry 619@Component 620struct Index{ 621 @State isLock: boolean = true; 622 @State xc_width: number = 500; 623 @State xc_height: number = 700; 624 myXComponentController: XComponentController = new XComponentController(); 625 626 build() { 627 Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Start }) { 628 XComponent({ 629 id: 'xComponentId', 630 type: XComponentType.SURFACE, 631 libraryname: 'nativerender', 632 controller: this.myXComponentController 633 }) 634 .width(this.xc_width) 635 .height(this.xc_height) 636 .onLoad(() => { 637 let surfaceRotation: SurfaceRotationOptions = { lock: this.isLock }; 638 this.myXComponentController.setXComponentSurfaceRotation(surfaceRotation); 639 console.log("Surface getXComponentSurfaceRotation lock = " + 640 this.myXComponentController.getXComponentSurfaceRotation().lock); 641 }) 642 } 643 .width('100%') 644 .height('100%') 645 } 646} 647``` 648