HarmonyOS .9.png图片类型?

我试了下貌似不支持.9.png,如果碰到了这种需要拉伸的场景图片,应该要怎么处理啊?

阅读 499
1 个回答

当前Image对于.9图的使用是给Image组件设置resizable属性,达到图片局部拉伸的效果,文档可参考:

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-image-V5\#resizable11

@Entry
@Component
export struct Image9SliceSample {
  @State left: number = 30;
  @State right: number = 2;
  @State top: number = 2;
  @State bottom: number = 2;
  @State fit: ImageFit = ImageFit.Cover;
  @State w: number = 100
  @State h: number = 200

  @Builder
  ImageBuilder() {
    Image($r('app.media.image1'))// .objectFit(this.fit)
      // .backgroundColor(Color.Pink)
      // .padding(5)
      .resizable({
        slice: {
          top: `${this.top}px`,
          left: `${this.left}px`,
          bottom: `${this.bottom}px`,
          right: `${this.right}px`,
        }
      })
      .width(`${this.w}px`)
      .height(`${this.h}px`)
  }
  build() {
    Column({ space: 20 }) {

      Text("123")
        .background(this.ImageBuilder)
        .width(`${this.w}px`)
        .height(`${this.h}px`);
      Blank().height(80)// Image($r('app.media.wph'))
        // .objectFit(this.fit)
        // .backgroundColor(Color.Pink)
        // .padding(5)
        // .resizable({
        // slice: {
        // top: `${this.top}px`,
        // left: `${this.left}px`,
        // bottom: `${this.bottom}px`,
        // right: `${this.right}px`,
        // }
        // })
        // .width(`${this.w}px`)
        .height(`${this.h}px`)
      Row() {
        Button("add top to " + this.top).fontSize(10)
          .onClick(() => {
            this.top += 1
          })
        Button("add bottom " + this.bottom).fontSize(10)
          .onClick(() => {
            this.bottom += 1
          })
        Button("add left " + this.left).fontSize(10)
          .onClick(() => {
            this.left += 1
          })
        Button("add right " + this.right).fontSize(10)
          .onClick(() => {
            this.right += 1
          })
      }


      Row() {
        Button("reduce top to " + this.top).fontSize(10)
          .onClick(() => {
            this.top -= 1
          })
        Button("reduce bottom " + this.bottom).fontSize(10)
          .onClick(() => {
            this.bottom -= 1
          })
        Button("reduce left " + this.left).fontSize(10)
          .onClick(() => {
            this.left -= 1
          })
        Button("reduce right " + this.right).fontSize(10)
          .onClick(() => {
            this.right -= 1
          })
      }

      Row() {
        Button("add w").fontSize(10)
          .onClick(() => {
            this.w += 10
          })
        Button("add h").fontSize(10)
          .onClick(() => {
            this.h += 10
          })
      }

    }
    .backgroundColor(Color.Orange)
    .width('100%').height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进