Harmony OS Next
Simplified Usage of Shape Clipping
Shape clipping functionality in HarmonyOS development is used for cropping and masking components. Below is a simplified guide to its usage.
Official documentation: https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-attributes-sharp-clipping-V5#mask12
-
Clipping Functionality
-
clipProperty (Supported from API Version 7, updated in API Version 12): Determines whether to clip child components of the current component. Accepts a boolean parameter. For example, when anImagecomponent is inside aRowcomponent, settingclip(true)on theRowwill constrain the image display within the row’s border (e.g., rounded corners set viaborderRadius). -
clipShapeProperty (Supported from API Version 12): Clips the component to a specified shape (CircleShape,EllipseShape,PathShape, orRectShape). For example, to crop an image into a circle, create aCircleobject with appropriate dimensions and apply it to theImagecomponent’sclipShapeproperty.
-
-
Masking Functionality
-
maskProperty (Supported from API Version 12): Adds a progress-based mask to the component. Accepts aProgressMaskparameter, which allows setting properties like progress value, maximum value, and color for dynamic effects. -
maskShapeProperty (Supported from API Version 12): Applies a geometric mask (circle, ellipse, path, or rectangle) to the component. Create a shape object (e.g.,Rect,Circle) with properties like fill color, then apply it to the component’smaskShapeproperty.
-
Example Code Analysis
-
Clipping Example
In theClipAndMaskExamplestruct:- The
clipproperty is demonstrated on aRowcontaining anImage. Settingclip(true)andborderRadius(20)crops the image to rounded corners. Withoutclip(true), the image corners would extend beyond the row’s rounded border. - The
clipShapeproperty uses aCircleshape with 280px diameter to crop the image into a perfect circle.
- The
-
Masking Example
In theProgressMaskExamplestruct:- A
ProgressMaskobject is applied to anImagevia themaskproperty, creating a progress-based mask. Animation effects are configured including duration, curve, delay, iterations, and play mode. - Button click events control:
-
updateProgress: Increases the mask’s progress value -
updateColor: Changes the mask color -
enableBreathingAnimation: Toggles the breathing glow animation when progress reaches 100%
-
- A
Shape clipping provides powerful styling capabilities for components, enabling various personalized UI effects. Developers can flexibly utilize these properties and methods to optimize application interfaces based on specific requirements.
