• 【Harmony OS】【ARK UI】【Demo】加载动画实现


     在HarmonyOS 中加载动画实现是很常见的技术,今天分享怎么实现加载动画的功能,分为“准备阶段”,“代码实现”,“运行效果”三步进行实现。

    1.准备阶段

    我们需要准备一张加载的图片(如下图所示),把图片放在resource/base/media/目录下(如图所示),我们还需要学习“显式动画”,“组件内转场”,“ 定时器”这个三篇资料

    image.png

    image.png

    2. 代码实现
    2.1绘画界面
    一个张图片显示加载,一个text显示开启动画,一个text显示关闭动画,代码和效果如下,

    1. @Entry
    2. @Component
    3. struct LoadAnimation {
    4. build() {
    5. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
    6. Image($r("app.media.loading"))
    7. .objectFit(ImageFit.Contain)
    8. .height(40)
    9. .aspectRatio(1)
    10. .width(40)
    11. .margin({ bottom: 5 })
    12. Text("开始动画").width("100%").height(100)
    13. .textAlign(TextAlign.Center).backgroundColor(Color.Red)
    14. .fontColor(Color.White).fontSize(20)
    15. .margin(10)
    16. Text("结束动画").width("100%").height(100)
    17. .textAlign(TextAlign.Center).backgroundColor(Color.White)
    18. .fontColor(Color.Black).fontSize(20)
    19. .margin(10)
    20. }
    21. .width('100%')
    22. .height('100%')
    23. }
    24. }

    image.png

    2.2 给Image添加旋转属性,参考资料“组件内转场”的rotate属性,代码如下

    image.png

    1. Image($r("app.media.loading"))
    2. .objectFit(ImageFit.Contain)
    3. .height(40)
    4. .aspectRatio(1)
    5. .width(40)
    6. .margin({ bottom: 5 })
    7. .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })

    2.3实现开启动画
    实现这个功能我们需要学习“显式动画”的animateTo的使用方法和“定时器”的setInterval功能实现

    image.png

    image.png

    代码如下

    1. startRotate() {
    2. this.rotateTimeOut = setInterval(() => {
    3. this.rotateAngle = 0
    4. animateTo({ duration: 800 }, () => {
    5. this.rotateAngle = 360
    6. })
    7. }, 800)
    8. }

    2.4关闭动画按钮点击实现实现
    我们清除定时器就可以了,代码如下

    1. clearAnimation(){
    2. clearInterval(this.rotateTimeOut)
    3. }

    3.运行效果

    3.1全部代码如下

    1. @Entry
    2. @Component
    3. struct LoadAnimation {
    4. @State rotateAngle:number=0
    5. private rotateTimeOut: any //计时器
    6. startRotate() {
    7. this.rotateTimeOut = setInterval(() => {
    8. this.rotateAngle = 0
    9. animateTo({ duration: 800 }, () => {
    10. this.rotateAngle = 360
    11. })
    12. }, 800)
    13. }
    14. clearAnimation(){
    15. clearInterval(this.rotateTimeOut)
    16. }
    17. build() {
    18. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
    19. Image($r("app.media.loading"))
    20. .objectFit(ImageFit.Contain)
    21. .height(40)
    22. .aspectRatio(1)
    23. .width(40)
    24. .margin({ bottom: 5 })
    25. .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })
    26. Text("开始动画").width("100%").height(100)
    27. .textAlign(TextAlign.Center).backgroundColor(Color.Red)
    28. .fontColor(Color.White).fontSize(20)
    29. .margin(10)
    30. .onClick(this.startRotate.bind(this))
    31. Text("结束动画").width("100%").height(100)
    32. .textAlign(TextAlign.Center).backgroundColor(Color.White)
    33. .fontColor(Color.Black).fontSize(20)
    34. .margin(10)
    35. .onClick(this.clearAnimation.bind(this))
    36. }
    37. .width('100%')
    38. .height('100%')
    39. }
    40. }

    3.2运行效果

    20220209-170716(WeLinkPC).gif

     

    更多相关学习资料:
    https://developer.huawei.com/consumer/cn/forum/topic/0202798102556350281?fid=0102683795438680754?ha_source=zzh
  • 相关阅读:
    数据库事务到底是什么?
    Python 1-06 练习
    @keyframes css3动画技巧与代码实例
    我的第一个项目(十二) :分数和生命值的更新(后端增删查改的"改")
    宝妈怎样边带娃边赚钱?
    Ubuntu 与Windows 之间搭建共享文件夹
    语音机器人空号识别介绍
    矩阵分析与应用
    【OpenCV 例程200篇】208. Photoshop 对比度自动调整算法
    如何理解FFT中的频谱泄露效应?
  • 原文地址:https://blog.csdn.net/weixin_44708240/article/details/125888910