基于Flutter实现爱心三连动画效果

2022-07-16,,,,

前言

我们开始 flutter 动画相关篇章之旅,在应用中通过动效能够给用户带来更愉悦的体验,比较典型的例子就是一些直播平台的动效了,比如送火箭能做出来那种火箭发射的动效——感觉倍有面子,当然这是土豪的享受,我等码农只在视频里看过。本篇我们来介绍基于 animation 类实现的基本动画构建。

animation 简介

animation 是一个抽象类,它并不参与屏幕的绘制,而是在设定的时间范围内对一段区间值进行插值。插值的方式可以是线性、曲线、一个阶跃函数或其他能够想到的方式。这个类的对象能够知道当前的值和状态(完成或消失)。animation 类提供了一个监听回调方法,当它的值改变的时候,就会调用该方法:

@override
void addlistener(voidcallback listener);

因此,在监听回调里,我们可以来刷新界面,通过animation 对象最新的值控制 ui 组件的位置、尺寸、角度,从而实现动画的效果animation 类通常会和 animationcontroller 一起使用。

animationcontroller 简介

animationcontroller 是一个特殊的 animation 类,它继承自 animation<double>。每当硬件准备好绘制下一帧时,animationcontroller就会产生一个新的值。默认情况下 animationcontroller 会在给定的时间范围内产生的值是从0到1.0的线性序列值(通常60个值/秒,以达到60 fps的效果)。例如,下面的代码构建一个时长为2秒的 animationcontroller

var controller =
    animationcontroller(duration: const duration(seconds: 2), vsync: this);

animationcontroller 具有 forwardreverse等控制动画的方法,通常用于控制动画的开始和恢复。

连接 animation 和 animationcontroller 的是 animatable类,该类也是一个抽象类, 常用的的实现类包括 tween<t>(线性插值),curvetween(曲线插值)。animatable 类有一个 animate 方法,该方法接收 animation<double>类参数(通常是 animationcontroller),并返回一个 animation 对象。

animation<t> animate(animation<double> parent) {
  return _animatedevaluation<t>(parent, this);
}

animate方法使用给定的 animation<double>对象驱动完成动效,但使用的值的范围是自身的值,从而可以构建自定义值范围的动效。比如,要构建一个2秒内从0增长100的动效值,可以使用如下的代码。

var controller =
        animationcontroller(duration: const duration(seconds: 2), vsync: this);
var animation = tween<double>(begin: 0, end: 100).animate(controller);

应用 - 爱心三连

有了上面的基础,我们就可以开始牛刀小试了,我们先来一个爱心三连放大缩小的动效,如下所示,首次点击逐渐放大,再次点击逐渐缩小恢复到原样。

界面代码很简单,三个爱心其实就是使用stack 将三个不同颜色的爱心 icon 组件叠加在一起,然后通过 animtion对象的值改变 icon 的大小。在 animation 值变化的监听回调里使用 setstate 刷新界面就好了。完整代码如下:

import 'package:flutter/material.dart';

class animtiondemo extends statefulwidget {
  const animtiondemo({key? key}) : super(key: key);

  @override
  _animtiondemostate createstate() => _animtiondemostate();
}

class _animtiondemostate extends state<animtiondemo>
    with singletickerproviderstatemixin {
  late animation<double> animation;
  late animationcontroller controller;

  @override
  void initstate() {
    super.initstate();
    controller =
        animationcontroller(duration: const duration(seconds: 1), vsync: this);
    animation = tween<double>(begin: 40, end: 100).animate(controller)
      ..addlistener(() {
        setstate(() {});
      });
    controller.addstatuslistener((status) {
      print(status);
    });
  }

  @override
  widget build(buildcontext context) {
    return scaffold(
      appbar: appbar(
        title: text('animation 动画'),
      ),
      body: center(
        child: stack(
          alignment: alignment.center,
          children: [
            icon(
              icons.favorite,
              color: colors.red[100],
              size: animation.value * 1.5,
            ),
            icon(
              icons.favorite,
              color: colors.red[400],
              size: animation.value,
            ),
            icon(
              icons.favorite,
              color: colors.red[600],
              size: animation.value / 2,
            ),
          ],
        ),
      ),
      floatingactionbutton: floatingactionbutton(
        child: icon(icons.play_arrow, color: colors.white),
        onpressed: () {
          if (controller.status == animationstatus.completed) {
            controller.reverse();
          } else {
            controller.forward();
          }
        },
      ),
    );
  }

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

这里需要提的是在_animtiondemostate中混入了singletickerproviderstatemixin,这里其实是为 animationcontroller 提供了一个 tickerproivder 对象。tickerproivder对象会在每一帧刷新前触发一个 ontick回调,从而实现animationcontroller的值更新。

总结

本篇介绍了flutter 动画构建类 animation 和 animationcontroller 的使用,通过这两个类可以实现很多基础动画效果,例如常见的进度条、缩放、旋转、移动等。接下来我们还将介绍基于 animation 实现动画的其他方式和其他类型的动画效果。

到此这篇关于基于flutter实现爱心三连动画效果的文章就介绍到这了,更多相关flutter爱心三连动画内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《基于Flutter实现爱心三连动画效果.doc》

下载本文的Word格式文档,以方便收藏与打印。