路径描述:从上底左侧点(150,0)开始,水平向右画到(250,0),接着以半径20的圆弧连接到(270,20),再直线延伸至右下角(350,180),然后通过另一个圆弧过渡到(330,200),再水平向左到(50,200),接着用圆弧连接到(30,180),然后垂直向下到(30,20),再通过圆弧回到起点(50,0),最后闭合路径。

示例代码如下:
import { PathShape } from '@kit.ArkUI';
@Entry
@Component
struct ServiceCardPage {
build() {
Column() {
Image($r('app.media.background'))
.width('500px')
.height('300px')
.clipShape(new PathShape({
commands: 'M 150 0 H 250 A 20 20 0 0 1 270 20 L 350 180 A 20 20 0 0 1 330 200 H 50 A 20 20 0 0 1 30 180 V 20 A 20 20 0 0 1 50 0 Z'
}));
}
.justifyContent(FlexAlign.Center)
.height('100%')
.width('100%');
}
} 构造一个宽度与图片一样,高度为图片1.5倍的RectShape,为此RectShape设置圆角,然后将此RectShape进行偏移。

示例代码如下:
import { RectShape } from '@kit.ArkUI';
@Entry
@Component
struct Index2 {
build() {
Column() {
Image($r('app.media.background'))
.width('50%')
.objectFit(ImageFit.Fill)
.clipShape(new RectShape({
radiusWidth: '40%',
radiusHeight: '10%',
width: '100%',
height: '150%'
})
.position({ y: '-50%' })
);
}
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%');
}
} 使用PathShape自定义路径,在底部中间位置绘制一个向上凹陷的圆弧,实现卡片式封面效果。
示例代码如下:
import { PathShape } from '@kit.ArkUI';
@Entry
@Component
struct ClipShapeExample {
private radiusVp: number = 16; // 圆角半径
build() {
Column() {
// 上方组件 - 使用PathShape裁剪
Column() {
// 上方组件内容区域
Text('上方内容')
.width('100%')
.height('100%')
.textAlign(TextAlign.Center)
.backgroundColor('#FFB6C1')
}
.width(300)
.height(100)
.clipShape(new PathShape()
.commands(this.getClipPath(300, 100, this.radiusVp))
)
// 下方组件 - 圆角16vp
Column() {
Text('下方内容')
.width('100%')
.height('100%')
.textAlign(TextAlign.Center)
.backgroundColor('#87CEEB')
}
.width(300)
.height(100)
.borderRadius(this.radiusVp)
.clip(true)
}
.width('100%')
.height('100%')
.justifyContent(FlexAlign.Center)
}
// 生成裁剪路径命令
// A指令参数说明: A rx ry x-rotation large-arc-flag sweep-flag x y
getClipPath(widthVp: number, heightVp: number, radiusVp: number): string {
const ctx = this.getUIContext();
const w = ctx.vp2px(widthVp);
const h = ctx.vp2px(heightVp);
const r = ctx.vp2px(radiusVp);
// 路径:从左上角开始,顺时针绘制,底部中间向上凹陷
return `M${r} 0
L${w - r} 0
A${r} ${r} 0 0 1 ${w} ${r}
L${w} ${h}
A${r} ${r} 0 0 0 ${w - r} ${h - r}
L${r} ${h - r}
A${r} ${r} 0 0 0 0 ${h}
L0 ${r}
A${r} ${r} 0 0 1 ${r} 0 Z`;
}
}