介绍
HarmonyOS上不支持.9资源文件进行安全拉伸。 作为替代方案,本案例中商城页面的促销标签边框使用同一张图片资源,通过设置图片的resizable属性,展示不同长度的促销标签效果。
效果图预览
使用说明
- 打开案例即可直接查看效果。
- 每一个商品的促销标签都使用同一张本地资源图片,不同长度的文案对应不同的图片拉伸比例。
实现思路
1.通过设置图片组件的resizable属性实现图片的安全拉伸。
Image($r('app.media.ninepatchimage_border'))
.width(this.discountTextWidth)
.height(this.discountTextHeight)
// TODO: 知识点:通过设置图片组件的resizable属性,设置顶部、右侧、底部、左侧的距离,使其边缘部分在图片拉伸时不会发生变化,仅图片中央区域被拉伸
.resizable({
slice: {
top: $r('app.integer.ninepatchimage_product_discount_image_resizable_top'),
right: $r('app.integer.ninepatchimage_product_discount_image_resizable_right'),
bottom: $r('app.integer.ninepatchimage_product_discount_image_resizable_bottom'),
left: $r('app.integer.ninepatchimage_product_discount_image_resizable_left')
}
})
2.依赖onAreaChange事件,观察促销标签文案所在Text组件的宽高变化,通过状态绑定的形式,动态修改促销标签边框底图的宽高。
Text(discount