基于Flutter制作一個火箭發(fā)射動畫
前言
北京時間10月16日0時23分,神舟十三號飛船成功發(fā)射,目前三名航天員已經順利進駐空間站,開始為期6個月的“太空差旅”生活。
國家的航天技術的突飛猛進也讓島上碼農很自豪,今天看 Flutter 的動畫知識,看到了 AnimatedPositioned
這個組件,可以用于控制組件的相對位置移動。結合這個神舟十三號的發(fā)射,靈機一動,正好可以使用AnimatedPositioned
這個組件實現(xiàn)火箭發(fā)射動畫。話不多說,先上效果!
效果說明
這里其實是兩張圖片疊加,一張是背景地球星空的背景圖,一張是火箭?;鸺诎l(fā)射過程中有兩個變化:
- 高度越來越高,其實就是相對圖片背景圖底部的位置越來越大就可以實現(xiàn);
- 尺寸越來越小,這個可以控制整個組件的尺寸實現(xiàn)。
然后是動畫取消的選擇,火箭的速度是越來越快,試了幾個 Flutter 自帶的曲線,發(fā)現(xiàn) easeInCubic
這個效果挺不錯的,開始慢,后面越來越快,和火箭發(fā)射的過程是類似的。
AnimatedPositioned介紹
AnimatedPositioned
組件的使用方式其實和 AnimatedContainer 類似。只是AnimatedPositioned
是 Positioned
組件的替代。構造方法定義如下:
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, })
前面的參數(shù)和 Positioned
一樣,后面是動畫控制參數(shù),這些參數(shù)的定義和 AnimatedContainer
的是一樣的:
curve
:動畫效果曲線;duration
:動畫時長;onEnd
:動畫結束后回調。
我們可以改變 left
,top
,width
等參數(shù)來實現(xiàn)動畫過渡的效果。比如我們的火箭發(fā)射,就是修改 bottom
(飛行高度控制)和 width
(尺寸大小控制)來實現(xiàn)的。
火箭發(fā)射動畫實現(xiàn)
有了上面的兩個分析,火箭發(fā)射動畫就簡單了!完整代碼如下:
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('火箭發(fā)射'), 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( '發(fā)射', style: TextStyle( color: Colors.white, ), textAlign: TextAlign.center, ), onPressed: () { setState(() { rocketBottom = MediaQuery.of(context).size.height; rocketWidth = 40.0; }); }, ), ); } }
其中一開始設置 bottom
為負值,是為了隱藏火箭的焰火,這樣會更有感覺一些。然后就是在點擊發(fā)射按鈕的時候,通過 setState
更改底部距離和火箭尺寸就可以搞定了。
總結
通過神舟十三飛船發(fā)射,來一個火箭動畫是不是挺有趣?其實這篇主要的知識點還是介紹 AnimatedPositioned
的應用。通過 AnimatedPositioned
可以實現(xiàn)很多層疊組件的相對移動變化效果,比如進度條的滑塊,滑動開關等。各位 Flutter 玩家也可以利用 AnimatedPositioned
這個組件自己來玩一下好玩的動畫效果哦!
到此這篇關于基于Flutter制作一個火箭發(fā)射動畫的文章就介紹到這了,更多相關Flutter火箭發(fā)射動畫內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
僅5步搞定Android開發(fā)環(huán)境部署 Android開發(fā)環(huán)境搭建教程
僅5步搞定Android開發(fā)環(huán)境部署,這篇文章主要為大家詳細介紹了Android開發(fā)環(huán)境搭建教程,感興趣的小伙伴們可以參考一下2016-02-02Android編程實現(xiàn)對話框Dialog背景透明功能示例
這篇文章主要介紹了Android編程實現(xiàn)對話框Dialog背景透明功能,涉及Android對話框的布局、屬性及事件處理相關操作技巧,需要的朋友可以參考下2017-07-07Android持久化技術之SharedPreferences存儲實例詳解
這篇文章主要介紹了Android持久化技術之SharedPreferences存儲,結合實例形式較為詳細的分析了SharedPreferences存儲的原理、應用及具體實現(xiàn)方法,需要的朋友可以參考下2016-01-01activitygroup 切換動畫效果如何實現(xiàn)
本文將詳細介紹activitygroup 切換動畫效果實現(xiàn)過程,需要聊解的朋友可以參考下2012-12-12