ts-universal-attributes-sharp-clipping.md 2.4 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
| Name   | Type                                    | Description                                 |
E
ester.zhou 已提交
14
| -----| ------------------------------------------ | ------------------------------------ |
E
ester.zhou 已提交
15 16
| 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 | Clip mode. If the value is a shape, the component is clipped based on the specified shape. If the value is of the Boolean type, it specifies whether to clip the component based on the edge outline of the parent container.<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) | Mask of the specified shape to add to the component.|
Z
zengyawen 已提交
17 18 19 20


## Example

E
ester.zhou 已提交
21 22
```ts
// xxx.ets
Z
zengyawen 已提交
23 24 25 26
@Entry
@Component
struct ClipAndMaskExample {
  build() {
E
ester.zhou 已提交
27 28
    Column({ space: 15 }) {
      Text('clip').fontSize(12).width('75%').fontColor('#DCDCDC')
Z
zengyawen 已提交
29
      Row() {
E
ester.zhou 已提交
30
        Image($r('app.media.testImg')).width('500px').height('280px')
Z
zengyawen 已提交
31
      }
E
ester.zhou 已提交
32
      .clip(true) // If clip is not set to true, the image is not confined by the rounded corners of the <Row> component and may extend beyond the <Row> component.
Z
zengyawen 已提交
33
      .borderRadius(20)
E
ester.zhou 已提交
34 35 36 37
      // Clip the image based on a circle with a diameter of 280 px.
      Image($r('app.media.testImg'))
        .clip(new Circle({ width: '280px', height: '280px' }))
        .width('500px').height('280px')
Z
zengyawen 已提交
38

E
ester.zhou 已提交
39 40 41
      Text('mask').fontSize(12).width('75%').fontColor('#DCDCDC')
      // Add a 500 px x 280 px square mask to the image.
      Image($r('app.media.testImg'))
Z
zengyawen 已提交
42 43 44
        .mask(new Rect({ width: '500px', height: '280px' }).fill(Color.Gray))
        .width('500px').height('280px')

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

E
ester.zhou 已提交
56
![clipAndMask](figures/clipAndMask.PNG)