美文网首页
flutter 旋转 缩放动画,基本使用

flutter 旋转 缩放动画,基本使用

作者: 微风_10a5 | 来源:发表于2021-09-01 11:52 被阅读0次

有一段时间没有更新,最近在项目中,有使用到动画,就把之前学习过的动画,和最近学习过的动画,分享给大家;先看最终效果


rotation.gif
scale.gif
旋转动画源码如下
import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_test_demos/custom_circle_widget.dart';

class RotationAnimationDemoPage extends StatefulWidget {
  @override
  _RotationAnimationDemoPageState createState() =>
      _RotationAnimationDemoPageState();
}

class _RotationAnimationDemoPageState extends State<RotationAnimationDemoPage>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  @override
  void initState() {
    _animationController = AnimationController(
        vsync: this,
        // upperBound: 180/360,
        duration: Duration(milliseconds: 3000),
    )
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          // _animationController.reset();
          // _animationController.forward();
        }
      });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("paint 12"),
      ),
      body: Column(
        children: [
          ButtonBar(
            alignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: () {
                    _animationController.forward();
                  },
                  child: Text("开始")),
              ElevatedButton(
                  onPressed: () {
                    _animationController.stop();
                  },
                  child: Text("停止")),
              ElevatedButton(
                  onPressed: () {
                    _animationController.reverse();
                  },
                  child: Text("反转")),
              ElevatedButton(
                  onPressed: () {
                    _animationController.forward();
                    _animationController.repeat();
                  },
                  child: Text("循环执行")),
              ElevatedButton(
                  onPressed: () {
                    _animationController.forward();
                  },
                  child: Text("旋转180度"))
            ],
          ),
          Expanded(
            child: Center(
                child: RotationTransition(
              turns: _animationController,
              child: Icon(
                Icons.refresh,
                size: 100,
              ),
            )),
          )
        ],
      ),
    );
  }
}


如果想旋转指定的角度 ,比如说想只旋转180度的话,就把// upperBound: 180/360,,这句注释取消就可以了,upperBound指的是动画最大值,系统默认为1.0,那我们就让他旋转到0.5,那么0.5*360 =180,就刚好是旋转180度喽;

缩放动画源码如下
import 'dart:math';
import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:flutter_test_demos/custom_circle_widget.dart';

class ScaleAnimationDemoPage extends StatefulWidget {
  @override
  _ScaleAnimationDemoPageState createState() => _ScaleAnimationDemoPageState();
}

class _ScaleAnimationDemoPageState extends State<ScaleAnimationDemoPage>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  Animation<double> scale;

  @override
  void initState() {
    _animationController = AnimationController(
        vsync: this,
        duration: Duration(milliseconds: 3000),
        )
      ..addStatusListener((status) {
        // if (status == AnimationStatus.completed) {
        //   // _animationController.reset();
        //   _animationController.reverse();
        // }else if(status == AnimationStatus.dismissed){
        //   _animationController.forward();
        //
        // }
      });
    scale = Tween(begin: 1.2,end:0.01 ).animate(_animationController);

    super.initState();
  }


  @override
  void dispose() {
    _animationController.dispose();

    // TODO: implement dispose
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("paint 12"),
      ),
      body: Column(
        children: [
          ButtonBar(
            alignment: MainAxisAlignment.center,
            children: [
              ElevatedButton(
                  onPressed: () {
                    _animationController.forward();
                  },
                  child: Text("开始")),
              ElevatedButton(
                  onPressed: () {
                    _animationController.stop();
                  },
                  child: Text("停止")),
              ElevatedButton(
                  onPressed: () {
                    _animationController.reverse();
                  },
                  child: Text("反转")),
              ElevatedButton(
                  onPressed: () {
                    _animationController.reset();
                    _animationController.forward();
                    _animationController.repeat();
                  },
                  child: Text("循环执行")),
            ],
          ),
          Expanded(
            child: Center(
                child: ScaleTransition(
              scale: scale,
              child: Icon(
                Icons.refresh,
                size: 100,
              ),
            )),
          )
        ],
      ),
    );
  }
}

如果想实现 缩小-->放大-->缩小-->放大这样连贯的动画(如:scale.gif最后一小段的动画效果),(而不是 缩小-->回到起始位置-->缩小-->回到起始位置),就把下面代码注释取消就可以了

       // if (status == AnimationStatus.completed) {
        //   // _animationController.reset();
        //   _animationController.reverse();
        // }else if(status == AnimationStatus.dismissed){
        //   _animationController.forward();
        //
        // }

结尾

看到这里的小伙们,或者觉得文章对你有点帮助的话,请点赞加关注喽,您的反馈就是我们前进的动力。后续会分享更多关于移动端的干货。谢谢~~

相关文章

  • flutter 旋转 缩放动画,基本使用

    有一段时间没有更新,最近在项目中,有使用到动画,就把之前学习过的动画,和最近学习过的动画,分享给大家;先看最终效果...

  • 微点滴:iOS动画 (基本动画)

    基本动画iOSApp基本的动画就是移动,旋转,缩放CALayer:隐式动画Block动画(UIView动画):帧动...

  • flutter 常用动画的封装

    flutter中常用简单动画(平移、旋转、缩放、隐藏显示)的封装。 用法: 直接播放动画: 通过行为控制动画 源码...

  • 第一篇

    使用css实现旋转,缩放,阴影,动画的效果。

  • Android 几种动画总结

    四种基本动画 透明度渐变动画(AlphaAnimation) 旋转动画(RotateAnimation) 缩放动画...

  • Flutter(6)-动画(平移/缩放/旋转/Alpha)

    本篇简单介绍下在Flutter中动画的实现以及示例平移、缩放、旋转和alpha动画。 1.动画相关知识介绍 在Fl...

  • CoreAnimation动画

    iOSApp基本的动画就是移动,旋转,缩放 这些,UIView的分类UIViewKeyframeAnimation...

  • Android动画原理

    动画分类 补间动画 旋转、位移、透明度、缩放 属性动画 同样的属性动画也可以做到对View进行缩放、移动、旋转以及...

  • android 简单动画

    平移动画 旋转 // 缩放动画 // 透明度

  • Flutter动画 --- 平移/旋转/缩放/渐变

    PS: Flutter中文网提供的动画组件真心太多了,貌似有25种,看着裂开了。。。太多了,用的时候反而不好选择...

网友评论

      本文标题:flutter 旋转 缩放动画,基本使用

      本文链接:https://www.haomeiwen.com/subject/olsxwltx.html