首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

HarmonyOS开发实战( Beta5版)合理使用动画丢帧规范实践

  • 25-03-03 06:42
  • 4416
  • 13228
blog.csdn.net

本文列举了部分用于优化动画时延的正反案例,帮助开发者在遇到相似场景时进行优化,解决构建页面动画时遇到动画时延较长的问题。

减少动画丢帧

在播放动画或者生成动画时,画面产生停滞而导致帧率过低的现象,称为动画丢帧。

播放动画时,系统需要在一个刷新周期内完成动画变化曲线的计算,完成组件布局绘制等操作。建议使用系统提供的动画接口,只需设置曲线类型、终点位置、时长等信息,就能够满足常用的动画功能,减少UI主线程的负载。

反例:应用使用了自定义动画,动画曲线计算过程很容易引起UI线程高负载,易导致丢帧。

  1. @Entry
  2. @Component
  3. struct AttrAnimationExample0 {
  4. @State widthSize: number = 200
  5. @State heightSize: number = 100
  6. @State flag: boolean = true
  7. computeSize() {
  8. let duration = 2000
  9. let period = 16
  10. let widthSizeEnd = 0
  11. let heightSizeEnd = 0
  12. if (this.flag) {
  13. widthSizeEnd = 100
  14. heightSizeEnd = 50
  15. } else {
  16. widthSizeEnd = 200
  17. heightSizeEnd = 100
  18. }
  19. let doTimes = duration / period
  20. let deltaHeight = (heightSizeEnd - this.heightSize) / doTimes
  21. let deltaWeight = (widthSizeEnd - this.widthSize) / doTimes
  22. for (let i = 1; i <= doTimes; i++) {
  23. let t = period * (i);
  24. setTimeout(() => {
  25. this.heightSize = this.heightSize + deltaHeight
  26. this.widthSize = this.widthSize + deltaWeight
  27. }, t)
  28. }
  29. this.flag = !this.flag
  30. }
  31. build() {
  32. Column() {
  33. Button('click me')
  34. .onClick(() => {
  35. let delay = 500
  36. setTimeout(() => { this.computeSize() }, delay)
  37. })
  38. .width(this.widthSize).height(this.heightSize).backgroundColor(0x317aff)
  39. }.width('100%').margin({ top: 5 })
  40. }
  41. }

img

使用系统提供的属性动效API

建议:通过系统提供的属性动效API实现上述动效功能。

  1. @Entry
  2. @Component
  3. struct AttrAnimationExample1 {
  4. @State widthSize: number = 200
  5. @State heightSize: number = 100
  6. @State flag: boolean = true
  7. build() {
  8. Column() {
  9. Button('click me')
  10. .onClick((event?: ClickEvent | undefined) => {
  11. if (this.flag) {
  12. this.widthSize = 100
  13. this.heightSize = 50
  14. } else {
  15. this.widthSize = 200
  16. this.heightSize = 100
  17. }
  18. this.flag = !this.flag
  19. })
  20. .width(this.widthSize).height(this.heightSize).backgroundColor(0x317aff)
  21. .animation({
  22. duration: 2000, // 动画时长
  23. curve: Curve.Linear, // 动画曲线
  24. delay: 500, // 动画延迟
  25. iterations: 1, // 播放次数
  26. playMode: PlayMode.Normal // 动画模式
  27. }) // 对Button组件的宽高属性进行动画配置
  28. }.width('100%').margin({ top: 5 })
  29. }
  30. }

img

更详细的API文档请参考:属性动画。

使用系统提供的显式动效API

建议:通过系统提供的显式动效API实现上述动效功能。

  1. @Entry
  2. @Component
  3. struct AnimateToExample2 {
  4. @State widthSize: number = 200;
  5. @State heightSize: number = 100;
  6. @State flag: boolean = true;
  7. build() {
  8. Column() {
  9. Button('click me')
  10. .onClick((event?: ClickEvent | undefined) => {
  11. if (this.flag) {
  12. animateTo({
  13. duration: 2000, // 动画时长
  14. curve: Curve.Linear, // 动画曲线
  15. delay: 500, // 动画延迟
  16. iterations: 1, // 播放次数
  17. playMode: PlayMode.Normal // 动画模式
  18. }, () => {
  19. this.widthSize = 100;
  20. this.heightSize = 50;
  21. })
  22. } else {
  23. animateTo({
  24. duration: 2000, // 动画时长
  25. curve: Curve.Linear, // 动画曲线
  26. delay: 500, // 动画延迟
  27. iterations: 1, // 播放次数
  28. playMode: PlayMode.Normal // 动画模式
  29. }, () => {
  30. this.widthSize = 200;
  31. this.heightSize = 100;
  32. })
  33. }
  34. this.flag = !this.flag;
  35. })
  36. .width(this.widthSize).height(this.heightSize).backgroundColor(0x317aff)
  37. }.width('100%').margin({ top: 5 })
  38. }
  39. }

img

更详细的API文档请参考:显式动画。

优化效果

相比于自定义动画,使用系统提供的动效API可提高动画帧数,提高应用性能。

动画实现方式帧数(fps)
自定义动画60
属性动效API120
显式动效API120

合理设置隐式动画

Tabs组件在不为BottomTabBarStyle样式时,切换页面时默认加载300ms的隐式动画,如果开发场景不需要该动画效果,会因默认加载导致页面跳转完成时延变长,此时可手动设置animationDuration减少动画完成时延。下述正反示例分别为100ms和1000ms的动画时延:

反例:

  1. @Entry
  2. @Component
  3. struct TabsExample {
  4. ...
  5. private controller: TabsController = new TabsController();
  6. build() {
  7. Column() {
  8. Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
  9. TabContent()
  10. TabContent()
  11. // ...
  12. }
  13. // ...
  14. // 设置Tabs页面跳转的动画时长为1000ms
  15. .animationDuration(1000)
  16. }
  17. .width('100%')
  18. }
  19. }

img

正例:

  1. @Entry
  2. @Component
  3. struct TabsExample {
  4. ...
  5. private controller: TabsController = new TabsController();
  6. build() {
  7. Column() {
  8. Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
  9. TabContent()
  10. TabContent()
  11. // ...
  12. }
  13. // ...
  14. // 设置Tabs页面跳转的动画时长为100ms
  15. .animationDuration(100)
  16. }
  17. .width('100%')
  18. }
  19. }

img

优化效果

优化前 1000ms优化后 100ms

img

img

上述示例通过减少animationDuration数值,减少Tabs切换完成时延。当数值设置为0且TabBar不为BottomTabBarStyle样式时,隐式动效延时为默认的300ms。开发者可根据实际场景适当减少隐式动效时延,如果应用没有特殊的动效要求时,建议设置数值为1,减少阻塞主线程,提高应用性能。

更详细的API文档请参考:Tabs-animationduration。

合理设置动效时长

滚动类组件可使用fling方法按传入的初始速度进行惯性滚动,不合理的滚动速度设置可能导致动效时长过长,此时应通过加快滚动速度减少动效时长。下述正反示例通过改变List组件惯性滚动速度减少动效时长:

反例:

  1. @Entry
  2. @Component
  3. struct ListExample {
  4. scrollerForList: Scroller = new Scroller();
  5. build() {
  6. Column() {
  7. Button('Fling100')
  8. .onClick(() => {
  9. // 设置当前滚动初始速度为100vp/s
  10. this.scrollerForList.fling(100);
  11. })
  12. List({ space: 20, initialIndex: 0, scroller: this.scrollerForList }) {
  13. // ...
  14. }
  15. }
  16. }
  17. }

正例:

  1. @Entry
  2. @Component
  3. struct ListExample {
  4. scrollerForList: Scroller = new Scroller();
  5. build() {
  6. Column() {
  7. Button('Fling100')
  8. .onClick(() => {
  9. // 设置当前滚动初始速度为10000vp/s
  10. this.scrollerForList.fling(10000);
  11. })
  12. List({ space: 20, initialIndex: 0, scroller: this.scrollerForList }) {
  13. // ...
  14. }
  15. }
  16. }
  17. }

优化效果

100vp/s:

img

10000vp/s:

img

示例动效耗时(ms)
优化前392
优化后200

上述示例在提高滚动速度到10000vp/s后,相比100vp/s减少了200ms的动画时延。开发者可根据实际场景适当增加滚动速度,在不影响页面效果的情况下减少页面完成时延,提高应用性能。

最后

小编在之前的鸿蒙系统扫盲中,有很多朋友给我留言,不同的角度的问了一些问题,我明显感觉到一点,那就是许多人参与鸿蒙开发,但是又不知道从哪里下手,因为资料太多,太杂,教授的人也多,无从选择。有很多小伙伴不知道学习哪些鸿蒙开发技术?不知道需要重点掌握哪些鸿蒙应用开发知识点?而且学习时频繁踩坑,最终浪费大量时间。所以有一份实用的鸿蒙(HarmonyOS NEXT)文档用来跟着学习是非常有必要的。 

为了确保高效学习,建议规划清晰的学习路线,涵盖以下关键阶段:

GitCode - 全球开发者的开源社区,开源代码托管平台  希望这一份鸿蒙学习文档能够给大家带来帮助~


鸿蒙(HarmonyOS NEXT)最新学习路线

​

该路线图包含基础技能、就业必备技能、多媒体技术、六大电商APP、进阶高级技能、实战就业级设备开发,不仅补充了华为官网未涉及的解决方案

路线图适合人群:

IT开发人员:想要拓展职业边界
零基础小白:鸿蒙爱好者,希望从0到1学习,增加一项技能。
技术提升/进阶跳槽:发展瓶颈期,提升职场竞争力,快速掌握鸿蒙技术

2.视频学习教程+学习PDF文档

HarmonyOS Next 最新全套视频教程

  纯血版鸿蒙全套学习文档(面试、文档、全套视频等)       

​​

总结

参与鸿蒙开发,你要先认清适合你的方向,如果是想从事鸿蒙应用开发方向的话,可以参考本文的学习路径,简单来说就是:为了确保高效学习,建议规划清晰的学习路线

鸿蒙NEXT全套学习资料
微信名片
注:本文转载自blog.csdn.net的让开,我要吃人了的文章"https://blog.csdn.net/weixin_55362248/article/details/141895615"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top