这篇文章主要介绍了基于Flutter怎么制作一个心碎动画特效的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇基于Flutter怎么制作一个心碎动画特效文章都会有所收获,下面我们一起来看看吧。
效果图先上:
首先我们使用两段三阶贝塞尔曲线制作一个心型,这里因为需要实现心碎的效果,所以我们需要将心的两段用两段路径path
进行绘制出来,效果:
绘制代码:
canvas.translate(size.width / 2, size.height / 2);
Paint paint = Paint();
paint
..style = PaintingStyle.stroke
..strokeWidth = 2
..color = Colors.black87;
Path path = Path();
path.moveTo(0, 0);
path.cubicTo(-200, -80, -60, -240, 0, -140);
path.close();
Path path3 = Path();
canvas.save();
canvas.drawPath(
path,
paint
..color = Colors.red
..style = PaintingStyle.stroke);
canvas.restore();
path3.cubicTo(200, -80, 60, -240, 0, -140);
path3.close();
canvas.drawPath(
path3,
paint..color = Colors.black87);
我们看到心确实分成两半了,但是中间还缺少裂痕,接下来我们就绘制心碎的裂痕,也很简单,在两段路径path
闭合前进行绘制线,效果:
绘制代码:
path.relativeLineTo(-10, 30);
path.relativeLineTo(20, 5);
path.relativeLineTo(-20, 30);
path.relativeLineTo(20, 20);
path.relativeLineTo(-10, 20);
path.relativeLineTo(10, 10);
path3.relativeLineTo(-10, 30);
path3.relativeLineTo(20, 5);
path3.relativeLineTo(-20, 30);
path3.relativeLineTo(20, 20);
path3.relativeLineTo(-10, 20);
path3.relativeLineTo(10, 10);
OK,我们已经看到心已经有了裂痕,如何心碎,只需将画布进行翻转一定角度即可,这里我们将画布翻转45°,看下效果:
左边:
右边:
已经有心碎的感觉了,接下来加入动画元素让心碎的过程动起来。
思路: 我们可以想一下,心碎的过程是什么样子,心的颜色慢慢变灰,心然后慢慢裂开,下方的动画运动曲线看起来更符合心碎的过程,里面有不舍,不甘,但最后心还是慢慢的碎了。
我们把画笔进行填充将这个动画加入进来看下最终效果。
是不是心碎了一地。
知识点: 这里我们需要找到红色和灰色的RGB色值,通过Color.fromRGBO(r, g, b, opacity)
方法赋值颜色的色值。然后通过动画值改变RGB的值即可。 这里我使用的色值是:
红色:Color.fromRGBO(255, 0, 0, 1)
灰色:Color.fromRGBO(169, 169, 169, 1)
class XinSui extends StatefulWidget {
const XinSui({Key? key}) : super(key: key);
@override
_XinSuiState createState() => _XinSuiState();
}
class _XinSuiState extends State<XinSui> with SingleTickerProviderStateMixin {
late AnimationController _controller =
AnimationController(vsync: this, duration: Duration(milliseconds: 4000))
..repeat();
late CurvedAnimation cure =
CurvedAnimation(parent: _controller, curve: Curves.bounceInOut);
late Animation<double> animation =
Tween<double>(begin: 0.0, end: 1.0).animate(cure);
@override
Widget build(BuildContext context) {
return Container(
child: CustomPaint(
size: Size(double.infinity, double.infinity),
painter: _XinSuiPainter(animation),
),
);
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
}
class _XinSuiPainter extends CustomPainter {
Animation<double> animation;
_XinSuiPainter(this.animation) : super(repaint: animation);
@override
void paint(Canvas canvas, Size size) {
canvas.translate(size.width / 2, size.height / 2);
Paint paint = Paint();
paint
..style = PaintingStyle.stroke
..strokeWidth = 2
..color = Colors.black87;
Path path = Path();
path.moveTo(0, 0);
path.cubicTo(-200, -80, -60, -240, 0, -140);
path.relativeLineTo(-10, 30);
path.relativeLineTo(20, 5);
path.relativeLineTo(-20, 30);
path.relativeLineTo(20, 20);
path.relativeLineTo(-10, 20);
path.relativeLineTo(10, 10);
path.close();
Path path3 = Path();
canvas.save();
canvas.rotate(-pi / 4 * animation.value);
canvas.drawPath(
path,
paint
..color = Colors.red
..color = Color.fromRGBO(
255 - (86 * animation.value).toInt(),
(animation.value * 169).toInt(),
(animation.value * 169).toInt(),
1)
..style = PaintingStyle.fill);
canvas.restore();
path3.cubicTo(200, -80, 60, -240, 0, -140);
path3.relativeLineTo(-10, 30);
path3.relativeLineTo(20, 5);
path3.relativeLineTo(-20, 30);
path3.relativeLineTo(20, 20);
path3.relativeLineTo(-10, 20);
path3.relativeLineTo(10, 10);
path3.close();
canvas.rotate(pi / 4 * animation.value);
canvas.drawPath(
path3,paint);
}
@override
bool shouldRepaint(covariant _XinSuiPainter oldDelegate) {
return oldDelegate.animation != animation;
}
}
关于“基于Flutter怎么制作一个心碎动画特效”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“基于Flutter怎么制作一个心碎动画特效”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。