1# @ohos.arkui.drawableDescriptor (DrawableDescriptor) 2 3The **DrawableDescriptor** module provides APIs for obtaining **pixelMap** objects, including the foreground, background, mask, and layered icons. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> You can preview how this component looks on a real device, but not in DevEco Studio Previewer. 10 11## Modules to Import 12 13```ts 14import { DrawableDescriptor, LayeredDrawableDescriptor } from '@kit.ArkUI'; 15``` 16 17## DrawableDescriptor 18 19Resources in PNG, JPG, BMP, SVG, GIF, WEBP, ASTC, and SUT formats are supported. 20 21### getPixelMap 22 23getPixelMap(): image.PixelMap 24 25Obtains this **pixelMap** object. 26 27**Atomic service API**: This API can be used in atomic services since API version 11. 28 29**System capability**: SystemCapability.ArkUI.ArkUI.Full 30 31**Return value** 32 33| Type | Description | 34| ---------------------------------------- | -------- | 35| [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | **PixelMap** object.| 36 37**Example** 38 ```ts 39import { DrawableDescriptor, LayeredDrawableDescriptor } from '@kit.ArkUI' 40let resManager = getContext().resourceManager 41let pixmap: DrawableDescriptor = (resManager.getDrawableDescriptor($r('app.media.icon') 42 .id)) as DrawableDescriptor; 43let pixmapNew: object = pixmap.getPixelMap() 44 ``` 45 46Creates a **DrawableDescriptor** object when the passed resource ID or name belongs to a common image. 47 48## PixelMapDrawableDescriptor<sup>12+</sup> 49 50Implements a **PixelMapDrawableDescriptor** object , which can be created by passing in a **pixelMap** object. Inherits from [DrawableDescriptor](#drawabledescriptor). 51 52### constructor<sup>12+</sup> 53 54constructor(src?: image.PixelMap) 55 56A constructor used to create a **PixelMapDrawableDescriptor** object. 57 58**Atomic service API**: This API can be used in atomic services since API version 12. 59 60**System capability**: SystemCapability.ArkUI.ArkUI.Full 61 62**Parameters** 63 64| Name | Type | Mandatory | Description | 65| --------- | ---------------- | ---- | ------------------------------------------ | 66| src | [image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7) | No| **PixelMap** image data.| 67 68 69## LayeredDrawableDescriptor 70 71Creates a **LayeredDrawableDescriptor** object when the passed resource ID or name belongs to a JSON file that contains foreground and background resources. Inherits from [DrawableDescriptor](#drawabledescriptor). 72 73The **drawable.json** file is located under **entry/src/main/resources/base/media** in the project directory. Below shows the file content: 74 75```json 76{ 77 "layered-image": 78 { 79 "background" : "$media:background", 80 "foreground" : "$media:foreground" 81 } 82} 83``` 84 85**Example** 86 871. Create a **LayeredDrawableDescriptor** object from a JSON file. 88 89 ```ts 90 // xxx.ets 91 import { DrawableDescriptor, LayeredDrawableDescriptor } from '@kit.ArkUI' 92 93 @Entry 94 @Component 95 struct Index { 96 private resManager = getContext().resourceManager 97 98 build() { 99 Row() { 100 Column() { 101 Image((this.resManager.getDrawableDescriptor($r('app.media.drawable').id) as LayeredDrawableDescriptor)) 102 Image(((this.resManager.getDrawableDescriptor($r('app.media.drawable') 103 .id) as LayeredDrawableDescriptor).getForeground()).getPixelMap()) 104 }.height('50%') 105 }.width('50%') 106 } 107 } 108 ``` 1092. Creates a **LayeredDrawableDescriptor** object using **PixelMapDrawableDescriptor**. 110 111 ```ts 112 import { DrawableDescriptor, LayeredDrawableDescriptor, PixelMapDrawableDescriptor } from '@kit.ArkUI' 113 import { image } from '@kit.ImageKit' 114 115 @Entry 116 @Component 117 struct Index { 118 @State fore1: image.PixelMap | undefined = undefined 119 @State back1: image.PixelMap | undefined = undefined 120 121 @State foregroundDraw:DrawableDescriptor|undefined=undefined 122 @State backgroundDraw:DrawableDescriptor|undefined=undefined 123 @State maskDraw:DrawableDescriptor|undefined=undefined 124 @State maskPixel: image.PixelMap | undefined = undefined 125 @State draw : LayeredDrawableDescriptor | undefined = undefined 126 async aboutToAppear() { 127 this.fore1 = await this.getPixmapFromMedia($r('app.media.foreground')) 128 this.back1 = await this.getPixmapFromMedia($r('app.media.background')) 129 this.maskPixel = await this.getPixmapFromMedia($r('app.media.ohos_icon_mask')) 130 // Create a LayeredDrawableDescriptor object using PixelMapDrawableDescriptor. 131 this.foregroundDraw = new PixelMapDrawableDescriptor(this.fore1) 132 this.backgroundDraw = new PixelMapDrawableDescriptor(this.back1) 133 this.maskDraw = new PixelMapDrawableDescriptor(this.maskPixel) 134 135 this.draw = new LayeredDrawableDescriptor(this.foregroundDraw,this.backgroundDraw,this.maskDraw) 136 } 137 build() { 138 Row() { 139 Column() { 140 Image(this.draw) 141 .width(300) 142 .height(300) 143 }.height('100%').justifyContent(FlexAlign.Center) 144 }.width('100%').height("100%").backgroundColor(Color.Pink) 145 } 146 // Obtain pixelMap from a resource through the image framework based on the resource 147 private async getPixmapFromMedia(resource: Resource) { 148 let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({ 149 bundleName: resource.bundleName, 150 moduleName: resource.moduleName, 151 id: resource.id 152 }) 153 let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength)) 154 let createPixelMap: image.PixelMap = await imageSource.createPixelMap({ 155 desiredPixelFormat: image.PixelMapFormat.BGRA_8888 156 }) 157 await imageSource.release() 158 return createPixelMap 159 } 160 } 161 ``` 162 163### constructor<sup>12+</sup> 164 165constructor(foreground?: DrawableDescriptor, background?: DrawableDescriptor, mask?: DrawableDescriptor); 166 167A constructor used to create a **LayeredDrawableDescriptor** object. 168 169**Atomic service API**: This API can be used in atomic services since API version 12. 170 171**System capability**: SystemCapability.ArkUI.ArkUI.Full 172 173**Parameters** 174 175| Name | Type | Mandatory | Description | 176| --------- | ---------------- | ---- | ------------------------------------------ | 177| foreground | [DrawableDescriptor](#drawabledescriptor) | No | Options for the foreground image of the layered drawable.| 178| background | [DrawableDescriptor](#drawabledescriptor) | No | Options for the background image of the layered drawable. | 179| mask | [DrawableDescriptor](#drawabledescriptor) | No| Options for the mask of the layered drawable.| 180 181### getForeground 182getForeground(): DrawableDescriptor; 183 184Obtains the **DrawableDescriptor** object of the foreground. 185 186**Atomic service API**: This API can be used in atomic services since API version 11. 187 188**System capability**: SystemCapability.ArkUI.ArkUI.Full 189 190**Return value** 191 192| Type | Description | 193| ---------------------------------------- | -------------------- | 194| [DrawableDescriptor](#drawabledescriptor) | **DrawableDescriptor** object.| 195 196**Example** 197 ```ts 198import { DrawableDescriptor, LayeredDrawableDescriptor } from '@kit.ArkUI' 199let resManager = getContext().resourceManager 200let drawable: LayeredDrawableDescriptor = (resManager.getDrawableDescriptor($r('app.media.drawable') 201 .id)) as LayeredDrawableDescriptor; 202let drawableNew: object = drawable.getForeground() 203 ``` 204 205### getBackground 206 207getBackground(): DrawableDescriptor; 208 209Obtains the **DrawableDescriptor** object of the background. 210 211**Atomic service API**: This API can be used in atomic services since API version 11. 212 213**System capability**: SystemCapability.ArkUI.ArkUI.Full 214 215**Return value** 216 217| Type | Description | 218| ---------------------------------------- | -------------------- | 219| [DrawableDescriptor](#drawabledescriptor) | **DrawableDescriptor** object.| 220 221**Example** 222 ```ts 223import { DrawableDescriptor, LayeredDrawableDescriptor } from '@kit.ArkUI' 224let resManager = getContext().resourceManager 225let drawable: LayeredDrawableDescriptor = (resManager.getDrawableDescriptor($r('app.media.drawable') 226 .id)) as LayeredDrawableDescriptor; 227let drawableNew: object = drawable.getBackground() 228 ``` 229 230### getMask 231 232getMask(): DrawableDescriptor 233 234Obtains the **DrawableDescriptor** object of the mask. 235 236**Atomic service API**: This API can be used in atomic services since API version 11. 237 238**System capability**: SystemCapability.ArkUI.ArkUI.Full 239 240**Return value** 241 242| Type | Description | 243| ---------------------------------------- | -------------------- | 244| [DrawableDescriptor](#drawabledescriptor) | **DrawableDescriptor** object.| 245 246**Example** 247 ```ts 248import { DrawableDescriptor, LayeredDrawableDescriptor } from '@kit.ArkUI' 249let resManager = getContext().resourceManager 250let drawable: LayeredDrawableDescriptor = (resManager.getDrawableDescriptor($r('app.media.drawable') 251 .id)) as LayeredDrawableDescriptor; 252let drawableNew: object = drawable.getMask() 253 ``` 254### getMaskClipPath 255 256static getMaskClipPath(): string 257 258Obtains the built-in clipping path parameters of the system. It is a static method of **LayeredDrawableDescriptor**. 259 260**Atomic service API**: This API can be used in atomic services since API version 11. 261 262**System capability**: SystemCapability.ArkUI.ArkUI.Full 263 264**Return value** 265 266| Type | Description | 267| ---------------------------------------- | -------------------- | 268| string | String of the clipping path.| 269 270**Example** 271 272 ```ts 273// xxx.ets 274import { DrawableDescriptor, LayeredDrawableDescriptor } from '@kit.ArkUI' 275 276@Entry 277@Component 278struct Index { 279 build() { 280 Row() { 281 Column() { 282 Image($r('app.media.icon')) 283 .width('200px').height('200px') 284 .clipShape(new Path({commands:LayeredDrawableDescriptor.getMaskClipPath()})) 285 Text(`Obtain the built-in clip path parameters:`) 286 .fontWeight(800) 287 Text(JSON.stringify(LayeredDrawableDescriptor.getMaskClipPath())) 288 .padding({ left: 20, right: 20 }) 289 }.height('100%').justifyContent(FlexAlign.Center) 290 }.width('100%') 291 } 292} 293 ``` 294 295## AnimationOptions<sup>12+</sup> 296 297Provides the playback options of the animation with a pixel map image array in an **Image** component. 298 299**Atomic service API**: This API can be used in atomic services since API version 12. 300 301**System capability**: SystemCapability.ArkUI.ArkUI.Full 302 303| Name | Type | Mandatory | Description | 304| ---------- | ------ | -----| --------------------------------------- | 305| duration | number | No | Total playback duration for the pixel map image array. The default value is 1 second per image. | 306| iterations | number | No | Number of times that the pixel map image array is played. The default value is **1**. The value **-1** indicates that the animation is played for an unlimited number of times.| 307 308**Example** 309 310```ts 311import { AnimationOptions } from '@kit.ArkUI' 312@Entry 313@Component 314struct Example { 315 options: AnimationOptions = { duration: 2000, iterations: 1 } 316 build() { 317 } 318} 319``` 320 321## AnimatedDrawableDescriptor<sup>12+</sup> 322 323Implements an **AnimatedDrawableDescriptor** object, which can be passed in when the **Image** component is used to play the pixel map image array. Inherits from [DrawableDescriptor](#drawabledescriptor). 324 325### constructor<sup>12+</sup> 326 327constructor(pixelMaps: Array\<image.PixelMap>, options?: AnimationOptions) 328 329A constructor used to create an **AnimatedDrawableDescriptor** instance. 330 331**Atomic service API**: This API can be used in atomic services since API version 12. 332 333**System capability**: SystemCapability.ArkUI.ArkUI.Full 334 335**Parameters** 336 337| Name | Type | Mandatory | Description | 338| --------- | ---------------- | ---- | ------------------------------------------ | 339| pixelMaps | Array\<[image.PixelMap](../apis-image-kit/js-apis-image.md#pixelmap7)> | Yes | **PixelMap** image data.| 340| options | [AnimationOptions](#animationoptions12) | No | Animation options. | 341 342**Example** 343 344```ts 345import { AnimationOptions, AnimatedDrawableDescriptor } from '@kit.ArkUI' 346import { image } from '@kit.ImageKit' 347 348@Entry 349@Component 350struct Example { 351 pixelmaps: Array<image.PixelMap> = []; 352 options: AnimationOptions = {duration:1000, iterations:-1}; 353 @State animated: AnimatedDrawableDescriptor = new AnimatedDrawableDescriptor(this.pixelmaps, this.options); 354 async aboutToAppear() { 355 this.pixelmaps.push(await this.getPixmapFromMedia($r('app.media.icon'))) 356 this.animated = new AnimatedDrawableDescriptor(this.pixelmaps, this.options); 357 } 358 build() { 359 Column() { 360 Row() { 361 Image(this.animated) 362 } 363 } 364 } 365 private async getPixmapFromMedia(resource: Resource) { 366 let unit8Array = await getContext(this)?.resourceManager?.getMediaContent({ 367 bundleName: resource.bundleName, 368 moduleName: resource.moduleName, 369 id: resource.id 370 }) 371 let imageSource = image.createImageSource(unit8Array.buffer.slice(0, unit8Array.buffer.byteLength)) 372 let createPixelMap: image.PixelMap = await imageSource.createPixelMap({ 373 desiredPixelFormat: image.PixelMapFormat.RGBA_8888 374 }) 375 await imageSource.release() 376 return createPixelMap 377 } 378} 379 380``` 381