flutter自定义进度动画

flutter自定义进度动画
github地址

效果

gif的帧率不正常


[图片上传中...(2018-06-04-17-42-35.gif-4659c2-1528105446191-0)]

2018-06-04-17-42-35.gif

实现

自定义 CustomPainter 画出圆

class _Progress extends CustomPainter {
  final Color color;
  final int count;
  final List<Animation<double>>animators;

  const _Progress({this.animators, this.color, this.count});

  @override
  void paint(Canvas canvas, Size size) {
    var radius = size.width / (3 * count + 1);
    var paint = new Paint()
      ..color = color
      ..style = PaintingStyle.fill;
    for (int i = 1; i < count + 1; i++) {
      double value = animators[i - 1].value;
      canvas.drawCircle(
          new Offset(radius * i * 3 - radius, size.height / 2),
          radius * (value > 1 ? (2 - value) : value), paint);
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return oldDelegate != this;
  }

}

用动画控制圆的半径

class _ProgressState extends State<MyProgress> with TickerProviderStateMixin {

  List<Animation<double>>animators = [];
  List<AnimationController>_animationControllers = [];

  @override
  void initState() {
    super.initState();
    for (int i = 0; i < widget.count; i++) {

      var animationController = new AnimationController(vsync: this,
          duration: Duration(milliseconds: widget.milliseconds * widget.count));
      animationController.value=0.8*i/widget.count;
      _animationControllers.add(animationController);
      Animation<double> animation = new Tween(begin: 0.1, end: 1.9).animate(
          animationController);
      animators.add(animation);
    }
    animators[0].addListener(_change);
    try {
      var mi = (widget.milliseconds~/(animators.length-1));
      for (int i = 0; i < animators.length; i++) {
        print(( mi*i).toString());
        dodelay(_animationControllers[i], mi*i);
      }
    } on Exception {

    }
  }

  void dodelay(AnimationController _animationControllers,
      int delay) async{
      Future.delayed(Duration(milliseconds: delay),(){
        _animationControllers..repeat().orCancel;
    });
  }

  void _change() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return new CustomPaint(painter: _Progress(
        animators: animators, color: widget.color, count: widget.count),
      size: widget.size,);
  }

  @override
  void dispose() {
    super.dispose();
    animators[0].removeListener(_change);
    for (AnimationController _animationController in _animationControllers) {
      _animationController.dispose();
    }
  }
}

使用

child: new MyProgress(size: new Size(100.0, 20.0))
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 【Android 动画】 动画分类补间动画(Tween动画)帧动画(Frame 动画)属性动画(Property ...
    Rtia阅读 11,368评论 1 38
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,914评论 25 709
  • 最近两年,我的思绪总是喜欢回到刚参加工作的那两年,不仅因为那时对学生的喜欢,还有当年对教学无限的热情,让我...
    绿色点点灰阅读 4,262评论 5 6
  • 初冬的暖日,夏日的凉风,两者在适宜的时节就是“人间四月天”,在不适宜的时节便是惹人厌的东西。“红玫瑰”与“白玫瑰”...
    d601ea2d8638阅读 2,571评论 0 1