当前位置: 移动技术网 > IT编程>移动开发>Android > flutter RotationTransition实现旋转动画

flutter RotationTransition实现旋转动画

2019年09月06日  | 移动技术网IT编程  | 我要评论

品色糖,凤舞战歌,儿女传奇胭脂错

本文实例为大家分享了flutter rotationtransition实现旋转动画的具体代码,供大家参考,具体内容如下

flutter 动画状态监听器

animationcontroller

 //动画控制器
  animationcontroller controller;
 //animationcontroller是一个特殊的animation对象,在屏幕刷新的每一帧,就会生成一个新的值,
 // 默认情况下,animationcontroller在给定的时间段内会线性的生成从0.0到1.0的数字
 //用来控制动画的开始与结束以及设置动画的监听
 //vsync参数,存在vsync时会防止屏幕外动画(动画的ui不在当前屏幕时)消耗不必要的资源
 //duration 动画的时长,这里设置的 seconds: 2 为2秒,当然也可以设置毫秒 milliseconds:2000.
 controller =
  animationcontroller(duration: const duration(seconds: 2), vsync: this);
 //动画开始、结束、向前移动或向后移动时会调用statuslistener
 controller.addstatuslistener((status) {
  if (status == animationstatus.completed) {
  //动画从 controller.reverse() 反向执行 结束时会回调此方法
  print("status is completed");
  // controller.reset(); 将动画重置到开始前的状态
  //开始执行
  //controller.forward();
  } else if (status == animationstatus.dismissed) {
  //动画从 controller.forward() 正向执行 结束时会回调此方法
  print("status is dismissed");
  //controller.forward();
  }else if (status == animationstatus.forward) {
  print("status is forward");
  //执行 controller.forward() 会回调此状态
  }else if (status == animationstatus.reverse) {
  //执行 controller.reverse() 会回调此状态
  print("status is reverse");
  }
 });

animationcontroller 的常用操作说明

flutter animationstatus 动画状态说明

1 flutter rotationtransition实现旋转动画

 //动画控制器
 animationcontroller controller;


 //animationcontroller是一个特殊的animation对象,在屏幕刷新的每一帧,就会生成一个新的值,
 // 默认情况下,animationcontroller在给定的时间段内会线性的生成从0.0到1.0的数字
 //用来控制动画的开始与结束以及设置动画的监听
 //vsync参数,存在vsync时会防止屏幕外动画(动画的ui不在当前屏幕时)消耗不必要的资源
 //duration 动画的时长,这里设置的 seconds: 2 为2秒,当然也可以设置毫秒 milliseconds:2000.
 controller =
  animationcontroller(duration: const duration(seconds: 2), vsync: this);
 //动画开始、结束、向前移动或向后移动时会调用statuslistener
 controller.addstatuslistener((status) {
  if (status == animationstatus.completed) {
  //动画从 controller.forward() 正向执行 结束时会回调此方法
  print("status is completed");
  } else if (status == animationstatus.dismissed) {
  //动画从 controller.reverse() 反向执行 结束时会回调此方法
  print("status is dismissed");
  } else if (status == animationstatus.forward) {
  print("status is forward");
  //执行 controller.forward() 会回调此状态
  } else if (status == animationstatus.reverse) {
  //执行 controller.reverse() 会回调此状态
  print("status is reverse");
  }
 });

执行动画的 widget

 //旋转
 widget buildrotationtransition() {
 return center(
  child: rotationtransition(
  //设置动画的旋转中心
  alignment: alignment.center,
  //动画控制器
  turns: controller,
  //将要执行动画的子view
  child: container(
   width: 100,
   height: 100,
   color: colors.grey,
  ),
  ),
 );
 }
}

2 flutter rotationtransition实现无限循环旋转动画

实现方法 就是在动画结束的时候再开启动画

//动画开始、结束、向前移动或向后移动时会调用statuslistener
 controller.addstatuslistener((status) {
  if (status == animationstatus.completed) {
  //动画从 controller.forward() 正向执行 结束时会回调此方法
  print("status is completed");
  //重置起点
  controller.reset();
  //开启
  controller.forward();
  } else if (status == animationstatus.dismissed) {
  //动画从 controller.reverse() 反向执行 结束时会回调此方法
  print("status is dismissed");
  } else if (status == animationstatus.forward) {
  print("status is forward");
  //执行 controller.forward() 会回调此状态
  } else if (status == animationstatus.reverse) {
  //执行 controller.reverse() 会回调此状态
  print("status is reverse");
  }
 });

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网