美文网首页flutter
Flutter 绘制一颗跳动的心

Flutter 绘制一颗跳动的心

作者: 卢融霜 | 来源:发表于2021-11-19 10:24 被阅读0次

效果图

效果图.gif

网页版(第一次打开网址加载会很慢)
网页版

分析

总体可以拆分为2个部分:

  1. 循环放大缩小的 中心。
  2. 5个外层边框,依次变大,同时颜色变浅, 直到变大变浅的最大值,然后重新从最小开始。

1.绘制一个循环放大缩小的中心

这里需要使用: AnimationController

 //1.声明
  AnimationController _controller;
 //2.初始化
    _controller = AnimationController(
        vsync: this,
        //循环周期
        duration: const Duration(seconds: 1),
        //起始值
        lowerBound: 60.r,
        //结束值
        upperBound: 100.r)
        //重复循环
      ..repeat(reverse: true)
      //添加监听
      ..addListener(() {
        //更新视图
        setState(() {});
      });
//3.传递给 图片,让图片的宽高改变
 Center(
                child: Image.asset(
                  "assets/images/ic_love.png",
                  width: _controller.value,
                  height: _controller.value,
                  fit: BoxFit.cover,
                ),
              )
//4.最后销毁
@override
  void dispose() {
     _timer.cancel();
    _controller.dispose();
    super.dispose();
  }

2.绘制5层边框,循环放大,变浅。

这里需要使用到:CustomPaint,CustomPainter,Paint,Timer

//1.声明
//边框层宽度
List<double> borderSize;
//边框透明度
List<double> opa;
//计时器 
Timer _timer;
//2.初始化
 borderSize = [20, 40, 60, 80, 100];
 opa = [1, 0.8, 0.6, 0.4, 0.2];
  _timer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
      //更新边框宽度,透明色
      ref();
    });
//3.更新透明色逻辑
  void ref() {
    for (int i = 0; i < borderSize.length; i++) {
      if (borderSize[i] < 120) {
        borderSize[i] += 1;
        opa[i] = double.parse((opa[i] - 0.01).toStringAsFixed(2));
      } else {
        opa[i] = 1.0;
        borderSize[i] = 20;
      }
    }
  }

让宽度数组,透明色数组,依次增加、减少循环数值


image.png
image.png

2.1.绘制心形边框代码

class PaintBorder extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    for (int i = 0; i < borderSize.length; i++) {
      var bodyColor = Color.fromRGBO(
        255,
        131,
        115,
        //透明色
        double.parse(opa[i].toDouble().toStringAsFixed(2)),
      );
      final Paint body = Paint();
      body
        ..color = bodyColor
        ..style = PaintingStyle.fill
        ..strokeWidth = 0;

      final Paint border = Paint();

      border
        ..color = bodyColor
        ..style = PaintingStyle.stroke
        ..strokeCap = StrokeCap.round
        //宽度
        ..strokeWidth = borderSize[i] * 2;

      //绘制心形状
      final double width = size.width;
      final double height = size.height;
      final Path path = Path();
      path.moveTo(0.5 * width, height * 0.5);
      path.cubicTo(0.2 * width, -0.2 * height, -0.2 * width, height * 0.4,
          0.5 * width, height * 0.8);
      path.moveTo(0.5 * width, height * 0.5);
      path.cubicTo(0.8 * width, -0.2 * height, 1.2 * width, height * 0.4,
          0.5 * width, height * 0.8);
      canvas.drawPath(path, body);
      canvas.drawPath(path, border);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

3.结合使用

 return BaseRoutesWidget(
        title: "跳动的心",
        child: Container(
          alignment: Alignment.center,
          child: Stack(
            children: [
              Center(
                child: CustomPaint(
                  size: Size(150.r, 150.r),
                  foregroundPainter: PaintBorder(),
                ),
              ),
              Center(
                child: Image.asset(
                  "assets/images/ic_love.png",
                  width: _controller.value,
                  height: _controller.value,
                  fit: BoxFit.cover,
                ),
              ),
            ],
          ),
        ));

4.整体代码

import 'dart:async';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter1/base/base_routes_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// @description 作用:绘制一颗跳动的心
// @date: 2021/11/18
// @author: 卢融霜

class HeadTwinkle extends StatefulWidget {
  const HeadTwinkle({Key key}) : super(key: key);

  @override
  _HeadTwinkleState createState() => _HeadTwinkleState();
}

//边框层宽度
List<double> borderSize;
//边框透明度
List<double> opa;
List<MaterialAccentColor> opaColor;

class _HeadTwinkleState extends State<HeadTwinkle>
    with SingleTickerProviderStateMixin {
  //声明
  AnimationController _controller;
  //计时器
  Timer _timer;

  @override
  void initState() {
    borderSize = [20, 40, 60, 80, 100];
    opa = [1, 0.8, 0.6, 0.4, 0.2];
    //初始化
    _controller = AnimationController(
        vsync: this,
        //循环周期
        duration: const Duration(seconds: 1),
        //起始值
        lowerBound: 60.r,
        //结束值
        upperBound: 100.r)
        //重复循环
      ..repeat(reverse: true)
      //添加监听
      ..addListener(() {
        //更新视图
        setState(() {});
      });

    _timer = Timer.periodic(const Duration(milliseconds: 100), (timer) {
      //更新边框宽度,透明色
      ref();
    });

    super.initState();
  }

  @override
  void dispose() {
    _timer.cancel();
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BaseRoutesWidget(
        title: "跳动的心",
        child: Container(
          alignment: Alignment.center,
          child: Stack(
            children: [
              Center(
                child: CustomPaint(
                  size: Size(150.r, 150.r),
                  foregroundPainter: PaintBorder(),
                ),
              ),
              Center(
                child: Image.asset(
                  "assets/images/ic_love.png",
                  width: _controller.value,
                  height: _controller.value,
                  fit: BoxFit.cover,
                ),
              ),
            ],
          ),
        ));
  }

  void ref() {
    for (int i = 0; i < borderSize.length; i++) {
      if (borderSize[i] < 120) {
        borderSize[i] += 1;
        opa[i] = double.parse((opa[i] - 0.01).toStringAsFixed(2));
      } else {
        opa[i] = 1.0;
        borderSize[i] = 20;
      }
    }
    print("borderSize:----------$borderSize");
    print("borderSize:----------$borderSize");
    print("opa:----------$opa");
    print("");
    print("");
    print("");

  }
}

class PaintBorder extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {
    for (int i = 0; i < borderSize.length; i++) {
      var bodyColor = Color.fromRGBO(
        255,
        131,
        115,
        //透明色
        double.parse(opa[i].toDouble().toStringAsFixed(2)),
      );
      final Paint body = Paint();
      body
        ..color = bodyColor
        ..style = PaintingStyle.fill
        ..strokeWidth = 0;

      final Paint border = Paint();

      border
        ..color = bodyColor
        ..style = PaintingStyle.stroke
        ..strokeCap = StrokeCap.round
        //宽度
        ..strokeWidth = borderSize[i] * 2;

      //绘制心形状
      final double width = size.width;
      final double height = size.height;
      final Path path = Path();
      path.moveTo(0.5 * width, height * 0.5);
      path.cubicTo(0.2 * width, -0.2 * height, -0.2 * width, height * 0.4,
          0.5 * width, height * 0.8);
      path.moveTo(0.5 * width, height * 0.5);
      path.cubicTo(0.8 * width, -0.2 * height, 1.2 * width, height * 0.4,
          0.5 * width, height * 0.8);
      canvas.drawPath(path, body);
      canvas.drawPath(path, border);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}

相关文章

  • Flutter 绘制一颗跳动的心

    效果图 网页版(第一次打开网址加载会很慢)网页版[https://static-9c90a346-a637-412...

  • 一颗心 是红的 还是红的 一颗心 是热的 还是热的 一颗心 是跳动的 还是跳动的

  • 一颗跳动的心

    一颗跳动的心,那么鲜活 愿时光不再无故蹉跎 太阳在单曲循环中沉默 带着耳机的依旧是你和我 闭目、聆听、思索 一颗跳...

  • 一颗跳动的心

    日日夜夜所盼的寒假,终于在火车开动的那一刹那开始了,坐着绿皮火车回家过年,身上背着母亲装的年货,脑海中浮现...

  • 想有一颗跳动的心

    去年,网上流行一句话:城市套路深,我要回农村。当时看了不由噗呲一笑,还想这又是哪个“漂”被虐了,发出如此感慨,可是...

  • 保持一颗跳动的心

    清单主题营即将结束,用一句话来形容我的旅程:满怀希望来, 满载收获归。这一程旅途,风景独美! 报名前的好奇,开营时...

  • 只有一颗心的跳动

    保持安静,又不敢靠近 许多个日日夜夜,你的回复就是我的好心情 我的参与却又杳无音信 任外头风和日丽 到后来,还只是...

  • 画一颗跳动的心

    引入图标库Font Awesome 使用该图标库里的heart图标 这是一个静态的图标,而且是灰色的,所以我们给这...

  • 一颗不会跳动的心

    看到这个标题,很多人想到的一定不一样吧! 寂静的眼里,不会跳动,那是更好的。 在安静的地方, 有你陪伴着我, 感谢...

  • flutter绘制系列

    1.为什么要写绘制 希望大家能够对flutter的绘制有一个系统的认识。 flutter绘制也能像h5的canva...

网友评论

    本文标题:Flutter 绘制一颗跳动的心

    本文链接:https://www.haomeiwen.com/subject/rgystrtx.html