HarmonyOS鸿蒙Next中为什么只有图片局部【不拉伸】,没有图片局部【拉伸】?

HarmonyOS鸿蒙Next中为什么只有图片局部【不拉伸】,没有图片局部【拉伸】?

以下是我的测试demo

@Entry
[@Component](/user/Component)
struct ResizeImage01 {
  @State show: Boolean = true
  @State top: number = 1
  @State bottom: number = 1
  @State left: number = 1
  @State right: number = 1
  @State fit: ImageFit = ImageFit.Cover
  @State w: number = 200
  @State h: number = 200

  build() {
    Scroll() {
      Column({ space: 10 }) {
        TestSlider({ title: '顶', targetValue: this.top, max: 200, })
        TestSlider({ title: '底', targetValue: this.bottom, max: 200, })
        TestSlider({ title: '左', targetValue: this.left, max: 200, })
        TestSlider({ title: '右', targetValue: this.right, max: 200, })
        TestSlider({ title: '适应', targetValue: this.fit, max: 16, })
        TestSlider({ title: '宽', targetValue: this.w, max: 300, })
        TestSlider({ title: '高', targetValue: this.h, max: 300, })
        Stack() {
          Image($r("app.media.img_2"))
            .width(this.w)
            .height(this.h)
            .resizable({
              slice: {
                top: this.top,
                bottom: this.bottom,
                left: this.left,
                right: this.right
              }
            })
            .objectFit(this.fit)
            .backgroundColor('#ffffd41d')
          if (this.show) {
            Divider().position({ top: this.top })
              .strokeWidth(1).color(Color.Red)
            Divider().position({ bottom: this.bottom })
              .strokeWidth(1).color(Color.Yellow)
            Divider().position({ left: this.left })
              .strokeWidth(1).color(Color.Blue).vertical(true)
            Divider().position({ right: this.right })
              .strokeWidth(1).color(Color.Green).vertical(true)

          }
        }.width(this.w)
        .height(this.h)

        Text('适应:' + ImageFit[this.fit])
        Text('slice:' + formatJson({
          top: this.top,
          bottom: this.bottom,
          left: this.left,
          right: this.right
        } as EdgeWidths))
        Text('宽:' + this.w)
        Text('高:' + this.h)

        Image($r("app.media.img_2"))

      }.width("100%").height("100%")
      .backgroundColor('#ffe6f1f0')
      .onClick(() => this.show = !this.show)

    }.height('100%')
  }
}

export function formatJson<T>(data: T) { if (data === undefined) { return ‘[未定义数据]’; } if (data === ‘’) { return ‘[空数据]’; }

try { return JSON.stringify(data, null, 2); } catch (e) { const error = e as Error; return [序列化失败: ${error.message}]; } }

@Component export struct TestSlider { @Prop title: string = ‘TestSlider’ @Prop max: number = 100 @Prop min: number = -1 @Link targetValue: number

build() { Row() { Text(this.title) Slider({ value: $$this.targetValue, max: this.max, min: this.min }).width(200) .selectedColor(’#ff6bedff’).showTips(true,this.targetValue.toString()).showSteps(true) } } }


更多关于HarmonyOS鸿蒙Next中为什么只有图片局部【不拉伸】,没有图片局部【拉伸】?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

在HarmonyOS中,图片局部拉伸功能通过component.ScaleType实现。Next版本默认使用FIT_XY模式会整体拉伸,若需局部拉伸可使用CENTER_CROP或自定义Canvas绘制。目前API未直接提供局部拉伸参数,开发者需通过计算目标区域和源区域比例手动实现。这与鸿蒙的组件化设计理念有关,保持图像完整性的同时提供基础缩放选项。

更多关于HarmonyOS鸿蒙Next中为什么只有图片局部【不拉伸】,没有图片局部【拉伸】?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,Image组件的resizable属性确实主要用于定义图片的"九宫格"不拉伸区域,这是通过slice参数指定四个边缘的固定区域来实现的。当前API设计中没有直接提供局部拉伸的功能,这是有意为之的设计选择。

要实现局部拉伸效果,可以通过以下两种方式变通实现:

  1. 使用两个Image组件叠加:

    • 一个作为背景图(设置resizable
    • 另一个作为前景图(需要拉伸的部分)
  2. 通过调整slice参数的值:

    • 将不需要拉伸的区域设为0
    • 这样剩余区域会自动拉伸

例如修改你的demo代码:

.resizable({
  slice: {
    top: 0,  // 顶部可拉伸
    bottom: this.bottom,  // 底部固定
    left: 0,  // 左侧可拉伸 
    right: this.right  // 右侧固定
  }
})

这种设计主要是为了避免图片在拉伸时产生变形失真,保持UI的视觉一致性。如果需要更复杂的图片处理,建议考虑使用Canvas绘图API来实现自定义的绘制逻辑。

回到顶部