WPF中自定義GridLengthAnimation
需求
我們想在編輯一個(gè)列表中某一個(gè)條目時(shí),將編輯的詳情內(nèi)容也放置當(dāng)前面,比如右側(cè)。
可以通過(guò)將一個(gè)Grid,分成兩個(gè)Cloumn,動(dòng)態(tài)調(diào)整兩個(gè)Cloumn的Width,就可以實(shí)現(xiàn)這個(gè)需求。
我們知道,Clomun的Width是個(gè),而默認(rèn)的動(dòng)畫沒(méi)有這樣子的。我們就需要自己實(shí)現(xiàn)這樣一人動(dòng)畫。
設(shè)計(jì)
我們從Animation的類圖上看到
我們可以從需求
我們想在編輯一個(gè)列表中某一個(gè)條目時(shí),將編輯的詳情內(nèi)容也放置當(dāng)前面,比如右側(cè)。
可以通過(guò)將一個(gè)Grid,分成兩個(gè)Cloumn,動(dòng)態(tài)調(diào)整兩個(gè)Cloumn的Width,就可以實(shí)現(xiàn)這個(gè)需求。
我們知道,Clomun的Width是個(gè)GridLength,而默認(rèn)的動(dòng)畫沒(méi)有這樣子的。我們就需要自己實(shí)現(xiàn)這樣一人動(dòng)畫。
設(shè)計(jì)
我們從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); }
如上所示,我們仿著默認(rèn)動(dòng)畫實(shí)現(xiàn)了From,To,同時(shí)將其屬性定義為GridLength,當(dāng)動(dòng)畫執(zhí)行時(shí),我們重寫了GetCurrentValue,使其根據(jù)From/To屬性相關(guān)聯(lián)。
優(yōu)化
通過(guò)以上代碼,我們實(shí)現(xiàn)了在GridLength變化時(shí),實(shí)現(xiàn)動(dòng)畫。但是,試用后我們發(fā)現(xiàn),動(dòng)畫,有點(diǎn)太線性。這個(gè)時(shí)候,怎么辦?
可以通過(guò)引入EasingFunction來(lái)實(shí)現(xiàn)。我們知道EasingFunction其實(shí)就是一個(gè)與時(shí)間t有關(guān)的時(shí)間函數(shù)f(t).通過(guò)時(shí)間函數(shù)的處理,我們使動(dòng)畫過(guò)渡不要那么線性。
/// <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));
對(duì)應(yīng)的,還要重寫GetCurrentValue函數(shù)。
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"/>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
ASP.NET MVC中設(shè)置跨域訪問(wèn)問(wèn)題
這篇文章主要介紹了ASP.NET MVC中設(shè)置跨域訪問(wèn)問(wèn)題,需要的朋友可以參考下2018-06-06VS2019中.NET如何實(shí)現(xiàn)打日志功能
本文主要介紹了VS2019中.NET如何實(shí)現(xiàn)打日志功能,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03異步調(diào)用webservice返回responseXML為空的問(wèn)題解決方法
異步調(diào)用webservice返回responseXML為空,詳細(xì)很多朋友都遇到過(guò)類似的問(wèn)題吧,接下來(lái)為大家提供詳細(xì)的解決方案,感興趣的朋友可以參考下哈2013-04-04.Net Core WebApi部署到Windows服務(wù)器上的步驟
這篇文章主要介紹了.Net Core WebApi部署到Windows服務(wù)器上的步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03.NET 8 中如何利用 MediatR 實(shí)現(xiàn)高效消息傳遞
這篇文章主要介紹了.NET 8 中利用 MediatR 實(shí)現(xiàn)高效消息傳遞,示例不僅說(shuō)明了如何使用MediatR?來(lái)處理通知,還說(shuō)明了如何實(shí)現(xiàn)通知處理模式,需要的朋友可以參考下2024-08-08.Net?7.0實(shí)現(xiàn)支付寶退款和結(jié)果查詢接口
支付寶對(duì) .Net 的支持還是比較充分的,在每個(gè)接口文檔中都有關(guān)于 C# 語(yǔ)言的示例,這樣就大大降低了對(duì)接的難度,很容易上手,這篇文章主要介紹了支付寶退款和結(jié)果查詢接口簡(jiǎn)單實(shí)現(xiàn)(.Net?7.0),需要的朋友可以參考下2024-07-07Request.QueryString與一般NameValueCollection的區(qū)別
最近在做一個(gè)搜索程序的優(yōu)化改進(jìn),將搜索結(jié)果按照查詢的參數(shù)不同進(jìn)行緩存。緩存的Key很自然的就想到了用查詢字符串,而獲取查詢字符串的最簡(jiǎn)單方式是通過(guò)Request.QueryString.ToString()方法2011-12-12Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示功能
這篇文章主要介紹了Asp.net MVC 中利用jquery datatables 實(shí)現(xiàn)數(shù)據(jù)分頁(yè)顯示功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-06-06ASP.NET Core使用JWT認(rèn)證授權(quán)的方法
這篇文章主要介紹了ASP.NET Core使用JWT認(rèn)證授權(quán)的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11