WPF中自定义GridLengthAnimation
时间:2019-03-30
本文章向大家介绍WPF中自定义GridLengthAnimation,主要包括WPF中自定义GridLengthAnimation使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
需求
我们想在编辑一个列表中某一个条目时,将编辑的详情内容也放置当前面,比如右侧。
可以通过将一个Grid,分成两个Cloumn,动态调整两个Cloumn的Width,就可以实现这个需求。
我们知道,Clomun的Width是个,而默认的动画没有这样子的。我们就需要自己实现这样一人动画。
设计
我们从Animation的类图上看到
我们可以从需求
我们想在编辑一个列表中某一个条目时,将编辑的详情内容也放置当前面,比如右侧。
可以通过将一个Grid,分成两个Cloumn,动态调整两个Cloumn的Width,就可以实现这个需求。
我们知道,Clomun的Width是个GridLength,而默认的动画没有这样子的。我们就需要自己实现这样一人动画。
设计
我们从Animation的类图上看到AnimationTimeline继承,重写其GetCurrentValue
public class GridLengthAnimation : AnimationTimeline { /// <summary> /// Returns the type of object to animate /// </summary> public override Type TargetPropertyType => typeof(GridLength); /// <summary> /// Creates an instance of the animation object /// </summary> /// <returns>Returns the instance of the GridLengthAnimation</returns> protected override System.Windows.Freezable CreateInstanceCore() { return new GridLengthAnimation(); } /// <summary> /// Dependency property for the From property /// </summary> public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation)); /// <summary> /// CLR Wrapper for the From depenendency property /// </summary> public GridLength From { get { return (GridLength)GetValue(GridLengthAnimation.FromProperty); } set { SetValue(GridLengthAnimation.FromProperty, value); } } /// <summary> /// Dependency property for the To property /// </summary> public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation)); /// <summary> /// CLR Wrapper for the To property /// </summary> public GridLength To { get { return (GridLength)GetValue(GridLengthAnimation.ToProperty); } set { SetValue(GridLengthAnimation.ToProperty, value); } } /// <summary> /// Animates the grid let set /// </summary> /// <param name="defaultOriginValue">The original value to animate</param> /// <param name="defaultDestinationValue">The final value</param> /// <param name="animationClock">The animation clock (timer)</param> /// <returns>Returns the new grid length to set</returns> public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value; double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value; if (fromVal > toVal) return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Star); else return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Star); }
如上所示,我们仿着默认动画实现了From,To,同时将其属性定义为GridLength,当动画执行时,我们重写了GetCurrentValue,使其根据From/To属性相关联。
优化
通过以上代码,我们实现了在GridLength变化时,实现动画。但是,试用后我们发现,动画,有点太线性。这个时候,怎么办?
可以通过引入EasingFunction来实现。我们知道EasingFunction其实就是一个与时间t有关的时间函数f(t).通过时间函数的处理,我们使动画过渡不要那么线性。
/// <summary> /// The <see cref="EasingFunction" /> dependency property's name. /// </summary> public const string EasingFunctionPropertyName = "EasingFunction"; /// <summary> /// Gets or sets the value of the <see cref="EasingFunction" /> /// property. This is a dependency property. /// </summary> public IEasingFunction EasingFunction { get { return (IEasingFunction)GetValue(EasingFunctionProperty); } set { SetValue(EasingFunctionProperty, value); } } /// <summary> /// Identifies the <see cref="EasingFunction" /> dependency property. /// </summary> public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register( EasingFunctionPropertyName, typeof(IEasingFunction), typeof(GridLengthAnimation), new UIPropertyMetadata(null));
对应的,还要重写GetCurrentValue函数。
public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock) { double fromVal = ((GridLength)GetValue(FromProperty)).Value; double toVal = ((GridLength)GetValue(ToProperty)).Value; //check that from was set from the caller //if (fromVal == 1) // //set the from as the actual value // fromVal = ((GridLength)defaultDestinationValue).Value; double progress = animationClock.CurrentProgress.Value; IEasingFunction easingFunction = EasingFunction; if (easingFunction != null) { progress = easingFunction.Ease(progress); } if (fromVal > toVal) return new GridLength((1 - progress) * (fromVal - toVal) + toVal, GridUnitType.Star); return new GridLength(progress * (toVal - fromVal) + fromVal, GridUnitType.Star); }
使用
<anim:GridLengthAnimation Storyboard.TargetProperty="Width" From="0" To="*" Duration="0:0:0.5"/>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 因为喜欢所以升级,MyStaging-3.0 继续
- 环境与工具篇:建立高效的macos环境
- 0202年你还不知道面向对象?
- 十问泛型,你能扛住吗?
- 【两万字】面试官:听说你精通集合源码,接我二十个问题!
- 手把手教你搭建一个技术人的博客
- 发布更新|腾讯云 Serverless 产品动态 20200723
- Spring中异步注解@Async的使用、原理及使用时可能导致的问题
- Elasticsearch 聚合数据结果不精确,怎么破?
- Elasticsearch 预处理没有奇技淫巧,请先用好这一招!
- 时滞微分方程的matlab解法
- Zabbix Monitoring Logstash
- Join()和 List()的 6 种化学反应
- lettuce和hbase中对netty的使用你都了解吗?
- SAS-新规试行下诞生的程序