You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
6.7 KiB
188 lines
6.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows;
|
|
using System.Windows.Media.Animation;
|
|
|
|
namespace SJ.Controls.Helpers
|
|
{
|
|
public class StoryboardHelper
|
|
{
|
|
/// <summary>
|
|
/// 播放动画
|
|
/// </summary>
|
|
/// <param name="fe">控件源</param>
|
|
/// <param name="from">开始值</param>
|
|
/// <param name="to">结束值</param>
|
|
/// <param name="duration">时间间隔</param>
|
|
/// <param name="IsAutoReverse">是否反向播放</param>
|
|
/// <param name="RepeatPlay">是否重复播放</param>
|
|
/// <param name="ef">动画类型</param>
|
|
/// <param name="Callback">回调函数</param>
|
|
/// <param name="PropertyPath">动画属性</param>
|
|
public static void _PlayDoubleAnimation(FrameworkElement fe, double from, double to, TimeSpan duration, bool IsAutoReverse, bool RepeatPlay, IEasingFunction ef, Action Callback, string PropertyPath)
|
|
{
|
|
Storyboard _sb = new Storyboard();
|
|
_sb.Completed += new EventHandler((s, e) =>
|
|
{
|
|
if (Callback != null)
|
|
Callback();
|
|
_sb.Stop();
|
|
_sb.Children.Clear();
|
|
_sb = null;
|
|
});
|
|
DoubleAnimation daRotation = new DoubleAnimation();
|
|
daRotation.From = from;
|
|
daRotation.To = to;
|
|
daRotation.EasingFunction = ef;
|
|
daRotation.Duration = duration;
|
|
Storyboard.SetTargetProperty(daRotation, new PropertyPath(PropertyPath));
|
|
Storyboard.SetTarget(daRotation, fe);
|
|
_sb.Children.Add(daRotation);
|
|
_sb.AutoReverse = IsAutoReverse;
|
|
if (RepeatPlay)
|
|
_sb.RepeatBehavior = RepeatBehavior.Forever;
|
|
_sb.Begin();
|
|
}
|
|
|
|
public static void _PlayAnimationUsingKeyFrames(IList<AnimationModel> AnimationUsingKeyFrameList, bool IsAutoReverse, bool IsRepeayPlay, Action Callback)
|
|
{
|
|
if (AnimationUsingKeyFrameList == null || AnimationUsingKeyFrameList.Count == 0)
|
|
return;
|
|
Storyboard _sb = new Storyboard();
|
|
_sb.Completed += new EventHandler((s, e) =>
|
|
{
|
|
if (Callback != null)
|
|
Callback();
|
|
_sb.Stop();
|
|
_sb.Children.Clear();
|
|
_sb = null;
|
|
});
|
|
_sb.AutoReverse = IsAutoReverse;
|
|
if (IsRepeayPlay)
|
|
_sb.RepeatBehavior = RepeatBehavior.Forever;
|
|
foreach (AnimationModel am in AnimationUsingKeyFrameList)
|
|
{
|
|
AnimationTimeline animationTimeLine = null;
|
|
switch (am._KeyFrameType)
|
|
{
|
|
case KeyFrameType.DoubleKeyFrame:
|
|
animationTimeLine = CreateDoubleAnimationUsingKeyFrames(am);
|
|
break;
|
|
case KeyFrameType.ColorKeyFrame:
|
|
animationTimeLine = CreateColorAnimationUsingKeyFrames(am);
|
|
break;
|
|
case KeyFrameType.ObjectKeyFrame:
|
|
animationTimeLine = CreateObjectAnimationUsingKeyFrames(am);
|
|
break;
|
|
}
|
|
_sb.Children.Add(animationTimeLine);
|
|
}
|
|
_sb.Begin();
|
|
}
|
|
|
|
private static AnimationTimeline CreateDoubleAnimationUsingKeyFrames(AnimationModel am)
|
|
{
|
|
DoubleAnimationUsingKeyFrames animationTimeline = new DoubleAnimationUsingKeyFrames();
|
|
Storyboard.SetTargetProperty(animationTimeline, new PropertyPath(am.PropertyPath));
|
|
Storyboard.SetTarget(animationTimeline, am.Element);
|
|
foreach (BaseKeyFrame baseKeyFrame in am.KeyFrames)
|
|
{
|
|
animationTimeline.KeyFrames.Add(
|
|
new EasingDoubleKeyFrame(
|
|
Convert.ToInt32(baseKeyFrame.Value),
|
|
baseKeyFrame._KeyTime,
|
|
baseKeyFrame.EasingFunction)
|
|
);
|
|
}
|
|
return animationTimeline;
|
|
}
|
|
|
|
private static AnimationTimeline CreateColorAnimationUsingKeyFrames(AnimationModel am)
|
|
{
|
|
ColorAnimationUsingKeyFrames animationTimeline = new ColorAnimationUsingKeyFrames();
|
|
Storyboard.SetTargetProperty(animationTimeline, new PropertyPath(am.PropertyPath));
|
|
Storyboard.SetTarget(animationTimeline, am.Element);
|
|
foreach (BaseKeyFrame baseKeyFrame in am.KeyFrames)
|
|
{
|
|
animationTimeline.KeyFrames.Add(
|
|
new EasingColorKeyFrame(
|
|
(System.Windows.Media.Color)baseKeyFrame.Value,
|
|
baseKeyFrame._KeyTime,
|
|
baseKeyFrame.EasingFunction)
|
|
);
|
|
}
|
|
return animationTimeline;
|
|
}
|
|
|
|
private static AnimationTimeline CreateObjectAnimationUsingKeyFrames(AnimationModel am)
|
|
{
|
|
ObjectAnimationUsingKeyFrames animationTimeline = new ObjectAnimationUsingKeyFrames();
|
|
Storyboard.SetTargetProperty(animationTimeline, new PropertyPath(am.PropertyPath));
|
|
Storyboard.SetTarget(animationTimeline, am.Element);
|
|
foreach (BaseKeyFrame baseKeyFrame in am.KeyFrames)
|
|
{
|
|
animationTimeline.KeyFrames.Add(
|
|
new DiscreteObjectKeyFrame(
|
|
baseKeyFrame.Value,
|
|
baseKeyFrame._KeyTime)
|
|
);
|
|
}
|
|
return animationTimeline;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 关键帧动画类型
|
|
/// </summary>
|
|
public enum KeyFrameType
|
|
{
|
|
DoubleKeyFrame = 1,
|
|
ColorKeyFrame = 2,
|
|
ObjectKeyFrame = 3
|
|
}
|
|
|
|
public class AnimationModel
|
|
{
|
|
public AnimationModel()
|
|
{
|
|
this.KeyFrames = new List<BaseKeyFrame>();
|
|
}
|
|
/// <summary>
|
|
/// 执行动画的对象
|
|
/// </summary>
|
|
public FrameworkElement Element;
|
|
|
|
/// <summary>
|
|
/// 作用于动画的属性
|
|
/// </summary>
|
|
public string PropertyPath;
|
|
|
|
/// <summary>
|
|
/// 动画类型枚举
|
|
/// </summary>
|
|
public KeyFrameType _KeyFrameType;
|
|
|
|
/// <summary>
|
|
/// 关键帧动画帧集合
|
|
/// </summary>
|
|
public IList<BaseKeyFrame> KeyFrames;
|
|
}
|
|
|
|
public class BaseKeyFrame
|
|
{
|
|
/// <summary>
|
|
/// 动画触发时间
|
|
/// </summary>
|
|
public TimeSpan _KeyTime;
|
|
|
|
/// <summary>
|
|
/// 值
|
|
/// </summary>
|
|
public object Value;
|
|
|
|
/// <summary>
|
|
/// 缓动函数类型
|
|
/// </summary>
|
|
public IEasingFunction EasingFunction;
|
|
}
|
|
}
|
|
|