鸿蒙基本UI控件(Image相关-含权限配置流程)

项目创建传送门:

鸿蒙基本UI控件(项目创建流程&&Text相关)https://blog.csdn.net/qq_39731011/article/details/157513416?spm=1001.2014.3001.5501

0、可选项:如需加载网络图片需要增加权限(sec/main/module.json5)

TypeScript 复制代码
{
  "module": {
    "requestPermissions": [
      {
        "name": "ohos.permission.INTERNET"
      }
    ],
    
    ...
    
  }
}

1、代码示例:pages目录下的Index.ets

TypeScript 复制代码
@Entry
@Component
struct Index {
  @State message: string = '默认文案';
  @State input: string = '默认输入框内容';

  setText(value: string) { //将输入框的内容赋值到第一个TextView
    this.message = value;
    if (value == "") {
      this.message = '默认文案'
    }
  }

  build() {
    Row() { //水平居中
      Column() { //垂直居中
//----------------------------------文本与输入框------------------------------------        
        Text(this.message)
          .fontSize($r('app.float.page_text_font_size'))
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            this.message = 'Welcome';
          })
        Text('(点击恢复默认文案)')
          .fontSize('28fp')
          .fontWeight(FontWeight.Bold)
          .background($r('app.color.textBack'))
          .onClick(() => {
            this.message = '默认文案';
          })
          .id("Two")
        //多行输入框
        TextArea({
          text: this.input,
          placeholder: "请输入文本"
        })
          .fontSize(34)
          .onChange((value: string) => {
            //将输入框的内容赋值到第一个TextView
            this.setText(value)
          })
//-------------------------------------图片相关---------------------------------
        //图片
        Image($rawfile('test_img.jpg'))
          .height('140fp')
        //图片(网络)
        Image("https://i-blog.csdnimg.cn/direct/cd011875ce49498eb901a67ec64a4626.jpeg")
          .height('140fp')
          .margin({
            top: 10
          })

      }
      .width('100%')
    }
    .height('100%')
  }
}

2、运行结果:

3、图片加载的模式举例:

  • Contain:保持宽高比例进行缩小或者放大,使得图片完全显示在显示边界内
  • Cover(默认):保持宽高比进行缩小或者方法,使得图片两边都大于或等于显示边界
  • Auto:适应显示
  • Fill:不保持宽高比进行放大或缩小,使图片充满显示边界
  • ScaleDown:保持宽高比显示,图片保持或者保持不变
TypeScript 复制代码
        Image($rawfile('test_img.jpg'))
          .height('140fp')
          .width(140)
          .objectFit(ImageFit.Fill)
相关推荐
2501_920627611 天前
Flutter 框架跨平台鸿蒙开发 - 古文学习应用
学习·flutter·harmonyos
芙莉莲教你写代码1 天前
Flutter 框架跨平台鸿蒙开发 - 魔术教学
flutter·华为·harmonyos
纯爱掌门人1 天前
鸿蒙文件预览开发实践:从打开文件到加速感知
华为·harmonyos
云和数据.ChenGuang1 天前
当智能体遇上原生鸿蒙:开启下一代操作系统的“智慧觉醒”
华为·harmonyos
左手厨刀右手茼蒿1 天前
Flutter 三方库 bs58 的鸿蒙化适配指南 - 在鸿蒙系统上构建极致、高效的 Base58 数字货币与区块链数据编解码引擎
flutter·harmonyos·鸿蒙·openharmony
加农炮手Jinx1 天前
Flutter 组件 substrate_bip39 的适配 鸿蒙Harmony 实战 - 驾驭区块链级助记词原语、实现鸿蒙端金融级 BIP39 安全私钥推导方案
flutter·harmonyos·鸿蒙·openharmony·substrate_bip39
左手厨刀右手茼蒿1 天前
Flutter 组件 substrate_bip39 的适配 鸿蒙Harmony 实战 - 驾驭区块链级 BIP39 安全底座、实现鸿蒙端私钥派生与国密级密钥保护方案
flutter·harmonyos·鸿蒙·openharmony·substrate_bip39
加农炮手Jinx1 天前
Flutter 三方库 fast_base58 的鸿蒙化进阶指南 - 挑战编解码吞吐量极限、助力鸿蒙端大规模区块链与分布式存储数据处理
flutter·harmonyos·鸿蒙·openharmony·fast_base58
里欧跑得慢1 天前
Flutter 三方库 ethereum 鸿蒙分布式区块链数字资产上链钱包适配突破:接通 JSON-RPC 加密管线深入打通智能合约闭环实现高价值数字加密交互-适配鸿蒙 HarmonyOS ohos
分布式·flutter·harmonyos
2501_920627611 天前
Flutter 框架跨平台鸿蒙开发 - 压力管理助手应用
flutter·华为·harmonyos