Dart 如何定义颤振中的离散动画?

Dart 如何定义颤振中的离散动画?,dart,flutter,flutter-layout,flutter-animation,Dart,Flutter,Flutter Layout,Flutter Animation,是否可以在颤振中创建一个动画,该动画不会持续更改其值,但仅在给定的时间间隔内 我现在有下面的代码正在工作,但我确信有更好的解决方案 int aniValue = 0; bool lock = false; _asyncFunc() async { if(lock) return; else lock = true; await new Future.delayed(const Duration(milliseconds: 50), () {

是否可以在颤振中创建一个动画,该动画不会持续更改其值,但仅在给定的时间间隔内

我现在有下面的代码正在工作,但我确信有更好的解决方案

  int aniValue = 0;
  bool lock = false;
  _asyncFunc() async {
    if(lock) return;
    else     lock = true;

    await new Future.delayed(const Duration(milliseconds: 50), () {
      ++aniValue;

      if (aniValue == 41) {
        aniValue = 0;
      }
    });

    lock = false;

    setState(() {});
    _asyncFunc();
  }

可以将曲线定义为动画;具有非线性级数

颤振不提供“阶跃”曲线,但您可以相当轻松地创建:

class StepCurve extends Curve {
  final int stepCount;

  const StepCurve([this.stepCount = 2]) : assert(stepCount > 1);

  @override
  double transform(double t) {
    final progress = (t * stepCount).truncate();
    return 1 / (stepCount - 1) * progress;
  }
}
然后,您可以通过将其关联到之间的
曲线来自由使用它:

@override
Widget build(BuildContext context) {
  return AlignTransition(
    alignment: AlignmentGeometryTween(
      begin: Alignment.centerLeft,
      end: Alignment.centerRight,
    )
        .chain(CurveTween(curve: const StepCurve(5)))
        .animate(animationController),
    child: Container(
      color: Colors.red,
      width: 42.0,
      height: 42.0,
    ),
  );
}


另一个解决方案是使用

class TestAnim扩展了StatefulWidget{
@凌驾
_TestAnimState createState()=>\u TestAnimState();
}
类_TestAnimState扩展状态
使用SingleTickerProviderStateMixin{
动画控制器;
@凌驾
void initState(){
super.initState();
animationController=animationController(
vsync:这个,,
持续时间:常数持续时间(秒数:1),
)…重复();
}
@凌驾
小部件构建(构建上下文){
最终颜色=[
颜色,红色,
颜色,蓝色,
颜色,石灰,
颜色,紫色,
];
返回装饰框转换(
装饰:TweenSequence(colorsToTween(colors.toList())
.animate(animationController),
子项:const SizedBox.expand(),
);
}
}
可染色织物(
列表颜色)同步*{
对于(int i=0;i
如果不想连续更改,为什么要使用动画?你能详细说明一下你到底想实现什么吗?我正在开发一个应用程序,它有一个n x m的彩色“像素”矩阵,我不想每150毫秒刷新一次它们的所有颜色。很抱歉,如果动画真的是断章取义,我真的在颤振新。不,别担心。在这里使用动画框架是最理想的解决方案。感谢您出色的回答,这里的问题是StepCurve在位置上有间隙,但我希望及时得到它们。我的意思是,第一步是在1s之后,第二步是在另一个1s之后,依此类推。你可以把时间转换成步骤。在您的示例中,总共有40个步骤,持续时间为2秒。我注意到动画没有动画值,有没有办法在此处设置计数器或当前步骤值?我想您需要instead@BenSabo添加了一个示例,去看看:)
class TestAnim extends StatefulWidget {
  @override
  _TestAnimState createState() => _TestAnimState();
}

class _TestAnimState extends State<TestAnim>
    with SingleTickerProviderStateMixin {
  AnimationController animationController;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1),
    )..repeat();
  }

  @override
  Widget build(BuildContext context) {
    final colors = <Color>[
      Colors.red,
      Colors.blue,
      Colors.lime,
      Colors.purple,
    ];

    return DecoratedBoxTransition(
      decoration: TweenSequence(colorsToTween(colors).toList())
          .animate(animationController),
      child: const SizedBox.expand(),
    );
  }
}

Iterable<TweenSequenceItem<Decoration>> colorsToTween(
    List<Color> colors) sync* {
  for (int i = 0; i < colors.length - 1; i++) {
    yield TweenSequenceItem<Decoration>(
      tween: DecorationTween(
        begin: BoxDecoration(color: colors[i]),
        end: BoxDecoration(color: colors[i + 1]),
      ),
      weight: 1.0,
    );
  }
}