其中四角TL/TR/BL/BR不会被拉伸,上下边T/B只会被水平拉伸,左右边L/R只会被垂直拉伸,中心C可以随意拉伸。

背景适配.9图并根据图案布局,有组件直接使用通用属性backgroundImageResizable和使用Stack堆叠Image和布局两种方案,其核心思路是相同的。思路如下:
slice: { top: this.getUIContext().px2vp(140), bottom: this.getUIContext().px2vp(50), left: this.getUIContext().px2vp(300), right: this.getUIContext().px2vp(50) }
Row() { Text(deviceInfo.deviceType) .width(this.getUIContext().px2vp(260)) .textAlign(TextAlign.Center) .fontSize(this.getUIContext().px2fp(60)); Text('使用backgroundImageResizable') .padding({ left: this.getUIContext().px2vp(24) }) .fontSize(this.getUIContext().px2fp(40)); } .margin({ top: this.getUIContext().px2vp(36) }) .width('100%') .justifyContent(FlexAlign.Start);
该问题两种方案的完整示例参考如下:
import { deviceInfo } from '@kit.BasicServicesKit'; @Entry @Component struct Point9 { build() { Column({ space: 10 }) { Column() { Row() { Text(deviceInfo.deviceType) .width(this.getUIContext().px2vp(260)) .textAlign(TextAlign.Center) .fontSize(this.getUIContext().px2fp(60)); Text('使用backgroundImageResizable') .padding({ left: this.getUIContext().px2vp(24) }) .fontSize(this.getUIContext().px2fp(40)); } .margin({ top: this.getUIContext().px2vp(36) }) .width('100%') .justifyContent(FlexAlign.Start); } .backgroundImage($r('app.media.point9')) .backgroundImageSize(ImageSize.FILL) .backgroundImageResizable({ slice: { top: this.getUIContext().px2vp(140), bottom: this.getUIContext().px2vp(50), left: this.getUIContext().px2vp(300), right: this.getUIContext().px2vp(50) } }) .width('90%') .height(200); Stack() { Image($r('app.media.point9')) .objectFit(ImageFit.Fill) .resizable({ slice: { top: this.getUIContext().px2vp(140), bottom: this.getUIContext().px2vp(50), left: this.getUIContext().px2vp(300), right: this.getUIContext().px2vp(50) } }); Row() { Text(deviceInfo.deviceType) .width(this.getUIContext().px2vp(260)) .textAlign(TextAlign.Center) .fontSize(this.getUIContext().px2fp(60)); Text('使用Image的resizable') .padding({ left: this.getUIContext().px2vp(24) }) .fontSize(this.getUIContext().px2fp(40)); } .margin({ top: this.getUIContext().px2vp(36) }) .width('100%') .justifyContent(FlexAlign.Start); } .alignContent(Alignment.TopStart) .width('90%') .height(200); } .width('100%') .height('100%') .justifyContent(FlexAlign.Center); } }
在手机设备上的效果预览:

在平板设备上的效果预览:
