Android Flutter制作交錯(cuò)動(dòng)畫的示例代碼
前言
之前一篇我們講了 Flutter組合動(dòng)畫實(shí)現(xiàn)的方式 —— 交錯(cuò)動(dòng)畫。借助 GIF 和繪圖技巧是可以做到類似 GIF 那種效果的。本篇我們來一個(gè)應(yīng)用實(shí)例,我們讓輪子在草地滾動(dòng)著前進(jìn),而且還能粘上“綠色的草”,運(yùn)行效果如下動(dòng)畫所示。
動(dòng)畫解析
上面實(shí)現(xiàn)的效果實(shí)際上由三個(gè)動(dòng)畫組成:
- 輪子前進(jìn)的動(dòng)畫
- 輪子滾動(dòng)
- 輪子的邊緣顏色漸變(由黑色變成綠色)
這三個(gè)動(dòng)畫是同時(shí)進(jìn)行的,因此需要使用到交錯(cuò)動(dòng)畫,即使用一個(gè) AnimationController
來控制三個(gè) Tween
對(duì)象實(shí)現(xiàn)上述的動(dòng)畫組合。
編碼實(shí)現(xiàn)
首先是輪子組件的定義,為了讓輪子轉(zhuǎn)動(dòng)的效果能夠看到,我們給輪子填充了線性的漸變色,然后輪子的尺寸、旋轉(zhuǎn)速度(time
)和邊框顏色由上級(jí)組件來控制。整個(gè)實(shí)現(xiàn)很簡(jiǎn)單,就是一個(gè)加了裝飾的 Container
而已。
class?Wheel?extends?StatelessWidget?{ ??final?double?size; ??final?Color?color; ??final?double?time; ??const?Wheel({ ????Key??key, ????required?this.size, ????required?this.time, ????required?this.color, ??})?:?super(key:?key); ??@override ??Widget?build(BuildContext?context)?{ ????return?Container( ??????width:?size, ??????height:?size, ??????transform:?Matrix4.identity()..rotateZ(2?*?pi?*?time), ??????transformAlignment:?Alignment.center, ??????decoration:?BoxDecoration( ????????border:?Border.all(color:?color,?width:?10.0), ????????borderRadius:?BorderRadius.circular(size?/?2), ????????gradient:?LinearGradient( ??????????colors:?[ ????????????Colors.white, ????????????Colors.orange[100]!, ????????????Colors.orange[400]!, ??????????], ????????), ??????), ????); ??} }
然后是整個(gè)頁面布局,整個(gè)頁面布局其實(shí)就是一個(gè) Stack
,然后底部是綠色的 Container
再加兩個(gè)輪子,都是使用 Positioned
來確定各自的位置。然后就是通過受控的Tween 對(duì)象控制輪子的旋轉(zhuǎn)速度,輪子外邊沿顏色和移動(dòng)的距離,代碼如下,其中輪子移動(dòng)距離通過控制邊距實(shí)現(xiàn)。
Widget?build(BuildContext?context)?{ ??final?bottomHeight?=?MediaQuery.of(context).size.height?/?3; ??return?Scaffold( ????appBar:?AppBar( ??????title:?const?Text('交錯(cuò)動(dòng)畫'), ????), ????body:?Stack(children:?[ ??????Positioned( ????????child:?Container( ??????????width:?double.infinity, ??????????height:?bottomHeight, ??????????color:?Colors.green[400], ????????), ????????bottom:?0, ????????left:?0, ????????right:?0, ??????), ??????Positioned( ??????????child:?Wheel( ????????????size:?wheelSize, ????????????color:?_color.value!, ????????????time:?_time.value, ??????????), ??????????left:?_offset.value?*?MediaQuery.of(context).size.width, ??????????bottom:?bottomHeight), ??????Positioned( ??????????child:?Wheel( ????????????size:?wheelSize, ????????????color:?_color.value!, ????????????time:?-_time.value, ??????????), ??????????right:?_offset.value?*?MediaQuery.of(context).size.width, ??????????bottom:?bottomHeight) ????]), ????floatingActionButton:?FloatingActionButton( ??????child:?Icon(Icons.play_arrow), ??????onPressed:?()?{ ????????if?(_controller.isCompleted)?{ ??????????_controller.reverse(); ????????}?else?if?(!_controller.isAnimating)?{ ??????????_controller.forward(); ????????} ??????}, ????), ??); }
最后就是構(gòu)建受AnimationController 控制的 Tween 對(duì)象了,這個(gè)在Flutter 做出 GIF 動(dòng)畫效果已經(jīng)介紹過了,代碼如下:
late?AnimationController?_controller; late?Animation<double>?_time; late?Animation<double>?_offset; late?Animation<Color?>?_color; final?wheelSize?=?80.0; @override void?initState()?{ ??_controller?= ??????AnimationController(duration:?Duration(seconds:?4),?vsync:?this) ????????..addListener(()?{ ??????????setState(()?{}); ????????}); ??_time?=?Tween<double>(begin:?0,?end:?8.0).animate( ????CurvedAnimation( ??????parent:?_controller, ??????curve:?Interval( ????????0.0, ????????1.0, ????????curve:?Curves.linear, ??????), ????), ??); ??_offset?=?Tween<double>(begin:?0,?end:?1.0).animate( ????CurvedAnimation( ??????parent:?_controller, ??????curve:?Interval( ????????0.0, ????????1.0, ????????curve:?Curves.easeInCubic, ??????), ????), ??); ??_color?=?ColorTween(begin:?Colors.black87,?end:?Colors.green).animate( ????CurvedAnimation( ??????parent:?_controller, ??????curve:?Interval( ????????0.0, ????????0.8, ????????curve:?Curves.easeIn, ??????), ????), ??); ??super.initState(); }
就這樣,一對(duì)奔向?qū)Ψ降妮喿觿?dòng)畫效果就完成了!
總結(jié)
交錯(cuò)動(dòng)畫實(shí)際上可以實(shí)現(xiàn)非常有創(chuàng)意的動(dòng)效,只是這樣會(huì)需要很高的繪圖技巧,比如使用 CustomPaint
來做。接下來的幾篇我們來介紹一下 CustomPaint
相關(guān)的內(nèi)容。
以上就是Android Flutter制作交錯(cuò)動(dòng)畫的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Android Flutter交錯(cuò)動(dòng)畫的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Android?Flutter實(shí)現(xiàn)搜索的三種方式詳解
- Android Flutter圖片處理之高斯模糊的實(shí)現(xiàn)
- Android使用Flutter實(shí)現(xiàn)錄音插件
- Android?Flutter繪制有趣的?loading加載動(dòng)畫
- Android Flutter利用CustomPaint繪制基本圖形詳解
- Android Flutter表格組件Table的使用詳解
- Android Flutter實(shí)現(xiàn)GIF動(dòng)畫效果的方法詳解
- Android利用Flutter實(shí)現(xiàn)立體旋轉(zhuǎn)效果
- Android Flutter實(shí)現(xiàn)原理淺析
相關(guān)文章
Android ListView實(shí)現(xiàn)簡(jiǎn)單列表功能
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)簡(jiǎn)單列表功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android Studio 引用外部依賴時(shí)報(bào)錯(cuò)的解決方法
這篇文章主要介紹了Android Studio報(bào)錯(cuò)Unable to resolve dependency for':app@release/compileClasspath':無法引用任何外部依賴的解決辦法,需要的朋友可以參考下2018-01-01A07_TimePicker & DatePicker & AnalogClock & Digi
本文將帶領(lǐng)大家一起學(xué)習(xí)時(shí)間日期和時(shí)鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈2013-06-06Android實(shí)現(xiàn)調(diào)用攝像頭和相冊(cè)的方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)調(diào)用攝像頭和相冊(cè)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04Android中ListView結(jié)合CheckBox實(shí)現(xiàn)數(shù)據(jù)批量選擇(全選、反選、全不選)
這篇文章主要介紹了Android編程中ListView結(jié)合CheckBox實(shí)現(xiàn)批量選擇,包括實(shí)現(xiàn)全選、反選、全不選等操作,感興趣的小伙伴們可以參考一下2016-01-01Android實(shí)現(xiàn)數(shù)據(jù)按照時(shí)間排序
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)數(shù)據(jù)按照時(shí)間排序的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09Android 自定View實(shí)現(xiàn)仿QQ運(yùn)動(dòng)步數(shù)圓弧及動(dòng)畫效果
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)高仿QQ運(yùn)動(dòng)步數(shù)圓弧及動(dòng)畫效果的實(shí)例代碼,本文涉及到繪制圓弧需要具備的知識(shí)點(diǎn),需要的朋友可以參考下2016-10-10Android編程之線性布局LinearLayout實(shí)例簡(jiǎn)析
這篇文章主要介紹了Android編程之線性布局LinearLayout用法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Android線性布局的使用技巧,需要的朋友可以參考下2016-01-01Android學(xué)習(xí)小結(jié)之獲取被啟動(dòng)的Activity傳回的數(shù)據(jù)
這篇文章主要介紹了獲取被啟動(dòng)的Activity傳回的數(shù)據(jù),非常不錯(cuò),介紹的非常詳細(xì),需要的朋友可以參考下2016-08-08