【每日学点HarmonyOS Next知识】获取页面堆栈对象、画布动画、安全按钮、图片组件修改图片颜色、设置弹窗只在当前页面展示

[复制链接]
发表于 2025-7-16 07:26:09 | 显示全部楼层 |阅读模式
1、HarmonyOS 如何获取到页面堆栈对象?

在通过 router 如何获取当前页面层级的环境,以及获取页面层级对象,例如 页面 A - B - C ,在C 页面是可以获取到当前页面层级的list,并且获取到对应页面对象
目前只能获取到router页面栈内当前页面的信息如index,路径,页面名称,和页面站内页面的数量,暂不支持获取到页面栈的list。
现在模块内和模块间的跳转都会能用navigation实现的都只管用navigation实现,因为后续router会停止演进,后续大概不会新增能力。模块间的跳转,用navigation导致的耦合精密的问题,可以通过动态import来解决。目前router不支持对页面栈举行精细操作,在清除栈方面,如A-B-B1不支持在B1页面时清除B页面,只支持使用claer清算掉当前页面以外的所有页面。router不支持跳过最大限度32.页面栈的最大容量为32个页面。router路由栈只支持获取栈顶页面属性,仅包含当前页面路径,页面文件名。navigation后续会继承演进,如果有能力为得到满意可以继承规划需求。navigation中可以通过getAllPathName 获取栈中所有NavDestination页面的名称,(具体可参考如下链接下的示例2:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-navigation-V5#getallpathname10)
  1. // Index.ets
  2. @Entry
  3. @Component
  4. struct NavigationExample {
  5.   pageInfos: NavPathStack = new NavPathStack()
  6.   isUseInterception: boolean = false;
  7.   registerInterception() {
  8.     this.pageInfos.setInterception({
  9.       // 页面跳转前拦截,允许操作栈,在当前跳转中生效。
  10.       willShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar",
  11.         operation: NavigationOperation, animated: boolean) => {
  12.         if (!this.isUseInterception) {
  13.           return;
  14.         }
  15.         if (typeof to === "string") {
  16.           console.log("target page is navigation home");
  17.           return;
  18.         }
  19.         // 重定向目标页面,更改为pageTwo页面到pageOne页面。
  20.         let target: NavDestinationContext = to as NavDestinationContext;
  21.         if (target.pathInfo.name === 'pageTwo') {
  22.           target.pathStack.pop();
  23.           target.pathStack.pushPathByName('pageOne', null);
  24.         }
  25.       },
  26.       // 页面跳转后回调,在该回调中操作栈在下一次跳转中刷新。
  27.       didShow: (from: NavDestinationContext | "navBar", to: NavDestinationContext | "navBar",
  28.         operation: NavigationOperation, isAnimated: boolean) => {
  29.         if (!this.isUseInterception) {
  30.           return;
  31.         }
  32.         if (typeof from === "string") {
  33.           console.log("current transition is from navigation home");
  34.         } else {
  35.           console.log(`current transition is from  ${(from as NavDestinationContext).pathInfo.name}`)
  36.         }
  37.         if (typeof to === "string") {
  38.           console.log("current transition to is navBar");
  39.         } else {
  40.           console.log(`current transition is to ${(to as NavDestinationContext).pathInfo.name}`);
  41.         }
  42.       },
  43.       // Navigation单双栏显示状态发生变更时触发该回调。
  44.       modeChange: (mode: NavigationMode) => {
  45.         if (!this.isUseInterception) {
  46.           return;
  47.         }
  48.         console.log(`current navigation mode is ${mode}`);
  49.       }
  50.     })
  51.   }
  52.   build() {
  53.     Navigation(this.pageInfos) {
  54.       Column() {
  55.         Button('pushPath', { stateEffect: true, type: ButtonType.Capsule })
  56.           .width('80%')
  57.           .height(40)
  58.           .margin(20)
  59.           .onClick(() => {
  60.             this.pageInfos.pushPath({ name: 'pageOne' }) //将name指定的NavDestination页面信息入栈
  61.           })
  62.         Button('use interception', { stateEffect: true, type: ButtonType.Capsule })
  63.           .width('80%')
  64.           .height(40)
  65.           .margin(20)
  66.           .onClick(() => {
  67.             this.isUseInterception = !this.isUseInterception;
  68.             if (this.isUseInterception) {
  69.               this.registerInterception();
  70.             } else {
  71.               this.pageInfos.setInterception(undefined);
  72.             }
  73.           })
  74.       }
  75.     }.title('NavIndex')
  76.   }
  77. }
复制代码
2、HarmonyOS 基于drawing.canvas实现的动画demo?

可参考:https://developer.huawei.com/consumer/cn/blog/topic/03797837883380062
3、HarmonyOS 安全按钮返回权限失败?

安全按钮返回权限失败,result 返回TEMPORARY_AUTHORIZATION_FAILED,如何解决
  1. // 默认参数下,图标、文字、背景都存在
  2. SaveButton().onClick(async (event:ClickEvent, result:SaveButtonOnClickResult) => {
  3.   if (result == SaveButtonOnClickResult.SUCCESS) {}
  4. }
复制代码
使用uri打开文件,可以连续写入内容,写入过程不受时间限定 let file = await fs.open(uri, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);这个是需要对应权限的,需要在modules.json5中定义对应的权限 { "name": "ohos.permission.WRITE\_MEDIA", "reason": "$string:EntryAbility\_desc", "usedScene": { "abilities": [ "EntryAbility" ], "when": "always" } }
4、HarmonyOS Image组件设置了本地资源文件后,可以动态修改图片的颜色吗?

Image组件设置了本地资源文件后,可以动态修改图片的颜色吗
参考以下文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-image-V5#colorfilter9
colorFilter(value: ColorFilter | DrawingColorFilter)
为图像设置颜色滤镜效果。
设置该属性时,renderMode属性设置不见效。
参数名类型必填阐明valueColorFilter | DrawingColorFilter是1. 给图像设置颜色滤镜效果,入参为一个的4x5的RGBA转换矩阵。

矩阵第一行表现R(红色)的向量值,第二行表现G(绿色)的向量值,第三行表现B(蓝色)的向量值,第四行表现A(透明度)的向量值,4行分别代表不同的RGBA的向量值。

当矩阵对角线值为1,别的值为0时,保持图片原有色彩。

计算规则:

如果输入的滤镜矩阵为:



像素点为[R, G, B, A]

则过滤后的颜色为 [R’, G’, B’, A’]



2. 从API Version12开始支持@ohos.graphics.drawing的ColorFilter类型作为入参。

阐明:
API Version 11及之前,svg类型图源不支持该属性。
API version 12开始,该接口中的DrawingColorfilter类型支持在元服务中使用。此中,svg类型的图源需具有stroke属性。 5、HarmonyOS 如何设置popup 只在当前页面展示?

正在使用popup 作为一个收到关照一个弹层提示,A页面 show出popup, A页已经跳转到B页面了,在显示 popup时,popup会出现在B页面上,是否可以设置仅在A页面展示
popup目前行为确实云云应用可以使用CustomDialog来实现,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-common-components-custom-dialog-V5
示例demo:
  1. import router from '@ohos.router';
  2. @CustomDialog
  3. export default struct UserPrivacyDialog {
  4.   controller: CustomDialogController = new CustomDialogController({ builder: '' });
  5.   cancel: Function = () => {
  6.   };
  7.   confirm: Function = () => {
  8.   };
  9.   visible: Visibility = Visibility.None
  10.   build() {
  11.     Column() {
  12.       Text('我是弹窗')
  13.       Button('jump')
  14.         .onClick(() => {
  15.           router.pushUrl({
  16.             url: 'pages/Second'
  17.           })
  18.         }).backgroundColor(0xffffff).fontColor(Color.Red)
  19.     }
  20.     .margin({ top: 22 })
  21.     .justifyContent(FlexAlign.SpaceEvenly)
  22.   }
  23. }
  24. @Entry
  25. @Component
  26. struct Index {
  27.   @State textValue: string = 'Hello World'
  28.   @State visible: Visibility = Visibility.None
  29.   build() {
  30.     Stack() {
  31.       Row() {
  32.         Column() {
  33.           Button('click')
  34.             .onClick(() => {
  35.               if (this.visible == Visibility.Visible) {
  36.                 this.visible = Visibility.None
  37.               } else {
  38.                 this.visible = Visibility.Visible
  39.               }
  40.             })
  41.             .backgroundColor(0x777474)
  42.             .fontColor(0x000000)
  43.         }
  44.         .width('100%')
  45.       }
  46.       .height('100%')
  47.       .backgroundColor(0x885555)
  48.       Text('')
  49.         .onClick(() => {
  50.           if (this.visible == Visibility.Visible) {
  51.             this.visible = Visibility.None
  52.           } else {
  53.             this.visible = Visibility.Visible
  54.           }
  55.         })
  56.         .width('100%')
  57.         .height('100%')// 透明度可以自己调节一下
  58.         .opacity(0.16)
  59.         .backgroundColor(0x000000)
  60.         .visibility(this.visible)
  61.       Column() {
  62.         GridRow({
  63.           columns: { xs: 1, sm: 4, md: 8, lg: 12 },
  64.           breakpoints: {
  65.             value: ["400vp", "600vp", "800vp"],
  66.             reference: BreakpointsReference.WindowSize
  67.           },
  68.         }) {
  69.           GridCol({
  70.             span: { xs: 1, sm: 2, md: 4, lg: 8 },
  71.             offset: { xs: 0, sm: 1, md: 2, lg: 2 }
  72.           }) {
  73.             Column() {
  74.               Flex({ justifyContent: FlexAlign.SpaceAround }) {
  75.                 UserPrivacyDialog();
  76.               }.margin({ bottom: 10 })
  77.             }
  78.             .backgroundColor(0xffffff)
  79.             .visibility(this.visible)
  80.             .clip(true)
  81.             .borderRadius(20)
  82.           }
  83.         }
  84.       }.width('95%') //设置弹窗宽度
  85.     }
  86.   }
  87. }
复制代码
componentUtils.getRectangleById参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-arkui-componentutils-V5#componentutilsgetrectanglebyid
getRectangleById(id: string): ComponentInfo
根据组件ID获取组件实例对象, 通过组件实例对象将获取的坐标位置和巨细同步返回给开辟者。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表