ts-universal-attributes-border-image.md 7.9 KB
Newer Older
E
ester.zhou 已提交
1
# Border Image
E
ester.zhou 已提交
2

E
ester.zhou 已提交
3
You can draw an image around a component.
E
ester.zhou 已提交
4

E
ester.zhou 已提交
5 6
>  **NOTE**
>
E
ester.zhou 已提交
7
>  The APIs of this module are supported since API version 9. Updates will be marked with a superscript to indicate their earliest API version.
E
ester.zhou 已提交
8 9 10

## Attributes

G
Gloria 已提交
11 12 13
| Name         | Type                                    | Description                                    |
| ----------- | ---------------------------------------- | -------------------------------------- |
| borderImage | [BorderImageOption](#borderimageoption) | Border image or border gradient.<br>This API is supported in ArkTS widgets.|
E
ester.zhou 已提交
14

E
ester.zhou 已提交
15
## BorderImageOption
E
ester.zhou 已提交
16

E
ester.zhou 已提交
17 18
This API is supported in ArkTS widgets.

E
ester.zhou 已提交
19 20
| Name  | Type                                                        | Description                                                        |
| ------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
G
Gloria 已提交
21
| source | string \| [Resource](ts-types.md#resource) \| [linearGradient](ts-universal-attributes-gradient-color.md) | Source or gradient color of the border image.<br>**NOTE**<br>The border image source applies only to container components, such as **\<Row>**, **\<Column>**, and **\<Flex>**.|
E
ester.zhou 已提交
22 23 24 25 26
| slice  | [Length](ts-types.md#length) \| [EdgeWidths](ts-types.md#edgewidths9) | Slice width of the upper left corner, upper right corner, lower left corner, and lower right corner of the border image.<br>Default value: **0**<br>**NOTE**<br>If this parameter is set to a negative value, the default value is used.<br>When this parameter is set to a value of the [Length](ts-types.md#length) type, the value applies to the four corners in a unified manner.<br>When this parameter is set to a value of the [EdgeWidths](ts-types.md#edgewidths9) type:<br>- **Top**: slice height of the upper left or upper right corner of the image.<br>- **Bottom**: slice height of the lower left or lower right corner of the image.<br>- **Left**: slice width of the upper left or lower left corner of the image.<br>- **Right**: slice width of the upper right or lower right corner of the image.|
| width  | [Length](ts-types.md#length) \| [EdgeWidths](ts-types.md#edgewidths9) | Width of the border image.<br>Default value: **0**<br>**NOTE**<br>If this parameter is set to a negative value, the default value is used.<br>When this parameter is set to a value of the [Length](ts-types.md#length) type, the value applies to the four corners in a unified manner.<br>When this parameter is set to a value of the [EdgeWidths](ts-types.md#edgewidths9) type:<br>- **Top**: width of the top edge of the border image.<br>- **Bottom**: width of the bottom edge of the border image.<br>- **Left**: width of the left edge of the border image.<br>- **Right**: width of the right edge of the border image.<br>If this parameter is set to a negative value, the value **1** is used.|
| outset | [Length](ts-types.md#length) \| [EdgeWidths](ts-types.md#edgewidths9) | Amount by which the border image is extended beyond the border box.<br>Default value: **0**<br>**NOTE**<br>If this parameter is set to a negative value, the default value is used.<br>When this parameter is set to a value of the [Length](ts-types.md#length) type, the value applies to the four corners in a unified manner.<br>When this parameter is set to a value of the [EdgeWidths](ts-types.md#edgewidths9) type:<br>- **Top**: amount by which the top edge of the border image is extended beyond the border box.<br>- **Bottom**: amount by which the bottom edge of the border image is extended beyond the border box.<br>- **Left**: amount by which the left edge of the border image is extended beyond the border box.<br>- **Right**: amount by which the right edge of the border image is extended beyond the border box.|
| repeat | [RepeatMode](#repeatmode)                            | Repeat mode of the source image's slices on the border.<br>Default value: **RepeatMode.Stretch**|
| fill   | boolean                                                      | Whether to fill the center of the border image.<br>Default value: **false**                    |
E
ester.zhou 已提交
27 28 29

## RepeatMode

E
ester.zhou 已提交
30 31
This API is supported in ArkTS widgets.

E
ester.zhou 已提交
32 33 34
| Name     | Description                                 |
| ------- | ----------------------------------- |
| Repeat  | The source image's slices are tiled. Tiles beyond the border box will be clipped.         |
E
ester.zhou 已提交
35
| Stretch | The source image's slices are stretched to fill the border box.               |
E
ester.zhou 已提交
36
| Round   | The source image's slices are tiled to fill the border box. Tiles may be compressed when needed.|
E
ester.zhou 已提交
37
| Space   | The source image's slices are tiled to fill the border box. Extra space will be distributed in between tiles.  |
E
ester.zhou 已提交
38

E
ester.zhou 已提交
39 40
## Example

E
ester.zhou 已提交
41 42 43
### Example 1


E
ester.zhou 已提交
44 45 46 47 48 49 50 51
```ts
// xxx.ets
@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
E
ester.zhou 已提交
52
        Text('This is gradient color.').textAlign(TextAlign.Center).height(50).width(200)
E
ester.zhou 已提交
53
          .borderImage({
E
ester.zhou 已提交
54 55 56 57 58 59 60 61
            source: {
              angle: 90,
              direction: GradientDirection.Left,
              colors: [[0xAEE1E1, 0.0], [0xD3E0DC, 0.3], [0xFCD1D1, 1.0]]
            },
            slice: { top: 10, bottom: 10, left: 10, right: 10 },
            width: { top: "10px", bottom: "10px", left: "10px", right: "10px" },
            repeat: RepeatMode.Stretch,
E
ester.zhou 已提交
62
            fill: false
E
ester.zhou 已提交
63
          })
E
ester.zhou 已提交
64 65 66 67 68 69 70 71
      }
      .width('100%')
    }
    .height('100%')
  }
}
```

E
ester.zhou 已提交
72
![en-us_image_borderImageGradient](figures/borderImageGradient.png)
E
ester.zhou 已提交
73

E
ester.zhou 已提交
74
### Example 2
E
ester.zhou 已提交
75 76 77 78 79

```ts
// xxx.ets
@Entry
@Component
E
ester.zhou 已提交
80 81 82 83 84 85 86 87
struct BorderImage {
  @State WidthValue: number = 0
  @State SliceValue: number = 0
  @State OutSetValue: number = 0
  @State RepeatValue: RepeatMode[] = [RepeatMode.Repeat, RepeatMode.Stretch, RepeatMode.Round, RepeatMode.Space]
  @State SelectIndex: number = 0
  @State SelectText: string = 'Repeat'
  @State FillValue: boolean = false
E
ester.zhou 已提交
88

E
ester.zhou 已提交
89 90
  build() {
    Row() {
E
ester.zhou 已提交
91
      Column({ space: 20 }) {
E
ester.zhou 已提交
92 93 94 95 96
        Row() {
          Text('This is borderImage.').textAlign(TextAlign.Center).fontSize(50)
        }
        .borderImage({
          source: $r('app.media.icon'),
E
ester.zhou 已提交
97 98 99 100 101
          slice: this.SliceValue,
          width: this.WidthValue,
          outset: this.OutSetValue,
          repeat: this.RepeatValue[this.SelectIndex],
          fill: this.FillValue
E
ester.zhou 已提交
102 103
        })

E
ester.zhou 已提交
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
        Column() {
          Text(`borderImageSlice = ${this.SliceValue}px`)
          Slider({
            value: this.SliceValue,
            min: 0,
            max: 100,
            style: SliderStyle.OutSet
          })
            .onChange((value: number, mode: SliderChangeMode) => {
              this.SliceValue = value
            })
        }

        Column() {
          Text(`borderImageWidth = ${this.WidthValue}px`)
          Slider({
            value: this.WidthValue,
            min: 0,
            max: 100,
            style: SliderStyle.OutSet
          })
            .onChange((value: number, mode: SliderChangeMode) => {
              this.WidthValue = value
            })
        }

        Column() {
          Text(`borderImageOutSet = ${this.OutSetValue}px`)
          Slider({
            value: this.OutSetValue,
            min: 0,
            max: 100,
            style: SliderStyle.OutSet
E
ester.zhou 已提交
137
          })
E
ester.zhou 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
            .onChange((value: number, mode: SliderChangeMode) => {
              this.OutSetValue = value
            })
        }

        Row() {
          Text('borderImageRepeat: ')
          Select([{ value: 'Repeat' }, { value: 'Stretch' }, { value: 'Round' }, { value: 'Space' }])
            .value(this.SelectText)
            .selected(this.SelectIndex)
            .onSelect((index: number, text: string) => {
              this.SelectIndex = index
              this.SelectText = text
            })
        }

        Row() {
          Text(`borderImageFill: ${this.FillValue} `)
          Toggle({ type: ToggleType.Switch, isOn: this.FillValue })
            .onChange((isOn: boolean) => {
              this.FillValue = isOn
            })
        }

E
ester.zhou 已提交
162 163 164 165 166 167 168 169
      }
      .width('100%')
    }
    .height('100%')
  }
}
```

E
ester.zhou 已提交
170
![borderImage](figures/borderImage.gif)