文章目录
- 1.类库说明
- 2.使用步骤
- 2.1 创建一个Windows Form 项目
- 2.2 安装类库
- 2.3 编码
- 2.4 效果
- 3.该库支持的缓动函数
- 4.代码下载
1.类库说明
App.Animations 类库是一个很精炼、好用的 csharp easing 动画库
- 基于 net-standard 2.0
- 提供 Fluent API,写代码非常舒服。
- 支持多个参数同时参与动画。
- 自带了十几种动画效果。
- 代码非常精炼,是学习线程控制的好示例项目。
2.使用步骤
2.1 创建一个Windows Form 项目
App.Animations 类库是基于 net-standard 2.0开发的,不依赖于其它任何类库,是可以跨平台使用的。这里以windows form项目为例,演示ui动画。窗口创建好后,在界面上拖入控件,效果如下:
2.2 安装类库
nuget-install App.Animations
2.3 编码
···csharp
using App.Animations;
using App.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AnimationForm
{
public partial class Form1 : Form
{
Animator _ani;
//-------------------------------------------------------
// Init
//-------------------------------------------------------
public Form1()
{
InitializeComponent();
BindEnum(this.cmbType1, typeof(AnimationType));
BindEnum(this.cmbType2, typeof(AnimationType));
}
//-------------------------------------------------------
// Combobox <-> Enum
//-------------------------------------------------------
void BindEnum(ComboBox cmb, Type enumType)
{
var infos = enumType.GetEnumInfos();
cmb.Items.Clear();
foreach (var info in infos)
{
cmb.Items.Add(info);
}
cmb.SelectedIndex = 0;
}
T GetEnum<T>(ComboBox cmb) where T : Enum
{
return (T)(cmb.SelectedItem as EnumInfo).Value;
}
//-------------------------------------------------------
// Run
//-------------------------------------------------------
private void btnStop_Click(object sender, EventArgs e)
{
if (_ani != null)
_ani.Stop();
}
// Animation on x
private void btnAnimX_Click(object sender, EventArgs e)
{
var start = 100; // x
var end = 300;
var type1 = GetEnum<AnimationType>(cmbType1);
var type2 = GetEnum<AnimationType>(cmbType2);
var dur1 = (long)numDur1.Value;
var dur2 = (long)numDur2.Value;
_ani = new Animator()
.SetInterval((int)numInterval.Value)
.AddPath(type1, start, end, dur1)
.AddPath(type2, end, start, dur2)
.SetFrameEvent((values) =>
{
Action action = () => {
label1.Left = (int)values[0];
label1.Text = string.Format("{0:000}", values[0]);
};
this.Invoke(action);
})
.SetEndEvent((_) => Trace.WriteLine("Animation end."))
.Start()
;
}
// Animation on y
private void btnAnimXY_Click(object sender, EventArgs e)
{
var start = new List<double> { 100, 10 }; // x, y
var end = new List<double> { 300, 100 };
var type1 = GetEnum<AnimationType>(cmbType1);
var type2 = GetEnum<AnimationType>(cmbType2);
var dur1 = (long)numDur1.Value;
var dur2 = (long)numDur2.Value;
_ani = new Animator()
.SetInterval((int)numInterval.Value)
.AddPath(type1, start, end, dur1)
.AddPath(type2, end, start, dur2)
.SetFrameEvent((values) =>
{
Action action = () => {
label1.Left = (int)values[0];
label1.Top = (int)values[1];
label1.Text = string.Format("({0:000},{1:000})", values[0], values[1]);
};
this.Invoke(action);
})
.SetEndEvent((values) => Trace.WriteLine("Animaion end."))
.Start();
}
// Animation on color
private void btnAnimColor_Click(object sender, EventArgs e)
{
var start = new List<double> { 255, 0, 0 }; // r, g, b
var end = new List<double> { 0, 255, 255 };
var type1 = GetEnum<AnimationType>(cmbType1);
var type2 = GetEnum<AnimationType>(cmbType2);
var dur1 = (long)numDur1.Value;
var dur2 = (long)numDur2.Value;
_ani = new Animator()
.SetInterval((int)numInterval.Value)
.AddPath(type1, start, end, dur1)
.AddPath(type2, end, start, dur2)
.SetFrameEvent((values) =>
{
Action action = () => {
label1.ForeColor = Color.FromArgb((int)values[0], (int)values[1], (int)values[2]);
label1.Text = string.Format("({0:0},{1:0},{2:0})", values[0], values[1], values[2]);
};
this.Invoke(action);
})
.SetEndEvent((values)=>
{
Action action = () => {
label1.ForeColor = Color.FromArgb((int)values[0], (int)values[1], (int)values[2]);
label1.Text = string.Format("({0:0},{1:0},{2:0})", values[0], values[1], values[2]);
};
this.Invoke(action);
})
.Start()
;
}
}
}
···
2.4 效果
3.该库支持的缓动函数
名称 | 说明 |
---|---|
Linear | 线性 |
BackEaseIn | 拉后 ease in |
BackEaseOut | 拉后 ease out |
BackEaseInOut | 拉后 ease in and ease out |
BounceEaseIn | 弹性 ease in |
BounceEaseOut | 弹性 ease out |
BounceEaseInOut | 弹性 ease in and ease out |
ElasticEaseIn | 橡皮筋 ease in |
ElasticEaseOut | 橡皮筋 ease out |
ElasticEaseInOut | 橡皮筋 ease in and ease out |
QuadraticEaseIn | 平方 ease in |
QuadraticEaseOut | 平方 ease out |
QuadraticEaseInOut | 平方 ease in and ease out |
CubicEaseIn | 立方 ease in |
CubicEaseInOut | 立方 ease in and cubic ease out |
CubicEaseOut | 立方 ease out |
QuarticEaseIn | 四次方 ease in |
QuarticEaseOut | 四次方 ease out |
QuarticEaseInOut | 四次方 ease in and ease outut |
QuinticEaseIn | 五次方 ease in |
QuinticEaseOut | 五次方 ease out |
QuinticEaseInOut | 五次方 ease in and ease out |
ExponentialEaseIn | 指数 ease in |
ExponentialEaseOut | 指数 ease out |
ExponentialEaseInOut | 指数 ease in and ease out |
SinusoidalEaseIn | 正弦曲线 ease in |
SinusoidalEaseOut | 正弦曲线 ease out |
SinusoidalEaseInOut | 正弦曲线 ease in and ease out |
CircularEaseIn | 圆形 ease in |
CircularEaseOut | 圆形 ease out |
CircularEaseInOut | 圆形 ease in and ease out |
4.代码下载
CSDN下载