Flutter 旋转动画 线性变化的旋转动画

news2024/10/5 12:52:23

直接上代码

图片自己添加一张就好了

import 'dart:math';

import 'package:flutter/material.dart';

import 'package:flutter/animation.dart';

void main() => runApp(MyApp());
//旋转动画
class MyApp extends StatelessWidget {
  
  Widget build(BuildContext context) {
    return MaterialApp(
      color: Colors.blue,
      home: MyAnimatedWidget(),
    );
  }
}

class MyAnimatedWidget extends StatefulWidget {
  
  _MyAnimatedWidgetState createState() => _MyAnimatedWidgetState();
}

class _MyAnimatedWidgetState extends State<MyAnimatedWidget>
    with SingleTickerProviderStateMixin {
  late final AnimationController _repeatController;
  late final Animation<double> _animation;

  
  void initState() {
    super.initState();
    _repeatController = AnimationController(
      duration: const Duration(seconds: 3),
      vsync: this,
    )..repeat(); // 设置动画重复播放

    // 创建一个从0到1的补间动画   end: 1 * pi   转速
    _animation = Tween<double>(begin: 0, end: 1).animate(_repeatController);
  }

  
  Widget build(BuildContext context) {
    // 你的UI构建代码,使用_animation来控制动画
    // ...

    // 示例按钮来停止和继续动画
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Center(
          child: AnimatedSwitcher(
            duration: const Duration(milliseconds: 500),
            transitionBuilder: (child, animation) {
              return RotationTransition(
                turns: _animation!,
                child: child,
              );
            },
            child: Image.asset(
              'assets/shenji2.png',
            ),
          ),
        ),
        Text('Animation Example'),
        SizedBox(
          height: 20,
        ),
        ElevatedButton(
          onPressed: () {
            stopAnimation(); // 停止动画
          },
          child: Text('Stop Animation'),
        ),
        SizedBox(
          height: 20,
        ),
        ElevatedButton(
          onPressed: () {
            resumeAnimation(); // 继续动画
          },
          child: Text('Resume Animation'),
        ),
        SizedBox(
          height: 20,
        ),
        ElevatedButton(
          onPressed: () {
            resumeAnimationZY(); // 继续动画
          },
          child: Text('Resume Animation2'),
        ),
      ],
    );
  }

  void stopAnimation() {
    _repeatController.stop();
  }

  void resumeAnimation() {
    _repeatController.repeat(reverse: false);
    
  }

  void resumeAnimationZY() {
  ///左右转动
    _repeatController.repeat(reverse: true);
  
  }

  
  void dispose() {
    _repeatController.dispose(); // 不要忘记在dispose中释放资源
    super.dispose();
  }
}



创建一个动画组件给其他界面调用自己回收内存
不规则图片的中心旋转动画

import 'dart:math';

import 'package:flutter/material.dart';

class AnimatedBox extends StatefulWidget {
  // final Color color;
  // final Duration duration;

  const AnimatedBox({
    Key? key,
  }) : super(key: key);

  
  _AnimatedBoxState createState() => _AnimatedBoxState();
}

class _AnimatedBoxState extends State<AnimatedBox>
    with SingleTickerProviderStateMixin {
  late final AnimationController _repeatController;
  late final Animation<double> _animation;

  
  void initState() {
    super.initState();
    //启动动画
    _repeatController = AnimationController(
      duration: const Duration(seconds: 3),
      vsync: this,
    )..repeat(); // 设置动画重复播放

    // 创建一个从0到1的补间动画
    _animation =
        Tween<double>(begin: 0, end: 1 * pi).animate(_repeatController);
  }

  
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _repeatController,
      builder: (BuildContext context, Widget? child) {
        return Center(
          child: Stack(
            children: [
              Positioned(
                top: 0,
                bottom: 0,
                left: 0,
                right: 0,
                child: Center(
                  child: Image.asset(
                    'assets/ic_oxygen_pump_c_10.png', // 你想要居中的组件
                  ),
                ),
              ),
              Positioned(
                top: 0,
                bottom: 1.5,
                left: 0,
                right: 6,
                child: Center(
                  child: AnimatedSwitcher(
                    duration: const Duration(milliseconds: 500),
                    transitionBuilder: (child, animation) {
                      return RotationTransition(
                        turns: _animation,
                        child: child,
                      );
                    },
                    child: Image.asset(
                      'assets/ic_oxygen_pump_c_polat.png', // 你想要居中的组件
                    ),
                  ),
                ),
              ),
            ],
          ),
        );

        ///放大动画
        /*return Transform.scale(
          scale: _animation.value,
          child: Container(
            width: 100,
            height: 100,
            color: widget.color,
          ),
        );*/
      },
    );
  }

  
  void dispose() {
    ///回收动画
    _repeatController.dispose();
    super.dispose();
  }
}

图片是白色的

在这里插入图片描述
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1544937.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Luminar Neo:重塑图像编辑新纪元,Mac与Win双平台畅享创意之旅

在数字时代的浪潮中&#xff0c;图像编辑软件已成为摄影师和设计师们不可或缺的创作工具。Luminar Neo&#xff0c;作为一款专为Mac与Windows双平台打造的图像编辑软件&#xff0c;正以其卓越的性能和创新的编辑功能&#xff0c;引领着图像编辑的新潮流。 Luminar Neo不仅继承…

SOC 子模块---中断控制器

中断控制器对soc 中的各个外设进行中断管理&#xff0c;进行优先权排队&#xff0c;并送出IQR信号给CPU&#xff1b; 中断控制器在整个系统中的结构&#xff1a; IRQ<n>来源于不同的中断源&#xff0c;比如&#xff1a;I2C,SPI等&#xff0c;INTC收集这些中断&#xff0…

word文件如何转PDF格式?word转PDF的方法

在当今数字化时代&#xff0c;文档格式的转换已成为日常生活和工作中不可或缺的一部分。其中&#xff0c;将Word文档转换为PDF格式更是受到了广大用户的青睐。本文将详细介绍Word转PDF的方法&#xff0c;帮助读者轻松实现文档格式的转换&#xff0c;并探讨转换过程中的注意事项…

P6技巧:对计划执行纠偏措施

前言 对施工计划的滞后原因分析&#xff0c;通常采取由大到小、由高到低的方法。即首先查看总体进度偏差&#xff0c;再分析其偏差主要来源于哪部分。 项目进度评估与偏差控制 项目实施过程中&#xff0c;项目控制人员应对进度实施情况进行跟踪、采集数据&#xff0c;并根据…

图解Kafka架构学习笔记(二)

kafka的存储机制 https://segmentfault.com/a/1190000021824942 https://www.lin2j.tech/md/middleware/kafka/Kafka%E7%B3%BB%E5%88%97%E4%B8%83%E5%AD%98%E5%82%A8%E6%9C%BA%E5%88%B6.html https://tech.meituan.com/2015/01/13/kafka-fs-design-theory.html https://feiz…

数据可视化基础与应用-04-seaborn库从入门到精通01-02

总结 本系列是数据可视化基础与应用的第04篇seaborn&#xff0c;是seaborn从入门到精通系列第1-2篇。本系列的目的是可以完整的完成seaborn从入门到精通。主要介绍基于seaborn实现数据可视化。 参考 参考:数据可视化-seaborn seaborn从入门到精通01-seaborn介绍与load_datas…

CharacterController.Move called on inactive controller

【问题背景】 Unity3D中开发物体的移动&#xff0c;实现代码如下&#xff1a; public class TargetController : MonoBehaviour {private CharacterController character;private float speedRate 4f;private void Start(){character GetComponent<CharacterController&…

等保测评密评对照:一文看懂两者差异

最近&#xff0c;在去几个客户的办公室交流的方案的时候&#xff0c;都会被重点问到网络安全问题,在方案中“等保”是如何体现和落实的。而且有些客户的领导也会提到“密评”与“等保”如何衔接&#xff0c;是否有先后顺序&#xff0c;可否同时进行测评等问题。 关于“等保”与…

网络原理(7)——以太网数据帧和DNS协议(数据链路层和应用层)

目录 一、以太网数据帧&#xff08;数据链路层&#xff09; 二、DNS协议(域名解析系统&#xff0c;应用层协议) 一、以太网数据帧&#xff08;数据链路层&#xff09; 以太网横跨了数据链路层和物理层&#xff0c;这里只做简单介绍&#xff0c;因为普通程序员用不到这一块&am…

UI自动化_id 元素定位

## 导包selenium from selenium import webdriver import time1、创建浏览器驱动对象 driver webdriver.Chrome() 2、打开测试网站 driver.get("你公司的平台地址") 3、使浏览器窗口最大化 driver.maximize_window() 4、在用户名输入框中输入admin driver.find_ele…

hbase启动错误-local host is“master:XXXX“ destination is:master

博主的安装前提&#xff1a; zookeeper安装完成&#xff0c;且启动成功 hdfs高可用安装&#xff0c;yarn高可用安装&#xff0c;且启动成功 报错原因&#xff1a;端口配置不对 解决方案&#xff1a; 输入&#xff1a;hdfs getconf -confKey fs.default.name 然后把相应的…

WM8978 —— 带扬声器驱动程序的立体声编解码器(7)

接前一篇文章&#xff1a;WM8978 —— 带扬声器驱动程序的立体声编解码器&#xff08;6&#xff09; 十、音频接口 WM8978具有一个标准的音频接口&#xff0c;支持立体声数据在芯片之间的传输。该接口是一个3线标准音频接口&#xff0c;支持多种音频数据格式&#xff0c;包括I…

C++ 简单模拟实现 STL 中的 list 与 queue

目录 一&#xff0c;list 1&#xff0c; list 的节点与迭代器 2&#xff0c;list 的数据结构、一些简单的功能、构造函数 3&#xff0c;list 的对元素操作 4&#xff0c;C 11 的一些功能 5&#xff0c;完整代码&#xff1a; 二&#xff0c;queue 一&#xff0c;list std…

Visual Studio 小更新:改善变量的可见性

在 Visual Studio 2022 17.10 预览版 2 中&#xff0c;我们改善了一些小功能&#xff0c;例如&#xff1a;在调试版本中&#xff0c;变量窗口现已可以显示调用堆栈中任意帧的局部变量。 如需体验此功能&#xff0c;请直接安装最新预览版本&#xff0c;就可以知道是怎么一回事儿…

CSS及javascript

一、CSS简介 css是一门语言&#xff0c;用于控制网页的表现。 cascading style sheet:层叠样式表 二、css的导入方式 css代码与html代码的结合方式 &#xff08;1&#xff09;css导入html有三种方式&#xff1a; 1.内联样式&#xff1a;<div style"color:red&quo…

mac 系统如何生成秘钥

1.打开终端&#xff0c;输入 cd ~/.ssh 进入.ssh目录&#xff0c;输入 ls 检查是否已经存在SSH密钥。如果看到类似 id_rsa.pub 的文件&#xff0c;说明已经有一对公钥和私钥&#xff0c;不用新建&#xff0c;直接查看就可以&#xff0c;如果没有需要生成新的密钥。 2.在终端输…

Unity 视频组件 VideoPlayer

组件添加&#xff1a; 在自己定义的组件下&#xff08;例如&#xff1a;Panel&#xff09; 点击 Inspector 面板中的 AddComponent &#xff0c;输入“VideoPlayer”。 资源 这里 视频资源有两种形式&#xff0c;第一种是 VideoClip &#xff0c;需要将视频文件拖拽到该属性字段…

【C语言】【Leetcode】88. 合并两个有序数组

文章目录 一、题目二、思路再思考 一、题目 链接: link 二、思路 这题属于简单题&#xff0c;比较粗暴的做法就是直接比较两个数组&#xff0c;先把第二个数组加到第一个的后面&#xff0c;如何冒泡排序&#xff0c;这种方法简单粗暴但有效&#xff0c;可是不适用于这题&…

政安晨:【Keras机器学习实践要点】(二)—— 给首次接触Keras 3 的朋友

政安晨的个人主页&#xff1a;政安晨 欢迎 &#x1f44d;点赞✍评论⭐收藏 收录专栏: TensorFlow与Keras实战演绎机器学习 希望政安晨的博客能够对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff01; 介绍 Keras 3是一个深度学习框架&#xff0…

牛客网BC-33 统计成绩(数组排序思想)

题目如下 --------------------------------------------------------------------------------------------------------------------------------- 思路&#xff1a;以数组形式输入&#xff0c;并将数组顺序&#xff08;或者逆序&#xff09;排序&#xff0c;最后输出最大值最…