csharp WPF控件动画扩展类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp WPF控件动画扩展类相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media;
namespace Wind.Windows.Animation
{
public static class AnimationExtensions
{
#region 动画对象
public class AnimationContent
{
public Storyboard Story { get; set; }
public List<FrameworkElement> Targets { get; set; }
public int StartIndex { get; set; }
public int EndIndex { get; set; }
public AnimationContent()
{
Story = new Storyboard();
Targets = new List<FrameworkElement>();
}
}
#endregion
/// <summary>
/// 检查TransformGroup
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
private static FrameworkElement CheckTransformGroup(FrameworkElement target)
{
if (!(target.RenderTransform is TransformGroup))
{
TransformGroup tgroup = new TransformGroup();
tgroup.Children.Add(new ScaleTransform());
tgroup.Children.Add(new SkewTransform());
tgroup.Children.Add(new RotateTransform());
tgroup.Children.Add(new TranslateTransform());
target.RenderTransform = tgroup;
}
return target;
}
/// <summary>
/// 构建一个AnimationContent对象
/// </summary>
/// <param name="target"></param>
/// <returns></returns>
public static AnimationContent Animation(this FrameworkElement target)
{
AnimationContent result = new AnimationContent();
target = CheckTransformGroup(target);
result.Targets.Add(target);
return result;
}
/// <summary>
/// 添加动画对象
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="newelements">需要添加的FrameworkElement对象</param>
public static void AddTarget(this AnimationContent target, params FrameworkElement[] newelements)
{
foreach (FrameworkElement fe in newelements)
{
target.Targets.Add(CheckTransformGroup(fe));
}
}
/// <summary>
/// 移除动画对象
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="removeelements">需要移出的FrameworkElement对象</param>
public static void RemoveTarget(this AnimationContent target, params FrameworkElement[] removeelements)
{
foreach (FrameworkElement fe in removeelements)
{
target.Targets.Remove(fe);
}
}
/// <summary>
/// 启动动画
/// </summary>
/// <param name="target">AnimationContent对象</param>
public static void Begin(this AnimationContent target)
{
target.Story.Begin();
}
/// <summary>
/// 停止动画
/// </summary>
/// <param name="target">AnimationContent对象</param>
public static void Stop(this AnimationContent target)
{
target.Story.Stop();
}
/// <summary>
/// 设置中心点
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="x1">中心点X</param>
/// <param name="x2">中心点Y</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent Origin(this AnimationContent target, double x1, double x2)
{
foreach (FrameworkElement element in target.Targets)
{
element.RenderTransformOrigin = new Point(x1, x2);
}
return target;
}
/// <summary>
/// 设置中心点
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="index">对象组中的第几个</param>
/// <param name="x1">中心点X</param>
/// <param name="x2">中心点Y</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent Origin(this AnimationContent target, int index, double x1, double x2)
{
target.Targets[index].RenderTransformOrigin = new Point(x1, x2);
return target;
}
/// <summary>
/// 透明度
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent Opacity(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Opacity)", args);
}
/// <summary>
/// 缩放X
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent ScaleX(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)", args);
}
/// <summary>
/// 缩放Y
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent ScaleY(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)", args);
}
/// <summary>
/// 扭曲X
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent SkewX(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleX)", args);
}
/// <summary>
/// 扭曲Y
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent SkewY(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.RenderTransform).(TransformGroup.Children)[1].(SkewTransform.AngleY)", args);
}
/// <summary>
/// 旋转
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent Rotate(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.RenderTransform).(TransformGroup.Children)[2].(RotateTransform.Angle)", args);
}
/// <summary>
/// 平移X
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent TranslationX(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)", args);
}
/// <summary>
/// 平移Y
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent TranslationY(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)", args);
}
/// <summary>
/// 平面旋转X
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneRotateX(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.RotationX)", args);
}
/// <summary>
/// 平面旋转Y
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneRotateY(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.RotationY)", args);
}
/// <summary>
/// 平面偏移Z(屏幕)
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneGlobalOffsetZ(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.GlobalOffsetZ)", args);
}
/// <summary>
/// 平面偏移X(屏幕)
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneGlobalOffsetX(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.GlobalOffsetX)", args);
}
/// <summary>
/// 平面偏移Y(屏幕)
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneGlobalOffsetY(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.GlobalOffsetY)", args);
}
/// <summary>
/// 平面偏移Z(对象)
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneLocalOffsetZ(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.LocalOffsetZ)", args);
}
/// <summary>
/// 平面偏移X(对象)
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneLocalOffsetX(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.LocalOffsetX)", args);
}
/// <summary>
/// 平面偏移Y(对象)
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent PlaneLocalOffsetY(this AnimationContent target, params double[] args)
{
return SplineDoubleProperty(target, "(UIElement.Projection).(PlaneProjection.LocalOffsetY)", args);
}
/// <summary>
/// 设置前一个动画Splines
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="x1">第一个Spline X坐标</param>
/// <param name="y1">第二个Spline Y坐标</param>
/// <param name="x2">第一个Spline X坐标</param>
/// <param name="y2">第二个Spline Y坐标</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent Splines(this AnimationContent target, double x1, double y1, double x2, double y2)
{
for (int num = target.StartIndex; num < target.EndIndex; num++)
{
foreach (SplineDoubleKeyFrame sdk in (target.Story.Children[num] as DoubleAnimationUsingKeyFrames).KeyFrames)
{
KeySpline ks = new KeySpline();
ks.ControlPoint1 = new Point(x1, y1);
ks.ControlPoint2 = new Point(x2, y2);
sdk.KeySpline = ks;
}
}
return target;
}
/// <summary>
/// 设置前一个动画Splines
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="index">KeyFrames的位置</param>
/// <param name="x1">第一个Spline X坐标</param>
/// <param name="y1">第二个Spline Y坐标</param>
/// <param name="x2">第一个Spline X坐标</param>
/// <param name="y2">第二个Spline Y坐标</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent Splines(this AnimationContent target, int index, double x1, double y1, double x2, double y2)
{
for (int i = target.StartIndex; i < target.EndIndex; i++)
{
KeySpline ks = new KeySpline();
ks.ControlPoint1 = new Point(x1, y1);
ks.ControlPoint2 = new Point(x2, y2);
((target.Story.Children[i] as DoubleAnimationUsingKeyFrames).KeyFrames[index] as SplineDoubleKeyFrame).KeySpline = ks;
}
return target;
}
/// <summary>
/// 根据指定属性创建动画
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="propertyname">属性名称</param>
/// <param name="args">动画参数(参数奇数位是KeyTime,偶数位是Value)</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent Property(this AnimationContent target, string propertyname, params double[] args)
{
return SplineDoubleProperty(target, propertyname, args);
}
/// <summary>
/// 插入DoubleKeyFrame
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="value">DoubleKeyFrame对象</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent InsertDoubleKeyFrame(AnimationContent target, DoubleKeyFrame value)
{
return InsertDoubleKeyFrame(target, 0, value);
}
/// <summary>
/// 插入DoubleKeyFrame
/// </summary>
/// <param name="target">AnimationContent对象</param>
/// <param name="index">指定位置</param>
/// <param name="value">DoubleKeyFrame对象</param>
/// <returns>AnimationContent对象</returns>
public static AnimationContent InsertDoubleKeyFrame(AnimationContent target, int index, DoubleKeyFrame value)
{
for (int i = target.StartIndex; i < target.EndIndex; i++)
{
(target.Story.Children[i] as DoubleAnimationUsingKeyFrames).KeyFrames.Insert(index, value);
}
return target;
}
private static AnimationContent SplineDoubleProperty(AnimationContent target, string propertyPath, params double[] args)
{
if (args.Length % 2 != 0)
throw new Exception("参数不正确");
foreach (FrameworkElement element in target.Targets)
{
DoubleAnimationUsingKeyFrames keyframes = new DoubleAnimationUsingKeyFrames();
for (int i = 0; i < args.Length; i += 2)
{
SplineDoubleKeyFrame splinekey = new SplineDoubleKeyFrame();
splinekey.KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromSeconds(args[i]));
splinekey.Value = args[i + 1];
keyframes.KeyFrames.Add(splinekey);
}
Storyboard.SetTargetProperty(keyframes, new PropertyPath(propertyPath, new object[0]));
Storyboard.SetTarget(keyframes, element);
target.Story.Children.Add(keyframes);
}
target.StartIndex = target.EndIndex;
target.EndIndex += target.Targets.Count;
return target;
}
}
}
以上是关于csharp WPF控件动画扩展类的主要内容,如果未能解决你的问题,请参考以下文章