Flutter 中动画的使用

…addListener(() {
// 更新状态
setState(() {});
});
//在启动动画时,使用 repeat(reverse: true),让动画来回重复执行。
controller.repeat(reverse: true);
// 监听动画状态。在动画结束时,反向执行;在动画反向执行完毕时,重新启动执行。
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
//在动画结束时,反向执行
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
//在动画反向执行完毕时,重新启动执行
controller.forward();
}
});
// 开始动画
controller.forward();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“基础动画”),
),
body: Center(
child: Container(
width: animation.value,
height: animation.value,
color: Colors.yellow,
),
),
);
}

@override
void dispose() {
// 停止动画
controller.stop();
super.dispose();
}
}

可以看到 animation只提供动画数据,因此我们还需要监听动画执行进度,并在回调中使用 setState 强制刷新界面才能看到动画效果。这些步骤都是固定的,有没有什么更简单的方法吗?

AnimatedWidget 与 AnimatedBuilder

通过这两个widget 可以简化我们的动画步骤。 AnimatedWidget: 把对动画的监听放到 AnimatedWidget中,在里面就可以拿到动画的值,进行页面的重绘。

// 通过 AnimatedWidget 来实现动画

class AnimatedWidgetDemo extends AnimatedWidget {
AnimatedWidgetDemo({Key key, Animation animation})
: super(key: key, listenable: animation);

@override
Widget build(BuildContext context) {
final Animation animation = listenable;
return Container(
width: animation.value,
height: animation.value,
color: Colors.red,
);
}
}

class AnimationDemo2 extends StatefulWidget {
@override
State createState() {
return _AnimationDemo2State();
}
}

class _AnimationDemo2State extends State with SingleTickerProviderStateMixin {
AnimationController controller;
Animation animation;

@override
void initState() {
super.initState();
// 创建 controller
controller = AnimationController(
vsync: this, duration: Duration(milliseconds: 1000));

animation = Tween(begin: 50.0, end: 250.0).animate(controller);

//在启动动画时,使用 repeat(reverse: true),让动画来回重复执行。
controller.repeat(reverse: true);
// 监听动画状态。在动画结束时,反向执行;在动画反向执行完毕时,重新启动执行。
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
//在动画结束时,反向执行
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
//在动画反向执行完毕时,重新启动执行
controller.forward();
}
});
// 开始动画
controller.forward();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“AnimatedWidgetDemo”),
),
body: Cente
r(
child: AnimatedWidgetDemo(
animation: animation,
),
),
);
}

@override
void dispose() {
controller.stop();
super.dispose();
}
}

AnimatedBuilder: 将动画和渲染逻辑分开。

class _AnimationDemo3State extends State with SingleTickerProviderStateMixin {
AnimationController controller;
Animation animation;

@override
void initState() {
super.initState();
// 创建 controller
controller = AnimationController(
vsync: this, duration: Duration(milliseconds: 1000));

animation = Tween(begin: 50.0, end: 250.0).animate(controller);

//在启动动画时,使用 repeat(reverse: true),让动画来回重复执行。
controller.repeat(reverse: true);
// 监听动画状态。在动画结束时,反向执行;在动画反向执行完毕时,重新启动执行。
animation.addStatusListener((status) {
if (status == AnimationStatus.completed) {
//在动画结束时,反向执行
controller.reverse();
} else if (status == AnimationStatus.dismissed) {
//在动画反向执行完毕时,重新启动执行
controller.forward();
}
});
// 开始动画
controller.forward();
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“AnimatedWidgetDemo”),
),
body: AnimatedBuilder(
animation: animation,
builder: (context, child) => Center(
child: Container(
width: animation.value,
height: animation.value,
color: Colors.deepPurple,
),
),
));
}

@override
void dispose() {
controller.stop();
super.dispose();
}
}

可以看到 通过AnimatedBuilder,传入 animation ,然后可以在里面的widget 拿到 animation 的值,然后设置相应的属性,更新UI。

通过上面几种方式对比,仅仅是在动画监听 做了修改而已,从自己添加 listener 到继承 AnimationWidget 传入animation对象,在里面可以使用到;在到 AnimationBuild ,直接传入animation 对象,字面的子 widget 获取到 animation的属性后,更新UI,基本思路都是差不多。

hero 动画

实现小图到大图页面逐步放大的动画切换效果,而当用户关闭大图时,也实现原路返回的动画。这样的跨页面共享的控件动画效果有一个专门的名词,即“共享元素变换”(Shared Element Transition)。

Flutter 也有类似的概念,即 Hero 控件。通过 Hero,我们可以在两个页面的共享元素之间,做出流畅的页面切换效果。

为了实现共享元素变换,我们需要将这两个组件分别用 Hero 包裹,并同时为它们设置相同的 tag “hero”

class AnimationDemo4 extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(“hero动画”),
),
body: GestureDetector(
//手势监听点击

最后

小编这些年深知大多数初中级Android工程师,想要提升自己,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助

因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取
4年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。**

[外链图片转存中…(img-EYMd4Xzm-1719053157150)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人

都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

资料⬅专栏获取

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值