Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaolongs committed Jul 20, 2020
1 parent ec4517b commit 5fd23a2
Show file tree
Hide file tree
Showing 13 changed files with 802 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
**/doc/api/
.dart_tool/
.flutter-plugins
.flutter-plugins-dependencies
.packages
.pub-cache/
.pub/
build/

# Android related
**/android/**/gradle-wrapper.jar
**/android/.gradle
**/android/captures/
**/android/gradlew
**/android/gradlew.bat
**/android/local.properties
**/android/**/GeneratedPluginRegistrant.java

# iOS/XCode related
**/ios/**/*.mode1v3
**/ios/**/*.mode2v3
**/ios/**/*.moved-aside
**/ios/**/*.pbxuser
**/ios/**/*.perspectivev3
**/ios/**/*sync/
**/ios/**/.sconsign.dblite
**/ios/**/.tags*
**/ios/**/.vagrant/
**/ios/**/DerivedData/
**/ios/**/Icon?
**/ios/**/Pods/
**/ios/**/.symlinks/
**/ios/**/profile
**/ios/**/xcuserdata
**/ios/.generated/
**/ios/Flutter/App.framework
**/ios/Flutter/Flutter.framework
**/ios/Flutter/Flutter.podspec
**/ios/Flutter/Generated.xcconfig
**/ios/Flutter/app.flx
**/ios/Flutter/app.zip
**/ios/Flutter/flutter_assets/
**/ios/Flutter/flutter_export_environment.sh
**/ios/ServiceDefinitions.json
**/ios/Runner/GeneratedPluginRegistrant.*

# Exceptions to above rules.
!**/ios/**/default.mode1v3
!**/ios/**/default.mode2v3
!**/ios/**/default.pbxuser
!**/ios/**/default.perspectivev3
!/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: 84c84fb24914e098667649be04614f6ea19d689c
channel: dev

project_type: package
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## [0.0.1] - TODO: Add release date.

* TODO: Describe initial release.
1 change: 1 addition & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TODO: Add your license here.
7 changes: 7 additions & 0 deletions lib/shake_animation_widget.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


export 'package:shake_animation_widget/src/shake_animation_builder.dart';
export 'package:shake_animation_widget/src/shake_animation_controller.dart';
export 'package:shake_animation_widget/src/shake_animation_text.dart';
export 'package:shake_animation_widget/src/shake_animation_type.dart';
export 'package:shake_animation_widget/src/shake_animation_widget.dart';
128 changes: 128 additions & 0 deletions lib/src/shake_animation_builder.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shake_animation_widget/src/shake_animation_type.dart';

/**
* 创建人: Created by zhaolong
* 创建时间:Created by on 2020/7/17.
*
* 可关注公众号:我的大前端生涯 获取最新技术分享
* 可关注网易云课堂:https://study.163.com/instructor/1021406098.htm
* 可关注博客:https://blog.csdn.net/zl18603543572
*/

///lib/demo/shake/shake_animation_builder.dart
/// 抖动动画效果的 Builder
class ShakeAnimationBuilder extends StatelessWidget {
///[child] 执行动画的组件
///[animation] 执行的动画
ShakeAnimationBuilder(
{@required this.child,
@required this.animation,
this.randomValue = 5,
this.shakeAnimationType = ShakeAnimationType.RoateShake});

///执行动画的子Widget
final Widget child;

///动画的定义
final Animation<double> animation;

///抖动的类型
final ShakeAnimationType shakeAnimationType;

///随机动画时使用构建随机数
final Random random = Random();

///随机动画时抖动的波动范围
final double randomValue;

///lib/demo/shake/shake_animation_builder.dart
@override
Widget build(BuildContext context) {
///通过 AnimatedBuilder 组合动画
return AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget child) {
return new Transform(

///构建Matrix4
transform: buildMatrix4(),

///中心对齐
alignment: Alignment.center,
child: this.child);
},
);
}

///lib/demo/shake/shake_animation_builder.dart
///根据不同的模式来构建不同的矩阵变化
Matrix4 buildMatrix4() {
if (shakeAnimationType == ShakeAnimationType.RoateShake) {
///在XOY平面绕Z轴的旋转
return Matrix4.rotationZ(animation.value);
} else if (shakeAnimationType == ShakeAnimationType.RandomShake) {
///随机使用旋转、上下平移、左右平移
return buildRandowMatrix4();
} else {
double dx = 0;
double dy = 0;
if (shakeAnimationType == ShakeAnimationType.LeftRightShake) {
///X轴方向的平移
dx = animation.value * 15;
} else if (shakeAnimationType == ShakeAnimationType.TopBottomShake) {
///Y轴方向平移
dy = animation.value * 15;
} else {
///对齐线方向平移
dx = animation.value * 15;
dy = animation.value * 15;
}

print("dx $dx dy $dy");

///在XOY平面的平移
return Matrix4.translationValues(dx, dy, 0);
}
}

///lib/demo/shake/shake_animation_builder.dart
///构建随机变换的矩阵
///[animation.value]同时要适配旋转,
///[Matrix4]的旋转是使用弧度计算的,一般抖动使用 0.1左右的弧度微旋转即可
///所以这时配置的[animation.value]的取值范围建议使用 [-0.1,0.1]
///那么对于[Matrix4]的translationValues平移来讲是使用的逻辑像素
/// [-0.1,0.1]这个范围的变动对于平移无法有明显的抖动效果
/// 所以在这里 对于平移来说使用的 [-1.5,1.5] 就会有明显一点的抖动效果
///[random.nextDouble()]这个方法的值范围为 [0.0-1.0]
///然后通过结合配置的[randomValue]抖动的波动范围 默认为 5
/// [Matrix4]平移范围为 [-1.5,6.5]
Matrix4 buildRandowMatrix4() {
///随机数
int nextRandom = random.nextInt(10);

///Matrix4矩阵偏移量
double dx = 0;
double dy = 0;
if (nextRandom % 4 == 0) {
///左右平移
dx = animation.value * 15 + randomValue * random.nextDouble();
return Matrix4.translationValues(dx, dy, 0);
} else if (nextRandom % 4 == 1) {
///上下平移
dy = animation.value * 15 + randomValue * random.nextDouble();
return Matrix4.translationValues(dx, dy, 0);
} else if (nextRandom % 4 == 2) {
///对角线平移
dx = animation.value * 15 + randomValue * random.nextDouble();
dy = animation.value * 15 + randomValue * random.nextDouble();
return Matrix4.translationValues(dx, dy, 0);
} else {
///旋转
return Matrix4.rotationZ(animation.value);
}
}
}
48 changes: 48 additions & 0 deletions lib/src/shake_animation_controller.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

/// 创建人: Created by zhaolong
/// 创建时间:Created by on 2020/7/17.
///
/// 可关注公众号:我的大前端生涯 获取最新技术分享
/// 可关注网易云课堂:https://study.163.com/instructor/1021406098.htm
/// 可关注博客:https://blog.csdn.net/zl18603543572
///lib/demo/shake/shake_animation_controller.dart
///抖动监听
typedef ShakeAnimationListener = void Function(bool isOpen, int shakeCount);

///抖动动画控制器
class ShakeAnimationController {
///当前抖动动画的状态
bool animationRunging = false;

///监听
ShakeAnimationListener _shakeAnimationListener;

///控制器中添加监听
setShakeListener(ShakeAnimationListener listener) {
_shakeAnimationListener = listener;
}

///打开
void start({int shakeCount = 1}) {
if (_shakeAnimationListener != null) {
animationRunging = true;
_shakeAnimationListener(true, shakeCount);
}
}

///关闭
void stop() {
if (_shakeAnimationListener != null) {
animationRunging = false;
_shakeAnimationListener(false, 0);
}
}

///移除监听
void removeListener() {
_shakeAnimationListener = null;
}
}
110 changes: 110 additions & 0 deletions lib/src/shake_animation_text.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shake_animation_widget/src/shake_animation_type.dart';
import 'package:shake_animation_widget/src/shake_animation_widget.dart';

/// 创建人: Created by zhaolong
/// 创建时间:Created by on 2020/7/17.
///
/// 可关注公众号:我的大前端生涯 获取最新技术分享
/// 可关注网易云课堂:https://study.163.com/instructor/1021406098.htm
/// 可关注博客:https://blog.csdn.net/zl18603543572
///
///
/// 抖动文本组件
class ShakeTextAnimationWidget extends StatefulWidget {
///需要添加动画的文本
final String animationString;

///文本样式
final TextStyle textStyle;

///文字之间的间距
final double space;

///文字的行间距
final double runSpace;

///抖动次数
final shakeCount;
ShakeTextAnimationWidget(
{@required this.animationString,
this.textStyle,
this.space = 1,
this.runSpace = 6,
this.shakeCount = 0});

@override
_TextAnimationState createState() => _TextAnimationState();
}

/// lib/demo/shake/shake_animation_text.dart
class _TextAnimationState extends State<ShakeTextAnimationWidget> {
///用来保存形成抖动动画的每个文字
List<Widget> textWidgetList = [];
@override
void initState() {
super.initState();

///遍历String中的字符
for (var i = 0; i < widget.animationString.length; i++) {
///拆分文字
String itemStr = widget.animationString.substring(i, i + 1);

///创建抖动类型
ShakeAnimationType shakeAnimationType = ShakeAnimationType.LeftRightShake;
if (i % 3 == 0) {
///左右
shakeAnimationType = ShakeAnimationType.LeftRightShake;
} else if (i % 3 == 1) {
///上下
shakeAnimationType = ShakeAnimationType.TopBottomShake;
} else {
///微旋转
shakeAnimationType = ShakeAnimationType.RoateShake;
}
if (itemStr.trim().length == 0) {
///如果当前截取的是空格 就不添加动画效果
textWidgetList.add(
Text(
itemStr,
style: widget.textStyle,
),
);
} else {
///构建动画
ShakeAnimationWidget animationWidget = ShakeAnimationWidget(
///执行动画的子Widget
///这里只是一个字
child: Text(
itemStr,
style: widget.textStyle,
),

///抖动次数限制
shakeCount: widget.shakeCount,

///抖动动画的类型
shakeAnimationType: shakeAnimationType,
);
textWidgetList.add(animationWidget);
}
}
}

@override
Widget build(BuildContext context) {
///使用流布局将所有的字组合到一起
///形成段落
return Wrap(
///两个字之间的距离
spacing: widget.space,

///每行之间的距离
runSpacing: widget.runSpace,

///所有的字
children: textWidgetList,
);
}
}
Loading

0 comments on commit 5fd23a2

Please sign in to comment.