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