参考demo:@Entry @Component struct SetSlice { @State top: number = 0 @State bottom: number = 0 @State left: number = 0 @State right: number = 0 @State fit: ImageFit = ImageFit.Cover // 图片的原本宽高 宽472px高200px @State w: number = px2vp(472) @State h: number = px2vp(200) build() { Column({ space: 10 }) { Stack() { Image($r('app.media.bubble3')) .width(this.w) .height(this.h) .borderRadius(4) .resizable({ slice: { top: this.top, bottom: this.bottom, left: this.left, right: this.right } }) .objectFit(this.fit) Divider().strokeWidth(1).color(Color.Red) .position({ top: this.top }) Divider().strokeWidth(1).color(Color.Yellow) .position({ bottom: this.bottom }) Divider().strokeWidth(1).color(Color.Blue).vertical(true) .position({ left: this.left }) Divider().strokeWidth(1).color(Color.Green).vertical(true) .position({ right: this.right }) } .width(this.w) .height(this.h) Stack() { Text() .width(this.w) .height(this.h) .borderRadius(4) .backgroundImage($r('app.media.bubble3')) .backgroundImageSize({ width: this.w, height: this.h }) .backgroundImageResizable({ slice: { top: this.top, bottom: this.bottom, left: this.left, right: this.right } }) Divider().strokeWidth(1).color(Color.Red) .position({ top: this.top }) Divider().strokeWidth(1).color(Color.Yellow) .position({ bottom: this.bottom }) Divider().strokeWidth(1).color(Color.Blue).vertical(true) .position({ left: this.left }) Divider().strokeWidth(1).color(Color.Green).vertical(true) .position({ right: this.right }) } .width(this.w) .height(this.h) Row() { Button('add top to ' + this.top).fontSize(10) .onClick(() => { this.top += 2 }) Button('add bottom ' + this.bottom).fontSize(10) .onClick(() => { this.bottom += 2 }) Button('add left ' + this.left).fontSize(10) .onClick(() => { this.left += 2 }) Button('add right ' + this.right).fontSize(10) .onClick(() => { this.right += 2 }) } Row() { Button('reduce top to ' + this.top).fontSize(10) .onClick(() => { this.top -= 2 }) Button('reduce bottom ' + this.bottom).fontSize(10) .onClick(() => { this.bottom -= 2 }) Button('reduce left ' + this.left).fontSize(10) .onClick(() => { this.left -= 2 }) Button('reduce right ' + this.right).fontSize(10) .onClick(() => { this.right -= 2 }) } Row() { Button('add w' + this.w).fontSize(10) .onClick(() => { this.w += 2 }) Button('add h' + this.h).fontSize(10) .onClick(() => { this.h += 2 }) } Row() { Button('reduce w' + this.w).fontSize(10) .onClick(() => { this.w -= 2 }) Button('reduce h' + this.h).fontSize(10) .onClick(() => { this.h -= 2 }) } } .width('100%') .height('100%') .backgroundColor(Color.Pink) .padding({ top: 10 }) } }
参考demo: