ts-universal-attributes-sharp-clipping.md 2.2 KB
Newer Older
Z
zengyawen 已提交
1
# Shape Clipping
Z
zengyawen 已提交
2

E
ester.zhou 已提交
3
Shape clipping changes the visible portion of a component through clipping or masking.
Z
zengyawen 已提交
4

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

Z
zengyawen 已提交
9

Z
zengyawen 已提交
10 11 12
## Attributes


E
ester.zhou 已提交
13 14 15 16
| Name | Type                                    | Description                                 |
| -----| ------------------------------------------ | ------------------------------------ |
| clip | [Circle](ts-drawing-components-circle.md) \| [Ellipse](ts-drawing-components-ellipse.md) \| [Path](ts-drawing-components-path.md) \| [Rect](ts-drawing-components-rect.md) \| boolean | Specifies a clip mode. The value **Shape** indicates that the current component is cropped based on the specified shape. The value **boolean** specifies whether to clip the component based on the edge outline.<br>Default value: **false**|
| mask | [Circle](ts-drawing-components-circle.md) \| [Ellipse](ts-drawing-components-ellipse.md) \| [Path](ts-drawing-components-path.md) \| [Rect](ts-drawing-components-rect.md) | Adds a mask of the specified shape to the current component.|
Z
zengyawen 已提交
17 18 19 20


## Example

E
ester.zhou 已提交
21 22
```ts
// xxx.ets
Z
zengyawen 已提交
23 24 25 26 27 28 29
@Entry
@Component
struct ClipAndMaskExample {
  build() {
    Column({ space: 5 }) {
      Text('clip').fontSize(9).width('90%').fontColor(0xCCCCCC)
      // Clip the image by using a circle with a diameter of 280px.
E
ester.zhou 已提交
30
      Image($r('app.media.example'))
Z
zengyawen 已提交
31 32 33 34
        .clip(new Circle({ width: 80, height: 80 }))
        .width('500px').height('280px')

      Row() {
E
ester.zhou 已提交
35
        Image($r('app.media.example')).width('500px').height('280px')
Z
zengyawen 已提交
36 37 38 39 40 41
      }
      .clip(true)
      .borderRadius(20)

      Text('mask').fontSize(9).width('90%').fontColor(0xCCCCCC)
      // Add a 500 px x 280 px mask to the image.
E
ester.zhou 已提交
42
      Image($r('app.media.example'))
Z
zengyawen 已提交
43 44 45 46
        .mask(new Rect({ width: '500px', height: '280px' }).fill(Color.Gray))
        .width('500px').height('280px')

      // Add a 280 px x 280 px circle mask to the image.
E
ester.zhou 已提交
47
      Image($r('app.media.example'))
Z
zengyawen 已提交
48 49 50 51 52 53 54 55 56
        .mask(new Circle({ width: '280px', height: '280px' }).fill(Color.Gray))
        .width('500px').height('281px')
    }
    .width('100%')
    .margin({ top: 5 })
  }
}
```

Z
zengyawen 已提交
57
![en-us_image_0000001212218452](figures/en-us_image_0000001212218452.png)