HarmonyOSBaseLibs/src/main/ets/components/AutoImage.ets
徐勤民 741555107f feat(feedback): 新增意见反馈页面及图片自适应组件
- 创建 FeedbackCommonView 页面,包含反馈列表展示与空状态处理
- 添加点击反馈图标跳转至意见反馈页面的功能
- 在 HomeTopView 中新增反馈入口图标及点击事件
- 实现 AutoImage 组件,支持固定高度、宽度自适应的图片显示
- 更新路由配置,注册 Feedback 页面路径
- 完善基础库文档与导出模块,新增 AutoImage说明与引用
2025-09-25 16:29:14 +08:00

37 行
1.1 KiB
Plaintext

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

// components/AutoImage.ets
interface ImgSize {
width: number
height: number
}
@Component
export struct AutoImage {
@Prop src: PixelMap | ResourceStr | DrawableDescriptor
@Prop fixedHeight: number = 120 // 固定高度px
@State imgWidth: number = 0 // 按比例算出的宽度
private naturalWidth: number = 0
private naturalHeight: number = 0
// 图片加载完成时,获取原图宽高并算宽度
onImageLoad(info: ImgSize) {
this.naturalWidth = info.width
this.naturalHeight = info.height
if (this.fixedHeight > 0 && this.naturalHeight > 0) {
// 按比例计算宽度
this.imgWidth = this.fixedHeight * (this.naturalWidth / this.naturalHeight)
}
}
build() {
Image(this.src)
.width(this.imgWidth > 0 ? this.imgWidth : 0)// 动态计算宽度
.height(this.fixedHeight)// 固定高度
.objectFit(ImageFit.Fill)// 保持比例用Fill/Cover/Contain自行选择
.onComplete((info) => {
if (info) {
this.onImageLoad({ width: info.width, height: info.height })
}
})
}
}