注册

庆祝神舟十三号发射成功,来一个火箭发射动画

前言


北京时间10月16日0时23分,神舟十三号飞船成功发射,目前三名航天员已经顺利进驻空间站,开始为期6个月的“太空差旅”生活。
image.png
国家的航天技术的突飞猛进也让岛上码农很自豪,今天看 Flutter 的动画知识,看到了 AnimatedPositioned 这个组件,可以用于控制组件的相对位置移动。结合这个神舟十三号的发射,灵机一动,正好可以使用AnimatedPositioned 这个组件实现火箭发射动画。话不多说,先上效果!
火箭发射动画.gif


效果说明


这里其实是两张图片叠加,一张是背景地球星空的背景图,一张是火箭。火箭在发射过程中有两个变化:



  • 高度越来越高,其实就是相对图片背景图底部的位置越来越大就可以实现;
  • 尺寸越来越小,这个可以控制整个组件的尺寸实现。

然后是动画取消的选择,火箭的速度是越来越快,试了几个 Flutter 自带的曲线,发现 easeInCubic 这个效果挺不错的,开始慢,后面越来越快,和火箭发射的过程是类似的。


AnimatedPositioned介绍


AnimatedPositioned组件的使用方式其实和 AnimatedContainer 类似。只是AnimatedPositionedPositioned 组件的替代。构造方法定义如下:


const AnimatedPositioned({
Key? key,
required this.child,
this.left,
this.top,
this.right,
this.bottom,
this.width,
this.height,
Curve curve = Curves.linear,
required Duration duration,
VoidCallback? onEnd,
})

前面的参数和 Positioned 一样,后面是动画控制参数,这些参数的定义和 AnimatedContainer 的是一样的:



  • curve:动画效果曲线;
  • duration:动画时长;
  • onEnd:动画结束后回调。

我们可以改变 lefttopwidth等参数来实现动画过渡的效果。比如我们的火箭发射,就是修改 bottom (飞行高度控制)和 width (尺寸大小控制)来实现的。


火箭发射动画实现


有了上面的两个分析,火箭发射动画就简单了!完整代码如下:


class RocketLaunch extends StatefulWidget {
RocketLaunch({Key? key}) : super(key: key);

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

class _RocketLaunchState extends State<RocketLaunch> {
var rocketBottom = -80.0;
var rocketWidth = 160.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('火箭发射'),
brightness: Brightness.dark,
backgroundColor: Colors.black,
),
backgroundColor: Colors.black,
body: Center(
child: Stack(
alignment: Alignment.bottomCenter,
children: [
Image.asset(
'images/earth.jpeg',
height: double.infinity,
fit: BoxFit.fill,
),
AnimatedPositioned(
child: Image.asset(
'images/rocket.png',
fit: BoxFit.fitWidth,
),
bottom: rocketBottom,
width: rocketWidth,
duration: Duration(seconds: 5),
curve: Curves.easeInCubic,
),
],
),
),
floatingActionButton: FloatingActionButton(
child: Text(
'发射',
style: TextStyle(
color: Colors.white,
),
textAlign: TextAlign.center,
),
onPressed: () {
setState(() {
rocketBottom = MediaQuery.of(context).size.height;
rocketWidth = 40.0;
});
},
),
);
}
}

其中一开始设置 bottom 为负值,是为了隐藏火箭的焰火,这样会更有感觉一些。然后就是在点击发射按钮的时候,通过 setState 更改底部距离和火箭尺寸就可以搞定了。


总结


通过神舟十三飞船发射,来一个火箭动画是不是挺有趣?其实这篇主要的知识点还是介绍 AnimatedPositioned 的应用。通过 AnimatedPositioned可以实现很多层叠组件的相对移动变化效果,比如进度条的滑块,滑动开关等。各位 Flutter 玩家也可以利用 AnimatedPositioned 这个组件自己来玩一下好玩的动画效果哦!


作者:岛上码农
链接:https://juejin.cn/post/7019593544788606990
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

0 个评论

要回复文章请先登录注册