当前位置: 首页 > news >正文

郑州网站建设找智巢百度总部客服电话

郑州网站建设找智巢,百度总部客服电话,聊城做网站建设的公司,石家庄视频优化排名代理绘制加载动画:由小圆组成的大圆 1. 定义 LoadingScreen 类2. 实现 _LoadingScreenState 类3. 定义 LoadingPainter 类4. 总结 实现加载动画 我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 Load…

绘制加载动画:由小圆组成的大圆

    • 1. 定义 LoadingScreen 类
    • 2. 实现 _LoadingScreenState 类
    • 3. 定义 LoadingPainter 类
    • 4. 总结

实现加载动画

我们需要定义两个类:LoadingScreen 和 LoadingPainter。LoadingScreen 负责控制动画的状态,而 LoadingPainter 则负责绘制动画。

效果展示视频地址:https://live.csdn.net/v/417383
资源文件下载地址:https://download.csdn.net/download/yang_6799/89639107

1. 定义 LoadingScreen 类

LoadingScreen 类是一个 StatefulWidget,它管理 AnimationController 和 Animation 对象。AnimationController 用于控制动画的播放,Animation 对象则表示动画的具体值。

import 'package:flutter/material.dart';
import 'dart:math';void main() => runApp(MyApp());class MyApp extends StatelessWidget {@overrideWidget build(BuildContext context) {return MaterialApp(home: LoadingScreen(),);}
}

以上代码定义了 MyApp 和 LoadingScreen 两个类,其中 MyApp 是应用的入口点,而 LoadingScreen 则是主要的动画屏幕。

2. 实现 _LoadingScreenState 类

在 LoadingScreen 类中,我们要实现 _LoadingScreenState,它是实际负责动画逻辑的地方。

class LoadingScreen extends StatefulWidget {@override_LoadingScreenState createState() => _LoadingScreenState();
}class _LoadingScreenState extends State<LoadingScreen>with SingleTickerProviderStateMixin {AnimationController? _controller;Animation<double>? _animation;@overridevoid initState() {super.initState();_controller = AnimationController(vsync: this,duration: Duration(seconds: 3),)..repeat();_animation = Tween<double>(begin: 0, end: 1).animate(_controller!);}@overridevoid dispose() {_controller?.dispose();super.dispose();}@overrideWidget build(BuildContext context) {return Scaffold(backgroundColor: Colors.white,body: Center(child: CustomPaint(painter: LoadingPainter(animation: _animation!),child: SizedBox(width: 200.0,height: 200.0,),),),);}
}

详解

  • initState 方法:
    • 初始化时创建 AnimationController 并设置动画持续时间为 3 秒。
    • 调用 …repeat() 方法让动画重复播放。
    • 使用 Tween 创建一个从 0 到 1 的动画,并与控制器关联。
  • dispose 方法:
    • 销毁控制器以释放资源。
  • build 方法:
    • 使用 CustomPaint 来绘制自定义内容,这里我们指定了 LoadingPainter 作为画笔,然后设置了一个 200x200 的 SizedBox 来容纳绘制内容。

3. 定义 LoadingPainter 类

LoadingPainter 类继承自 CustomPainter,负责实际绘制每个小圆。我们需要计算每个小圆的位置、大小和透明度,以便实现顺时针方向依次从小到大的动画效果。

class LoadingPainter extends CustomPainter {final Animation<double> animation;LoadingPainter({required this.animation}) : super(repaint: animation);@overridevoid paint(Canvas canvas, Size size) {double radius = size.width / 2;int circleCount = 12;double baseCircleRadius = 5.0;double maxCircleRadius = 15.0;Paint paint = Paint()..style = PaintingStyle.fill;for (int i = 0; i < circleCount; i++) {double angle = (i / circleCount) * 2 * pi;double x = radius + radius * cos(angle);double y = radius + radius * sin(angle);// 计算每个小圆在动画中的进度,并加入相位偏移double progress = (animation.value - i / circleCount) % 1;double scaleFactor = (sin(progress * 2 * pi) + 1) / 2;double currentRadius =baseCircleRadius + scaleFactor * (maxCircleRadius - baseCircleRadius);// 确保透明度不低于 0.3double opacity = 0.3 + 0.7 * scaleFactor;paint.color = Colors.blue.withOpacity(opacity);canvas.drawCircle(Offset(x, y), currentRadius, paint);}}@overridebool shouldRepaint(covariant CustomPainter oldDelegate) {return true;}
}

详解

  • 构造函数:
    • 接受一个 Animation 类型参数,并调用 super(repaint: animation) 以便在动画值改变时触发重绘。
  • paint 方法:
    • 初始化一些常量,包括大圆的半径、小圆的数量以及小圆的最小和最大半径。
    • 使用 Paint 进行绘制配置。
    • 遍历每个小圆,并根据其序号和当前动画进度计算位置和大小。
    • 通过 sin 函数计算缩放因子 scaleFactor,并使用该因子调整小圆的半径和透明度。
    • 最后,通过 canvas.drawCircle 在计算出的坐标处绘制每个小圆。

4. 总结

在这篇文章中,我们学习了如何使用 Flutter 创建一个加载动画。通过 AnimationController 和 CustomPainter,我们可以轻松地实现各种复杂的动画效果。这种加载动画不仅可以提升用户体验,还可以让您的应用看起来更加专业。

http://www.mmbaike.com/news/67922.html

相关文章:

  • 国内好的设计网站给我免费播放片高清在线观看
  • 服装网站建设中期目标佛山做优化的公司
  • 如何做英文网站外链免费学生网页制作成品代码
  • 相亲网站认识的可以做朋友百度怎么发布广告
  • 网站团队介绍2022最火营销方案
  • 个人建网站多少钱seo诊断优化方案
  • 崇州网站制作网上推广怎么弄?
  • 阿里巴巴的网站应该怎么做口碑营销怎么做
  • 租车网站制作方案seo联盟
  • 网站域名和密码优化推广方案
  • 用html做一号店网站怎么做河北网站建设推广
  • 深圳石岩做网站网站建设营销推广
  • 重庆网站建设哪里比较好呢互联网舆情监控系统
  • 成都网站设计费用网站优化搜索排名
  • 网站图片用什么格式互联网营销师培训机构哪家好
  • 做网站商机百度站长平台官网
  • 怎样做软件网站奉节县关键词seo排名优化
  • 工业设计网站哪家好推广app是什么工作
  • 那里有帮做网站的三只松鼠网络营销案例分析
  • 开网站做赌博赚钱吗百度链接提交入口
  • 网页游戏网站快手福建企业seo推广
  • 网站建设linux济南seo排名搜索
  • 平台网站做代理商在哪里推广比较好
  • 佛山网站建设品牌seo快速入门教程
  • 石首做网站的公司找培训机构的平台
  • 深圳做网站排名公司营业推广经典案例
  • 国外免费素材模板网站百家号权重查询站长工具
  • 做网站如何备案在线网站排名工具
  • 网站建设需要什么材料广州百度seo代理
  • 深圳网站建设推广百度关键词挖掘工具爱站网