flutter PositionedTransition實(shí)現(xiàn)縮放動(dòng)畫
本文實(shí)例為大家分享了flutter實(shí)現(xiàn)縮放動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
flutter 動(dòng)畫狀態(tài)監(jiān)聽器
AnimationController
//動(dòng)畫控制器
AnimationController controller;
//AnimationController是一個(gè)特殊的Animation對(duì)象,在屏幕刷新的每一幀,就會(huì)生成一個(gè)新的值,
// 默認(rèn)情況下,AnimationController在給定的時(shí)間段內(nèi)會(huì)線性的生成從0.0到1.0的數(shù)字
//用來(lái)控制動(dòng)畫的開始與結(jié)束以及設(shè)置動(dòng)畫的監(jiān)聽
//vsync參數(shù),存在vsync時(shí)會(huì)防止屏幕外動(dòng)畫(動(dòng)畫的UI不在當(dāng)前屏幕時(shí))消耗不必要的資源
//duration 動(dòng)畫的時(shí)長(zhǎng),這里設(shè)置的 seconds: 2 為2秒,當(dāng)然也可以設(shè)置毫秒 milliseconds:2000.
controller =
AnimationController(duration: const Duration(seconds: 2), vsync: this);
//動(dòng)畫開始、結(jié)束、向前移動(dòng)或向后移動(dòng)時(shí)會(huì)調(diào)用StatusListener
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
//動(dòng)畫從 controller.reverse() 反向執(zhí)行 結(jié)束時(shí)會(huì)回調(diào)此方法
print("status is completed");
// controller.reset(); 將動(dòng)畫重置到開始前的狀態(tài)
//開始執(zhí)行
//controller.forward();
} else if (status == AnimationStatus.dismissed) {
//動(dòng)畫從 controller.forward() 正向執(zhí)行 結(jié)束時(shí)會(huì)回調(diào)此方法
print("status is dismissed");
//controller.forward();
}else if (status == AnimationStatus.forward) {
print("status is forward");
//執(zhí)行 controller.forward() 會(huì)回調(diào)此狀態(tài)
}else if (status == AnimationStatus.reverse) {
//執(zhí)行 controller.reverse() 會(huì)回調(diào)此狀態(tài)
print("status is reverse");
}
});
AnimationController 的常用操作說明

flutter AnimationStatus 動(dòng)畫狀態(tài)說明

flutter PositionedTransition 實(shí)現(xiàn)中心縮放動(dòng)畫

動(dòng)畫開始與結(jié)束分析

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_x/base/base_appbar_page.dart';
/**
* RelativeRectTween 縮放動(dòng)畫
*/
class RelativeRectTweenPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return new RelativeRectTweenState();
}
}
class RelativeRectTweenState extends BaseAppBarPageState<RelativeRectTweenPage>
with SingleTickerProviderStateMixin {
//動(dòng)畫控制器
AnimationController controller;
Animation<RelativeRect> animation;
@override
String buildInitState() {
buildBackBar("動(dòng)畫", backIcon: Icons.arrow_back_ios);
controller =
AnimationController(duration: const Duration(seconds: 2), vsync: this);
//動(dòng)畫開始、結(jié)束、向前移動(dòng)或向后移動(dòng)時(shí)會(huì)調(diào)用StatusListener
controller.addStatusListener((status) {
if (status == AnimationStatus.completed) {
//動(dòng)畫從 controller.forward() 正向執(zhí)行 結(jié)束時(shí)會(huì)回調(diào)此方法
print("status is completed");
//反向執(zhí)行
//controller.reverse();
} else if (status == AnimationStatus.dismissed) {
//動(dòng)畫從 controller.reverse() 反向執(zhí)行 結(jié)束時(shí)會(huì)回調(diào)此方法
print("status is dismissed");
//controller.forward();
} else if (status == AnimationStatus.forward) {
print("status is forward");
//執(zhí)行 controller.forward() 會(huì)回調(diào)此狀態(tài)
} else if (status == AnimationStatus.reverse) {
//執(zhí)行 controller.reverse() 會(huì)回調(diào)此狀態(tài)
print("status is reverse");
}
});
// 這個(gè)動(dòng)畫的過程是
// 子widget 距左邊距離從 10.0 變化 到100.0
// 子widget 距上邊距離從 10.0 變化 到100.0
// 子widget 距右邊距離從 10.0 變化 到100.0
// 子widget 距下邊距離從 10.0 變化 到100.0
// 四邊同時(shí)變化 相同的距離 倍率,所以看出來(lái)是 中心縮小的動(dòng)畫
RelativeRectTween rectTween = RelativeRectTween(
//初始位置設(shè)置
begin: const RelativeRect.fromLTRB(
//子widget 距父布局 left 10.0
10.0,
//子widget 距父布局 top 10.0
10.0,
//子widget 距父布局 right 10.0
10.0,
//子widget 距父布局 bottom 10.0
10.0),
//結(jié)束位置設(shè)置
end: RelativeRect.fromLTRB(
//子widget 距父布局 left 100.0
100.0,
//子widget 距父布局 top 100.0
100.0,
//子widget 距父布局 right 100.0
100.0,
//子widget 距父布局 bottom 100.0
100.0,
),
);
//關(guān)聯(lián) controller
animation = rectTween.animate(controller);
return null;
}
@override
Widget buildWidget(BuildContext context) {
return buildSlideTransition();
}
@override
void dispose() {
super.dispose();
controller.dispose();
}
//漸變動(dòng)畫
Widget buildSlideTransition() {
return Stack(
children: <Widget>[
//必須作為Stack的子widget
PositionedTransition(
rect: animation,
child: Container(
color: Colors.grey,
child: Image.network(
"http://img5.duitang.com/uploads/item/201411/16/20141116124947_xBNxM.jpeg",
),
),
),
Positioned(
bottom: 20,
left: 20,
child: FlatButton(
onPressed: () {
if (controller.isDismissed) {
///正向動(dòng)畫開始
controller.forward();
} else if (controller.isCompleted) {
///反向動(dòng)畫開始
controller.reverse();
} else {
//停止
controller.dispose();
//重置動(dòng)畫
controller.reset();
}
},
child: Text("開始")),
)
],
);
}
}
flutter PositionedTransition 向上縮放

RelativeRectTween rectTween = RelativeRectTween( //初始位置設(shè)置 begin: const RelativeRect.fromLTRB( 50.0, 50.0, 50.0, 50.0), //結(jié)束位置設(shè)置 end: RelativeRect.fromLTRB( 50.0, 50.0, 50.0, 500.0, ), );
flutter PositionedTransition 向右縮放

RelativeRectTween rectTween = RelativeRectTween( //初始位置設(shè)置 begin: const RelativeRect.fromLTRB( 50.0, 50.0, 50.0, 50.0), //結(jié)束位置設(shè)置 end: RelativeRect.fromLTRB( 500.0, 50.0, 50.0, 50.0, ), );
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Flutter實(shí)現(xiàn)五種酷炫文字動(dòng)畫效果詳解
animated_text_kit這一動(dòng)畫庫(kù)有多種文字動(dòng)畫效果,文中將利用它實(shí)現(xiàn)五種酷炫的文字動(dòng)畫:波浪涌動(dòng)效果、波浪線跳動(dòng)文字組、彩虹動(dòng)效、滾動(dòng)廣告牌效果和打字效果,需要的可以參考一下2022-03-03
Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法總結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)屏幕旋轉(zhuǎn)方法,實(shí)例總結(jié)了屏幕旋轉(zhuǎn)的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
Android EditText限制輸入字符的方法總結(jié)
這篇文章主要介紹了 Android EditText限制輸入字符的方法總結(jié)的相關(guān)資料,這里提供了五種方法來(lái)實(shí)現(xiàn)并進(jìn)行比較,需要的朋友可以參考下2017-07-07
Android實(shí)現(xiàn)的狀態(tài)欄定制和修改方法
這篇文章主要介紹了Android實(shí)現(xiàn)的狀態(tài)欄定制和修改方法,涉及Android針對(duì)狀態(tài)欄屬性設(shè)置的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
詳解Retrofit2.0 公共參數(shù)(固定參數(shù))
這篇文章主要介紹了Retrofit2.0 公共參數(shù)(固定參數(shù)),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-04-04
Kotlin超簡(jiǎn)單實(shí)現(xiàn)StepView的方法
這篇文章主要介紹了Kotlin超簡(jiǎn)單實(shí)現(xiàn)StepView的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-11-11
Android自定義View實(shí)現(xiàn)縱向跑馬燈效果詳解
對(duì)于跑馬燈效果在我們?nèi)粘J褂玫腶pp中還是很常見的,比如外賣app的商家公告就使用了此效果,但是它是橫向滾動(dòng)的,橫向滾動(dòng)多適用于單條信息;但凡涉及到多條信息的滾動(dòng)展示,用縱向滾動(dòng)效果會(huì)有更好的用戶體驗(yàn),今天我們通過自定義View來(lái)看看如何實(shí)現(xiàn)縱向跑馬燈效果。2016-11-11

