HarmonyOS鸿蒙Next中ArkTS有没有类似OC中resizableImageWithCapInsets可以局部拉伸图片的函数?
HarmonyOS鸿蒙Next中ArkTS有没有类似OC中resizableImageWithCapInsets可以局部拉伸图片的函数?
3 回复
Image(this.imgUrl)
.width('100%')
.height(100)
.resizable({
slice: {
top: '44%',
bottom: '45%',
left: '10%',
right: '89%',
}
})
更多关于HarmonyOS鸿蒙Next中ArkTS有没有类似OC中resizableImageWithCapInsets可以局部拉伸图片的函数?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在鸿蒙Next的ArkTS中,可以使用Image
组件的objectFit
属性结合borderImage
实现类似功能。具体可通过borderImageSlice
设置九宫格切分区域,使用borderImageWidth
定义拉伸区域。示例代码片段:
Image($r('app.media.example'))
.borderImage({
source: $r('app.media.example'),
slices: {top: 20, right: 20, bottom: 20, left: 20},
width: {top: 20, right: 20, bottom: 20, left: 20},
repeat: 0 /* RepeatStyle.None */
})
.objectFit(ImageFit.Fill)
该方式通过九宫格参数控制图片的可拉伸区域。
在HarmonyOS Next的ArkTS中,确实提供了类似的图片局部拉伸功能。可以使用Image
组件的resize
和borderRadius
属性来实现类似OC中resizableImageWithCapInsets
的效果。
具体实现方式如下:
- 使用
Image
组件时设置resize
属性为ResizeMode.Cover
或ResizeMode.Contain
- 通过
borderRadius
属性设置图片的圆角区域(相当于OC中的cap insets) - 结合
clip
属性来控制图片的裁剪区域
示例代码:
Image($r('app.media.example'))
.resize(ResizeMode.Cover)
.borderRadius({
topLeft: 20,
topRight: 20,
bottomLeft: 20,
bottomRight: 20
})
.clip(true)
这种方式可以实现图片的局部拉伸而不失真,特别适合用于按钮背景等需要适配不同尺寸的场景。