【每日学点鸿蒙知识】绘制实心圆、发布报错7014、页面间跳转、PersistentStorage相关问题、应用启动初始化相关动作

发布于:2025-02-11 ⋅ 阅读:(40) ⋅ 点赞:(0)
1、HarmonyOS 如何绘制实心圆,DrawingRenderingContext可以吗?

参考代码

@Entry
@Component
struct CircleByOneHeart {
  @State currentCenterColor: string = '蓝色'
  @State currentUpColor: string = '蓝色'
  @State currentDownColor: string = '蓝色'
  @State currentLeftColor: string = '蓝色'
  @State currentRightColor: string = '蓝色'
  private countDown: number = 0; //设置一个flag,用来接受定时器,可以看作定时器实体

  aboutToDisappear(): void {
    clearTimeout(this.countDown) //销毁定时器
  }

  build() {
    Row() {
      Column({ space: 100 }) {
        //画一个直径为90的同心圆
        Column() {
          Line({
            //上
            width: 10,
            height: 20
          })
            .startPoint([5, 0])
            .endPoint([5, 20])
            .strokeWidth(10)
            .position({ x: 45, y: 5 })
            .stroke(this.currentUpColor == '蓝色' ? Color.Blue : Color.Red)
            .onClick(() => {
              if (this.currentUpColor == '蓝色') {
                this.currentUpColor = '红色'
              } else {
                this.currentUpColor = '蓝色'
              }
              this.countDown = setTimeout(() => {
                this.currentUpColor = '蓝色'
              }, TimeSetting.stayTime) //设置定时器时间以及处理事件
            })
          Line({
            //下
            width: 10,
            height: 20
          })
            .startPoint([5, 0])
            .endPoint([5, 20])
            .strokeWidth(10)
            .position({ x: 45, y: 75 })
            .stroke(this.currentDownColor == '蓝色' ? Color.Blue : Color.Red)
            .onClick(() => {
              if (this.currentDownColor == '蓝色') {
                this.currentDownColor = '红色'
              } else {
                this.currentDownColor = '蓝色'
              }
              this.countDown = setTimeout(() => {
                this.currentDownColor = '蓝色'
              }, TimeSetting.stayTime) //设置定时器时间以及处理事件
            })
          Line({
            //左
            width: 20,
            height: 10
          })
            .startPoint([0, 5])
            .endPoint([20, 5])
            .strokeWidth(10)
            .position({ x: 5, y: 45 })
            .stroke(this.currentLeftColor == '蓝色' ? Color.Blue : Color.Red)
            .onClick(() => {
              if (this.currentLeftColor == '蓝色') {
                this.currentLeftColor = '红色'
              } else {
                this.currentLeftColor = '蓝色'
              }
              this.countDown = setTimeout(() => {
                this.currentLeftColor = '蓝色'
              }, TimeSetting.stayTime) //设置定时器时间以及处理事件
            })
          Line({
            //右
            width: 20,
            height: 10
          })
            .startPoint([0, 5])
            .endPoint([20, 5])
            .strokeWidth(10)
            .position({ x: 75, y: 45 })
            .stroke(this.currentRightColor == '蓝色' ? Color.Blue : Color.Red)
            .onClick(() => {
              if (this.currentRightColor == '蓝色') {
                this.currentRightColor = '红色'
              } else {
                this.currentRightColor = '蓝色'
              }
              this.countDown = setTimeout(() => {
                this.currentRightColor = '蓝色'
              }, TimeSetting.stayTime) //设置定时器时间以及处理事件
            })
          Button('确定')
            .width(50)
            .height(50)
            .type(ButtonType.Circle)
            .position({ x: 25, y: 25 })//绝对定位
            .backgroundColor(this.currentCenterColor == '蓝色' ? Color.Blue : Color.Red)
            .onClick(() => {
              if (this.currentCenterColor == '蓝色') {
                this.currentCenterColor = '红色'
              } else {
                this.currentCenterColor = '蓝色'
              }
              this.countDown = setTimeout(() => {
                this.currentCenterColor = '蓝色'
              }, TimeSetting.stayTime) //设置定时器时间以及处理事件
            })
        }
        .width('100')
        .height('100')
        .backgroundColor('#ffffff')
        .border({
          //border的宽度是向内的,宽度越大占用内部空间越大
          color: Color.Blue,
          width: 10,
          radius: 100
        })
      }
      .width('100%')
      .height('100%')
      .justifyContent(FlexAlign.Center)
    }
    .height('100%')
  }
}

class TimeSetting {
  public static readonly stayTime = 300 //单位是毫秒
}
2、HarmonyOS 发布的时候提示错误7014,Profile创建的时候只能选择两个权限?

错误码:7014,表示:软件包内配置的权限与Profile申请的权限不一致出现此错误,原因是软件包内配置的权限与Profile申请的权限不一致。可以参考文档:https://developer.huawei.com/consumer/cn/doc/app/agc-help-harmonyoserror-0000001651912985#section9225124218158

3、HarmonyOS 登录模块的页面可以跳转到我的模块的页面, 我的模块的页面可以跳转到登录模块页面?

hap包之间可以通过命名路由的方式进行跳转,但router后续不再更新,推荐使用Navigation方式统一进行页面跳转配置。

  • 命名路由:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-router-V5#routerpushnamedroute10
  • Navigation:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5
4、HarmonyOS应该怎么选?

不理解PersistentStorage和Preferences应该怎么选。用户是否点了统一隐私协议;某些功能页面的用户设置;

PersistentStorage主要使用场景是将APPStorage中存储的UI状态持久化,PersistentStorage只能在UIContext明确的地方使用。如果只是想标记一下应用是否点了统一隐私协议,并在首次点了统一隐私协议的时候进行初始化,还是建议使用Preferences;如果需要持久化的状态和UI有关,并希望可以直接在UI组件上使用,可以使用PersistentStorage。

5、HarmonyOS 想把一些请求放在启动项中,需要放在哪里来处理呢?

一些网络请求和三方SDK注册ArkTs放在哪里来初始化这些都是需要在APP启动时就开始,应该是在加载启动页的时候完成,启动页关闭进入APP时这些都结束

应该将SDK初始化写在Ability的onInitialized()方法中。在这个方法里, 可以确保应用程序在启动时只初始化一次SDK,并且避免了在运行时尝试初始化SDK导致的错误。


网站公告

今日签到

点亮在社区的每一天
去签到