HarmonyOS上不支持.9资源文件进行安全拉伸。作为替代方案,本案例中商城页面的促销标签边框使用同一张图片资源,通过设置图片的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')
}
})Text(discount)
.fontSize($r('app.integer.ninepatchimage_product_discount_font_size'))
.fontColor(Color.Red)
// TODO: 知识点:通过监听文本组件的宽高,以及状态变量,动态设置背景图片的宽高
.onAreaChange((oldValue: Area, newValue: Area) => {
this.discountTextWidth = newValue.width;
this.discountTextHeight = newValue.height;
})
.padding({
left: $r('app.integer.ninepatchimage_product_discount_padding'),
right: $r('app.integer.ninepatchimage_product_discount_padding')
})欢迎大家关注公众号<程序猿百晓生>,可以了解到一下知识点。1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案)
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......WaterFlow() {
LazyForEach(this.productsDataSource, (item: Product) => {
FlowItem() {
// TODO:性能知识点:使用@Reusable修饰可复用的组件,并通过设置reuseId提供瀑布流滑动场景中的组件复用能力
ProductItemComp({
product: item
})
.reuseId(item.type)
}
.width('100%')
})
}@Reusable
@Component
export struct ProductItemComp {
...
}Text(discount)
.fontSize($r('app.integer.imageresizable_product_discount_font_size'))
.fontColor(Color.Red)
// TODO: 知识点:通过监听文本组件的宽高,以及状态变量,动态设置背景图片的宽高
// TODO: 性能知识点:onAreaChange为系统高频回调,每一帧都会执行,应避免在其中进行冗余和耗时的操作
.onAreaChange((oldValue: Area, newValue: Area) => {
this.discountTextWidth = newValue.width;
this.discountTextHeight = newValue.height;
})ninepatchimage // har类型
|---common
| |---constants
| | |---CommonConstants.ets // 通用常量
|---components
| |---ProductItemComp.ets // 自定义组件-商品简易信息
|---model
| |---ProductModel.ets // 模型层-商品数据模型
|---pages
| |---ProductWaterFlowPage.ets // 展示层-商品瀑布流页面如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。