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
在HarmonyOS中,图片局部拉伸功能通过component.ScaleType
实现。Next版本默认使用FIT_XY
模式会整体拉伸,若需局部拉伸可使用CENTER_CROP
或自定义Canvas
绘制。目前API未直接提供局部拉伸参数,开发者需通过计算目标区域和源区域比例手动实现。这与鸿蒙的组件化设计理念有关,保持图像完整性的同时提供基础缩放选项。
更多关于HarmonyOS鸿蒙Next中为什么只有图片局部【不拉伸】,没有图片局部【拉伸】?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html