Flutter开发之——动画—Tween

一 概述

  • Tween动画,又叫补间动画
  • Tween有两个关键帧,begin起始帧,end结束帧,补间动画在begin和end之间插入补帧完成动画效果
  • 常见的Tween动画有:ColorTween、SizeTween、RectTween、IntTween、StepTween、ConstantTween、CurveTween

二 Tween

2.1 说明

  • Tween是将值变化的过程进行了封装(封装了begin起始值和end结束值)
  • 将之前通过setState刷新获取到的值转换为动画的值来完成显示

2.2 构造函数

Tween({
    this.begin,
    this.end,
  });

2.3 更换为Tween后对比

使用Tween之前
//定义变化量
double _size = 100;
//变化量作用对象
Container(
          height: _size,
          width: _size,
          color: Colors.blue,
          alignment: Alignment.center,
          child: Text('点我变大'),
		)
//变化量的修改		
  @override
  void initState() {
    super.initState();
     setState(() {
    	_size = 100+100*_controller.value;
  });
  }		
使用Tween之后
Animation<double> animation;//变化过程动画变量
//动画控制器
_controller =  AnimationController(
        vsync: this,
        lowerBound: 0,
        upperBound: 1,
        duration: Duration(milliseconds: 1000),
    )
//动画开始和结束值    
animation=Tween<double>(begin: 100,end: 300).animate(_controller);
 //作用对象
 Container(
      		height: animation.value,
            width: animation.value,
            color: Colors.red,
            alignment: Alignment.center,
            child: Text("点我变大"),
          )

三 示例

3.1 将上节动画使用Tween替换之后(Tween用于int,float类型变换)

代码
 AnimationController _controller;
 Animation<double> _animation;

  @override
  void initState() {
    super.initState();
     _controller =  AnimationController(
        vsync: this,
        lowerBound: 0,
        upperBound: 1,
        duration: Duration(milliseconds: 1000),
    )
      ..addStatusListener((AnimationStatus status) {
        if(status == AnimationStatus.completed){
          _controller.reverse();
        }else if(status == AnimationStatus.dismissed){
          _controller.forward();
        }
      });
    animation=Tween<double>(begin: 100,end: 300).animate(_controller);
  }
  //作用对象
  Center(
        child: GestureDetector(
          onTap: (){_controller.forward();},
          child: Container(
            height: animation.value,
            width: animation.value,
            color: Colors.red,
            alignment: Alignment.center,
            child: Text("点我执行动画"),
          ),
        ),
        )
 @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }       
效果图

3.2 ColorTween(ColorTween用于Color类型变换)

代码
//变量定义
AnimationController _controller;
 Animation<Color> animationColor;
 
 //动画监听
   @override
  void initState() {
    super.initState();
     _controller =  AnimationController(
        vsync: this,
        lowerBound: 0,
        upperBound: 1,
        duration: Duration(milliseconds: 1000),
    )
      ..addStatusListener((AnimationStatus status) {
        if(status == AnimationStatus.completed){
          _controller.reverse();
        }else if(status == AnimationStatus.dismissed){
          _controller.forward();
        }
      });
    animationColor=ColorTween(begin: Colors.red,end: Colors.blue).animate(_controller); 
  }
  //作用对象
  Center(
        child: GestureDetector(
          onTap: (){_controller.forward();},
          child: Container(
            height: 200,
            width: 200,
            color: animationColor.value,
            alignment: Alignment.center,
            child: Text("点我执行动画"),
          ),
        ),
        )
效果图

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Flutte中可以使用Flutter动画库来创建聊天语音播放动画。在Flutter中,动画可以通过使用AnimationController和Tween来实现。 首先,需要创建一个AnimationController对象来控制动画的进度。可以设置动画的持续时间和一个曲线来定义动画的速度曲线。然后,可以使用Tween来定义动画的初始值和结束值。 在聊天语音播放动画中,可以将AnimationController对象与某个按钮或触发器关联起来,以便在触发器被点击或滑动时开始动画。可以使用addStatusListener来监听动画的状态改变,例如动画开始、结束或循环等。 在动画开始时,可以通过调用AnimationController的forward方法来启动动画。然后,在动画的每一帧中,可以使用Tween的animate方法来根据动画的进度值获取当前的属性值。可以使用AnimatedBuilder小部件来包裹需要应用动画的相关小部件,并在builder回调中更新这些小部件的属性。 在聊天语音播放动画中,可以使用AnimatedContainer来实现音量动画效果,通过设置container的高度或宽度的变化来表示音量的大小。也可以结合使用AnimatedOpacity来实现透明度的变化效果,以模拟语音播放的动态效果。 最后,在动画播放结束时,可以使用AnimationController的reverse方法来反向播放动画,或者使用reset方法来复位动画的进度。 综上所述,通过使用Flutter动画库中的AnimationController、Tween和相关小部件,可以实现聊天语音播放动画,给用户带来更加生动和交互性的体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值