HarmonyOS项目实战:调频声波App(二)UI 原创

大黑布林李子
发布于 2024-1-18 19:16
浏览
0收藏

作者:大李子
团队:坚果派
十年iOS,All in转鸿蒙

调频声波App(一)概述
调频声波App(二)UI
调频声波App(三)播放声音

UI布局

  1. 首先我们实现频率调整的模块

HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区

	Row() {
          Button("-")
            .onClick(async event => {
              const newValue = this.frequency - this.step // 1. 把当前的频率减掉预设的步进
              this.frequency = Math.max(newValue, 0) // 2. 控制频率大于0
              this.updateFrequency() // 3. 让播放器更新频率
            })
            .fontSize(60)
            .fontColor(this.mainColor)
            .backgroundColor("opaque")

          Text(`${this.frequency} Hz`)
            .fontSize(50)
            .fontWeight(FontWeight.Bold)
            .fontColor(this.mainColor)

          Button("+")
            .onClick(async event => {
              const newValue = this.frequency + this.step // 4. 把当前的频率增加预设的步进
              this.frequency = Math.min(newValue, 30000) // 5. 控制频率小于三万
              this.updateFrequency() // 6. 让播放器更新频率
            })
            .fontSize(60)
            .fontColor(this.mainColor)
            .backgroundColor("opaque")
        }
        .margin({ top: "30%" })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  1. 频率下方加入一些使用提示
    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区
	Text("上下滑动屏幕\n以调整频率")
          .fontColor(this.subtitleColor)
          .textAlign(TextAlign.Center)
          .margin({ top: 20 })

	Text(this.readmeRecord ? "使用说明" : "使用必读!")
          .fontColor(this.readmeRecord ? "#2A1EB1" : Color.Red)
          .fontSize(this.readmeRecord ? 16 : 24)
          .margin({ top: 20 })
          .onClick(() => {
            router.pushUrl({ url: 'pages/ReadmePage' }) // 1. 跳转readme界面
            this.readmeRecord = true // 2. 首次使用的时候会使跳转按钮更显眼,跳转过以后就恢复正常UI。用一个state变量来控制显示状态
            preferences.getPreferences(getContext(this), "default").then((preference) => {
              preference.put("readmeRecord", true) // 3. 记录到preference
              preference.flush()
            })
          })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  1. 界面底部的播放/停止按钮
    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区
	Button(this.playing ? "停止" : "播放")
        .fontColor(this.bgColor)
        .fontSize(30)
        .height(60)
        .backgroundColor(this.mainColor)
        .width("100%")
        .type(ButtonType.Normal)
        .onClick(() => {
          this.playing ? this.stop() : this.play()
          this.playing = !this.playing
        })
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

至此,软件的基本功能就架设完成了。接下来还可以加一点实用功能。

  1. 选择波形。由于没有找到类似iOS中的segment组件,这里直接用Text来做手动布局。
    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区
  @Builder
  waveTypeSelector() {
    Row() {
      ForEach(this.waveOptions, (item: string, index: number) => {
        Image(index === this.index ? item[0] : item[1])
          .width(50)
          .height(50)
          .backgroundColor(index === this.index ? this.selectedBgColor : this.mainColor)
          .padding(2)
          .borderRadius({
            topLeft: index === 0 ? 20 : 0, // 1. 第一个选项左边做圆角
            bottomLeft: index === 0 ? 20 : 0,
            topRight: index === this.waveOptions.length - 1 ? 20 : 0, // 2. 最后一个选项右边做圆角
            bottomRight: index === this.waveOptions.length - 1 ? 20 : 0
          })
          .onClick(() => {
            this.setIndex(index)
          })
      }, (item: string) => item)
    }
    .margin({ top: 20 })
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

这是一个独立的模块,最后集成到build()方法里

this.waveTypeSelector()
  • 1.
  1. 管理预设的频率和波形
    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区
    HarmonyOS项目实战:调频声波App(二)UI-鸿蒙开发者社区
  @Builder
  presets() {
    Row() {
      ForEach(this.presetsData, (item: PresetModel, index: number) => {
        Column() {
          if (this.isEditMode) {
            Badge({ // 1. 如果是编辑模式,需要在图标右上角加一个badge,用于删除预设
              value: "X",
              style: {
                badgeColor: Color.Red
              }
            }) {
              this.presetItemImage(this.waveImageFromWaveType(item.waveType)) 
            }
            .onClick(event => {
              if (event.x > 32 && event.y < 16) { // 2. 右上角的badge不能设置点击,需要在整个badge控件上做点击位置判断,如果在badge图标的范围内,就删除预设数组相应位置的数据。
                this.presetsData.splice(index, 1)
              }
            })
          } else { // 3. 如果不是编辑模式,直接显示图片
            Flex() {
              this.presetItemImage(this.waveImageFromWaveType(item.waveType))
            }
            .width(50)
            .height(50)
            .onClick(() => {
              this.index = item.waveType // 4. 不是编辑模式的时候,点击图片,设置当前的波形和频率
              this.frequency = item.frequency
            })
          }
          Text(`${item.frequency} Hz`)
            .fontColor(this.mainColor)
            .fontSize(16)
            .margin({ top: 10 })
        }
        .width(64)
        .height(80)
        .margin({ right:
        index < this.presetsData.length - 1 ? 30 :
          this.isEditMode ? 30 :
            this.isPresetFull() ? 0 : 30 })
      }, (item: string) => item)

      Column() { // 5. 预设数组右边放置一个添加/完成按钮
        Image(this.isEditMode ? $r("app.media.prst_check") : $r("app.media.prst_add"))
          .width(50)
          .height(50)
          .backgroundColor(this.isEditMode ? this.mainColor : this.bgColor)
          .borderColor(this.mainColor)
          .borderWidth(4)
          .borderRadius(25)
          .onClick(() => {
            if (this.isEditMode) { // 6. 编辑模式的时候点击退出编辑模式
              this.isEditMode = false
            } else { // 7. 非编辑模式的时候点击添加预设,添加之后把预设数组写入preference
              if (this.isPresetFull()) {
                return
              }
              this.presetsData.push({ waveType: this.index, frequency: this.frequency })
              preferences.getPreferences(getContext(this), "default").then((preference) => {
                preference.put("presets", JSON.stringify(this.presetsData))
                preference.flush()
              })
            }
          })

        Text(this.isEditMode ? "完成" : "添加预设")
          .fontSize(16)
          .fontColor(this.mainColor)
          .margin({ top: 10 })
      }
      .width(64)
      .height(80)
      .visibility(this.isEditMode ? Visibility.Visible :
        this.isPresetFull() ? Visibility.None : Visibility.Visible) // 8. 预设数量有上限,达到上限以后不显示增加按钮
    }
    .margin({ top: 20 })
  }

  @Builder
  presetItemImage(image: Resource) {
    Image(image)
      .width(50)
      .height(50)
      .backgroundColor(this.mainColor)
      .borderRadius(25)
      .gesture(LongPressGesture()
        .onAction(() => {
          this.isEditMode = true
        })
      )
  }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.

本文的布局代码涉及一些状态变量的声明,本页完整源码请参考Index.ets

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2024-2-4 00:35:18修改
收藏
回复
举报


回复
    相关推荐