博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF动画公共类
阅读量:2430 次
发布时间:2019-05-10

本文共 12369 字,大约阅读时间需要 41 分钟。

很不错,省得自己写了。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows;using System.Windows.Controls;namespace CLeopardTestWPFAnimation{   public static class Animation_Helper    {        ///         /// 透明度播放—循环 (提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 });        ///         /// 透明动画对象        /// 动画时间        /// 动画移动所在窗口(推荐this)        /// 
返回动画对象
public static Storyboard Opacity_Animation(FrameworkElement myFrameworkElement, TimeSpan aTime, Window wind) { Storyboard myStoryboard = new Storyboard(); DoubleAnimation OpacityDoubleAnimation = new DoubleAnimation(); OpacityDoubleAnimation.From = 0; OpacityDoubleAnimation.To = 1; OpacityDoubleAnimation.Duration = aTime; Storyboard.SetTargetName(OpacityDoubleAnimation, myFrameworkElement.Name); Storyboard.SetTargetProperty(OpacityDoubleAnimation, new PropertyPath(DataGrid.OpacityProperty)); myStoryboard.Children.Add(OpacityDoubleAnimation); myStoryboard.RepeatBehavior = RepeatBehavior.Forever; myStoryboard.Begin(wind); return myStoryboard; } /// /// 透明度播放-可传值(起始透明度,结束透明度)(提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 透明动画对象 /// 动画时间 /// 起始透明度 /// 结束透明度 /// 动画移动所在窗口(推荐this) ///
返回动画对象
public static Storyboard Opacity_Animation(FrameworkElement myFrameworkElement, TimeSpan aTime, double dFrom, double dTo, Window wind) { Storyboard myStoryboard = new Storyboard(); DoubleAnimation OpacityDoubleAnimation = new DoubleAnimation(); OpacityDoubleAnimation.From = dFrom; OpacityDoubleAnimation.To = dTo; OpacityDoubleAnimation.Duration = aTime; Storyboard.SetTargetName(OpacityDoubleAnimation, myFrameworkElement.Name); Storyboard.SetTargetProperty(OpacityDoubleAnimation, new PropertyPath(DataGrid.OpacityProperty)); myStoryboard.Children.Add(OpacityDoubleAnimation); //myStoryboard.RepeatBehavior = RepeatBehavior.Forever; myStoryboard.Begin(wind); return myStoryboard; } /// /// 透明度播放-可传值(起始透明度,结束透明度,次数或是否循环)(提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 透明动画对象 /// 动画时间 /// 起始透明度 /// 结束透明度 /// 动画是否循环 /// 动画移动所在窗口(推荐this) ///
返回动画对象
public static Storyboard Opacity_Animation(FrameworkElement myFrameworkElement, TimeSpan aTime, double dFrom, double dTo, RepeatBehavior RBehavior, Window wind) { Storyboard myStoryboard = new Storyboard(); DoubleAnimation OpacityDoubleAnimation = new DoubleAnimation(); OpacityDoubleAnimation.From = dFrom; OpacityDoubleAnimation.To = dTo; OpacityDoubleAnimation.Duration = aTime; Storyboard.SetTargetName(OpacityDoubleAnimation, myFrameworkElement.Name); Storyboard.SetTargetProperty(OpacityDoubleAnimation, new PropertyPath(DataGrid.OpacityProperty)); myStoryboard.Children.Add(OpacityDoubleAnimation); myStoryboard.RepeatBehavior = RBehavior; myStoryboard.Begin(wind); return myStoryboard; } /// /// X方向移动动画(需要移动的对象、移动时间、相对控件原始位置的起始位置、相对控件原始位置的结束位置)(提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 需要移动的对象 /// 移动时间 /// 相对控件原始位置的-起始位置 /// 相对控件原始位置的-结束位置 /// 动画移动所在窗口(推荐this) ///
返回动画对象
public static Storyboard InDouble_X_Animation(FrameworkElement myFrameworkElement, TimeSpan aTime, double dFrom, double dTo, RepeatBehavior RForever, Window wind) { myFrameworkElement.RenderTransform = new TranslateTransform(); DependencyProperty[] propertyChain = new DependencyProperty[] { DataGrid.RenderTransformProperty, TranslateTransform.XProperty }; Storyboard myStoryboard = new Storyboard(); DoubleAnimation InDoubleAnimation = new DoubleAnimation(); InDoubleAnimation.From = dFrom; InDoubleAnimation.To = dTo; InDoubleAnimation.Duration = aTime; Storyboard.SetTargetName(InDoubleAnimation, myFrameworkElement.Name); Storyboard.SetTargetProperty(InDoubleAnimation, new PropertyPath("(0).(1)", propertyChain)); myStoryboard.Children.Add(InDoubleAnimation); myStoryboard.RepeatBehavior = RForever; myStoryboard.Begin(wind); return myStoryboard; } /// /// Y方向移动动画(需要移动的对象、移动时间、相对控件原始位置的起始位置、相对控件原始位置的结束位置)(提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 需要移动的对象 /// 移动时间 /// 相对控件原始位置的-起始位置 /// 相对控件原始位置的-结束位置 /// 动画是否循环 /// 动画移动所在窗口(推荐this) ///
返回动画对象
public static Storyboard InDouble_Y_Animation(FrameworkElement myFrameworkElement, TimeSpan aTime, double dFrom, double dTo, RepeatBehavior RForever, Window wind) { myFrameworkElement.RenderTransform = new TranslateTransform(); DependencyProperty[] propertyChain = new DependencyProperty[] { DataGrid.RenderTransformProperty, TranslateTransform.YProperty }; Storyboard myStoryboard = new Storyboard(); DoubleAnimation InDoubleAnimation = new DoubleAnimation(); InDoubleAnimation.From = dFrom; InDoubleAnimation.To = dTo; InDoubleAnimation.Duration = aTime; Storyboard.SetTargetName(InDoubleAnimation, myFrameworkElement.Name); Storyboard.SetTargetProperty(InDoubleAnimation, new PropertyPath("(0).(1)", propertyChain)); myStoryboard.Children.Add(InDoubleAnimation); myStoryboard.RepeatBehavior = RForever; myStoryboard.Begin(wind); return myStoryboard; } /// /// 缓动缩放动画 (提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 缩放控件 /// 缩放时间 /// 缩放起始值(推荐1) /// 缩放结束值(推荐1.5) /// 滑过动画目标的次数(推荐5) /// 弹簧刚度(推荐10) ///
返回动画对象
public static AnimationClock ScaleEasingAnimation(FrameworkElement element, TimeSpan aTime, double dFrom, double dTo, int aOscillations, int aSpringiness) { ScaleTransform scale = new ScaleTransform(); element.RenderTransform = scale; element.RenderTransformOrigin = new Point(0.5, 0.5);//定义圆心位置 EasingFunctionBase easing = new ElasticEase() { EasingMode = EasingMode.EaseOut, //公式 Oscillations = aOscillations, //滑过动画目标的次数 Springiness = aSpringiness //弹簧刚度 }; DoubleAnimation scaleAnimation = new DoubleAnimation() { From = dFrom, //起始值 To = dTo, //结束值 EasingFunction = easing, //缓动函数 Duration = aTime //动画播放时间 }; AnimationClock clock = scaleAnimation.CreateClock(); scale.ApplyAnimationClock(ScaleTransform.ScaleXProperty, clock); scale.ApplyAnimationClock(ScaleTransform.ScaleYProperty, clock); return clock; } /// /// 缩放匀速动画 (提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 缩放控件 /// 缩放X开始值 /// 缩放X结束值 /// 缩放Y开始值 /// 缩放Y结束值 /// 动画持续时间 /// 动画是否循环 /// 动画移动所在窗口(推荐this) ///
返回动画对象
public static Storyboard ScaleTransformAnimation(UIElement sender, double scaleXF, double scaleXT, double scaleYF, double scaleYT, TimeSpan aTime, RepeatBehavior RForever, Window wind) { DoubleAnimationUsingKeyFrames dba_SacleX = new DoubleAnimationUsingKeyFrames(); dba_SacleX.KeyFrames.Add(new EasingDoubleKeyFrame(scaleXF, TimeSpan.FromSeconds(0))); dba_SacleX.KeyFrames.Add(new EasingDoubleKeyFrame(scaleXT, aTime)); DoubleAnimationUsingKeyFrames dba_SacleY = new DoubleAnimationUsingKeyFrames(); dba_SacleY.KeyFrames.Add(new EasingDoubleKeyFrame(scaleYF, TimeSpan.FromSeconds(0))); dba_SacleY.KeyFrames.Add(new EasingDoubleKeyFrame(scaleYT, aTime)); Storyboard mystoryboard = new Storyboard(); Storyboard.SetTarget(dba_SacleX, sender); sender.RenderTransform = new ScaleTransform(); Storyboard.SetTargetProperty(dba_SacleX, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleX)")); mystoryboard.Children.Add(dba_SacleX); Storyboard.SetTarget(dba_SacleY, sender); Storyboard.SetTargetProperty(dba_SacleY, new PropertyPath("(UIElement.RenderTransform).(ScaleTransform.ScaleY)")); mystoryboard.Children.Add(dba_SacleY); mystoryboard.RepeatBehavior = RForever; mystoryboard.Begin(wind); return mystoryboard; } /// /// 旋转动画(初始点X和Y是按元素的左上角)(提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 动画对象 /// 旋转X原点 /// 旋转Y原点 /// 旋转角度(推荐0-360) /// 动画时间 /// 动画是否循环 /// 动画移动所在窗口(推荐this) ///
返回动画对象
public static Storyboard RotateTransformAnimation(UIElement sender, TimeSpan aTime, double centerX, double centerY, double angle, RepeatBehavior RForever, Window wind) { RotateTransform rt = new RotateTransform(); rt.CenterX = centerX; rt.CenterY = centerY; sender.RenderTransform = rt; DoubleAnimationUsingKeyFrames dba = new DoubleAnimationUsingKeyFrames(); dba.KeyFrames.Add(new LinearDoubleKeyFrame(angle, aTime)); Storyboard mysb = new Storyboard(); Storyboard.SetTarget(dba, sender); Storyboard.SetTargetProperty(dba, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)")); mysb.Children.Add(dba); mysb.RepeatBehavior = RForever; mysb.Begin(wind); return mysb; } /// /// 旋转动画(初始点按元素的中心)(提升效率)Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = 20 }); /// /// 动画对象 /// 旋转角度(推荐0-360) /// 动画时间 /// 动画是否循环 /// 动画移动所在窗口(推荐this) ///
返回动画对象
public static Storyboard RotateTransformAnimation(UIElement element, TimeSpan aTime, double angle, RepeatBehavior RForever, Window wind) { RotateTransform scale = new RotateTransform(); element.RenderTransform = scale; element.RenderTransformOrigin = new Point(0.5, 0.5);//定义圆心位置 //RotateTransform rt = new RotateTransform(); //rt.CenterX = 0; //rt.CenterY = 0; //element.RenderTransform = rt; DoubleAnimationUsingKeyFrames dba = new DoubleAnimationUsingKeyFrames(); dba.KeyFrames.Add(new LinearDoubleKeyFrame(angle, aTime)); Storyboard mysb = new Storyboard(); Storyboard.SetTarget(dba, element); Storyboard.SetTargetProperty(dba, new PropertyPath("(UIElement.RenderTransform).(RotateTransform.Angle)")); mysb.Children.Add(dba); mysb.RepeatBehavior = RForever; mysb.Begin(wind); return mysb; } }}

转载地址:http://itnmb.baihongyu.com/

你可能感兴趣的文章
太难了!开源项目如何商业化?
查看>>
谷歌排名第一的编程语言,死磕它这两点,小白也能学的会!不信你看!
查看>>
程序员掉头发的原因找到了 | 每日趣闻
查看>>
腾讯:我就是那只吃了假辣椒酱的憨憨。老干妈:企鹅你可长点心吧!
查看>>
倒计时1天 | 张钹院士领衔,AI开发者大会20大论坛全攻略!
查看>>
运维工程师的日常?? | 每日趣闻
查看>>
31 道 Java 核心面试题,统统打包给你!
查看>>
太拼了:谷歌第一编程语言小白也能学会!
查看>>
三分钟黑了阿里?马云下死命令留他?吴翰清辟谣:我没黑过阿里
查看>>
如果重新一次高考,你还会选择软件专业当程序员吗? | 每日趣闻
查看>>
如何设计一个安全可靠的 API 接口?
查看>>
一年一度程序员“补课”季来袭,618 背后技术大公开!
查看>>
我和美国 AI 博士聊了聊:2020 年,这件事比存钱更重要!
查看>>
陈芳,高考之后我要学计算机专业,将来做 IT 发财了,我就娶你!
查看>>
“编程能力差的程序员,90%输在这事上!”谷歌AI专家:都是瞎努力!
查看>>
张一鸣做电商:再造一个“抖音”
查看>>
“你写的 Bug 让我来改好吗” | 每日趣闻
查看>>
大厂技术文档:Redis+Nginx+Spring全家桶+Dubbo精选
查看>>
笑死,别再黑程序员了好吗? | 每日趣闻
查看>>
Python 爬取 13966 条运维招聘信息,这些岗位最吃香
查看>>