Flutter 用自定義轉(zhuǎn)場動畫實(shí)現(xiàn)頁面切換
fluro 轉(zhuǎn)場動畫源碼 在使用自定義轉(zhuǎn)場動畫前,先扒一扒 fluro 的源碼,通過源碼可以發(fā)現(xiàn)這么一個標(biāo)準(zhǔn)的轉(zhuǎn)場方法:
RouteTransitionsBuilder _standardTransitionsBuilder(
TransitionType? transitionType) {
return (BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation, Widget child) {
if (transitionType == TransitionType.fadeIn) {
return FadeTransition(opacity: animation, child: child);
} else {
const Offset topLeft = const Offset(0.0, 0.0);
const Offset topRight = const Offset(1.0, 0.0);
const Offset bottomLeft = const Offset(0.0, 1.0);
Offset startOffset = bottomLeft;
Offset endOffset = topLeft;
if (transitionType == TransitionType.inFromLeft) {
startOffset = const Offset(-1.0, 0.0);
endOffset = topLeft;
} else if (transitionType == TransitionType.inFromRight) {
startOffset = topRight;
endOffset = topLeft;
} else if (transitionType == TransitionType.inFromBottom) {
startOffset = bottomLeft;
endOffset = topLeft;
} else if (transitionType == TransitionType.inFromTop) {
startOffset = Offset(0.0, -1.0);
endOffset = topLeft;
}
return SlideTransition(
position: Tween<Offset>(
begin: startOffset,
end: endOffset,
).animate(animation),
child: child,
);
}
};
}
從源碼可以看出,根據(jù)不同枚舉返回了不同的動畫(即 transitionBuilder),其中TransitionType.fadeIn使用的是Flutter 自帶的 FadeTransition,通過改變透明度來完成動畫。而其他的左滑入、右滑入、下滑入和上滑入都是從初始偏移位置滑動到結(jié)束位置,使用的是 SlideTransition。Flutter 除了上述的 FadeTransition、SlideTransition 之外,還有如下的常用轉(zhuǎn)場形式:
- RotationTransition:旋轉(zhuǎn)轉(zhuǎn)場
- ScaleTransition:縮放轉(zhuǎn)場
既然是這樣,我們可以依葫蘆畫瓢,先用系統(tǒng)其他的轉(zhuǎn)場效果做一個自定義的轉(zhuǎn)場看看。
旋轉(zhuǎn)轉(zhuǎn)場動畫
先來看看旋轉(zhuǎn)的轉(zhuǎn)場RotationTransition,RotationTransition 的構(gòu)造方法定義如下:
const RotationTransition({
Key? key,
required Animation<double> turns,
this.alignment = Alignment.center,
this.child,
}) : assert(turns != null),
super(key: key, listenable: turns);
其中 turns 是動畫控制,表示旋轉(zhuǎn)的弧度數(shù),等于動畫控制值乘以2π。alignment 表示旋轉(zhuǎn)圍繞的中心位置,默認(rèn)是居中的。旋轉(zhuǎn)的弧度不要太大,否則動畫過快,導(dǎo)致不太好看,經(jīng)過驗(yàn)證,推薦的起始值0.2至0.3之間,結(jié)束值為0表示回到正常位置。起始值如果為負(fù),則是順時針;如果為正則是逆時針,示例代碼如下:
//逆時針圍繞中心旋轉(zhuǎn)
RouterManager.router.navigateTo(
context,
RouterManager.transitionPath,
transition: TransitionType.custom,
transitionBuilder:
(context, animation, secondaryAnimation, child) {
return RotationTransition(
turns: Tween<double>(
begin: 0.25,
end: 0.0,
).animate(animation),
child: child,
);
},
);
//...
//順時針圍繞左下角旋轉(zhuǎn)
RouterManager.router.navigateTo(
context,
RouterManager.transitionPath,
transition: TransitionType.custom,
transitionBuilder:
(context, animation, secondaryAnimation, child) {
return RotationTransition(
alignment: Alignment.bottomLeft,
turns: Tween<double>(
begin: -0.25,
end: 0.0,
).animate(animation),
child: child,
);
},
);
其中 Tween 是系統(tǒng)自帶的線性插值方法。
縮放轉(zhuǎn)場動畫
縮放轉(zhuǎn)場這類在圖片預(yù)覽會比較常見,一般是從較小的比例縮放到1:1比例。使用方式和旋轉(zhuǎn)轉(zhuǎn)場類似,示例代碼如下:
RouterManager.router.navigateTo(
context,
RouterManager.transitionPath,
transition: TransitionType.custom,
transitionBuilder:
(context, animation, secondaryAnimation, child) {
return ScaleTransition(
scale: Tween<double>(
begin: 0.5,
end: 1.0,
).animate(animation),
child: child,
);
},
);
自定義轉(zhuǎn)場動畫
通過閱讀源碼,其實(shí)可以發(fā)現(xiàn)RotationTransition、ScaleTransition 都是繼承自 AnimatedWidget,因此我們可以自己寫一個自定義的 Transition 繼承自 AnimatedWidget,在 build方法中返回一個 Transform 對象即可。通過這種方式可以做自定義的轉(zhuǎn)場動畫效果。我們以變形為例,可以利用 Matrix4的 skew 方法,在 x 和 y 軸進(jìn)行變形,就可以得到轉(zhuǎn)場類似卡片變形的效果。也可以只在 X 軸或 Y 軸變形(skewX和 skewY 方法)。這里以 x,y 軸同時變形定義了一個轉(zhuǎn)場動畫:
class SkewTransition extends AnimatedWidget {
const SkewTransition({
Key key,
Animation<double> turns,
this.alignment = Alignment.center,
this.child,
}) : assert(turns != null),
super(key: key, listenable: turns);
Animation<double> get turns => listenable as Animation<double>;
final Alignment alignment;
final Widget child;
@override
Widget build(BuildContext context) {
final double turnsValue = turns.value;
final Matrix4 transform =
Matrix4.skew(turnsValue * pi * 2.0, turnsValue * pi * 2.0);
return Transform(
transform: transform,
alignment: alignment,
child: child,
);
}
}
使用方式和 RotationTransition 類似:
RouterManager.router.navigateTo(
context,
RouterManager.transitionPath,
transition: TransitionType.custom,
transitionBuilder:
(context, animation, secondaryAnimation, child) {
return SkewTransition(
turns: Tween<double>(
begin: -0.05,
end: 0.0,
).animate(animation),
child: child,
);
},
);
實(shí)際也可以嘗試使用圍繞 X 軸旋轉(zhuǎn),圍繞 Y 軸旋轉(zhuǎn),以及變更中心點(diǎn)位置(alignment)來實(shí)現(xiàn)不同的動畫轉(zhuǎn)場效果。如果需要更為復(fù)雜的動畫效果,則可以研究動畫的實(shí)現(xiàn),后續(xù)篇章將有對應(yīng)動畫的介紹。
運(yùn)行效果
運(yùn)行效果如下圖所示:

以上就是Flutter 用自定義轉(zhuǎn)場動畫實(shí)現(xiàn)頁面切換的詳細(xì)內(nèi)容,更多關(guān)于Flutter 自定義轉(zhuǎn)場動畫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用
Retrofit和OkHttp API以及JVM擴(kuò)展RxJava都是開源項目,大家可以輕松在GitHub上找到,下載和基本配置部分這里我們不作重點(diǎn),主要還是來看一下Android中的Retrofit+OkHttp+RxJava緩存架構(gòu)使用:2016-06-06
Android自定義View仿QQ等級天數(shù)進(jìn)度
這篇文章主要為大家詳細(xì)介紹了Android自定義View仿QQ等級天數(shù)進(jìn)度效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup帶箭頭的圓角矩形菜單實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-07-07
Android自定義View實(shí)現(xiàn)簡單文字描邊功能
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)簡單文字描邊功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android使用內(nèi)置WebView打開TextView超鏈接的實(shí)現(xiàn)方法
這篇文章主要介紹了Android使用內(nèi)置WebView打開TextView超鏈接的實(shí)現(xiàn)方法,文中給出了詳細(xì)的示例代碼,對各位Android開發(fā)者們具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-03-03
Android EventBus 3.0.0 使用總結(jié)(必看篇)
下面小編就為大家?guī)硪黄狝ndroid EventBus 3.0.0 使用總結(jié)(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05
使用RecylerView完成拖動排序高仿qq側(cè)滑刪除功能
最近在做一個android項目,使用到Recylerview完成拖動排序,側(cè)滑刪除功能,今天小編把思路分享到腳本之家平臺,供大家學(xué)習(xí)2016-10-10

