如何使用WPF中自定義GridLengthAnimation?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作,成都做網(wǎng)站公司-創(chuàng)新互聯(lián)已向上千家企業(yè)提供了,網(wǎng)站設(shè)計(jì),網(wǎng)站制作,網(wǎng)絡(luò)營銷等服務(wù)!設(shè)計(jì)與技術(shù)結(jié)合,多年網(wǎng)站推廣經(jīng)驗(yàn),合理的價(jià)格為您打造企業(yè)品質(zhì)網(wǎng)站。需求
我們想在編輯一個(gè)列表中某一個(gè)條目時(shí),將編輯的詳情內(nèi)容也放置當(dāng)前面,比如右側(cè)。
可以通過將一個(gè)Grid,分成兩個(gè)Cloumn,動(dòng)態(tài)調(diào)整兩個(gè)Cloumn的Width,就可以實(shí)現(xiàn)這個(gè)需求。
我們知道,Clomun的Width是個(gè),而默認(rèn)的動(dòng)畫沒有這樣子的。我們就需要自己實(shí)現(xiàn)這樣一人動(dòng)畫。
設(shè)計(jì)
我們從Animation的類圖上看到
我們可以從需求
我們想在編輯一個(gè)列表中某一個(gè)條目時(shí),將編輯的詳情內(nèi)容也放置當(dāng)前面,比如右側(cè)。
可以通過將一個(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)畫沒有這樣子的。我們就需要自己實(shí)現(xiàn)這樣一人動(dòng)畫。
設(shè)計(jì)
我們從Animation的類圖上看到AnimationTimeline繼承,重寫其GetCurrentValue
public class GridLengthAnimation : AnimationTimeline { ////// Returns the type of object to animate /// public override Type TargetPropertyType => typeof(GridLength); ////// Creates an instance of the animation object /// ///Returns the instance of the GridLengthAnimation protected override System.Windows.Freezable CreateInstanceCore() { return new GridLengthAnimation(); } ////// Dependency property for the From property /// public static readonly DependencyProperty FromProperty = DependencyProperty.Register("From", typeof(GridLength), typeof(GridLengthAnimation)); ////// CLR Wrapper for the From depenendency property /// public GridLength From { get { return (GridLength)GetValue(GridLengthAnimation.FromProperty); } set { SetValue(GridLengthAnimation.FromProperty, value); } } ////// Dependency property for the To property /// public static readonly DependencyProperty ToProperty = DependencyProperty.Register("To", typeof(GridLength), typeof(GridLengthAnimation)); ////// CLR Wrapper for the To property /// public GridLength To { get { return (GridLength)GetValue(GridLengthAnimation.ToProperty); } set { SetValue(GridLengthAnimation.ToProperty, value); } } ////// Animates the grid let set /// /// The original value to animate /// The final value /// The animation clock (timer) ///Returns the new grid length to set 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)化
通過以上代碼,我們實(shí)現(xiàn)了在GridLength變化時(shí),實(shí)現(xiàn)動(dòng)畫。但是,試用后我們發(fā)現(xiàn),動(dòng)畫,有點(diǎn)太線性。這個(gè)時(shí)候,怎么辦?
可以通過引入EasingFunction來實(shí)現(xiàn)。我們知道EasingFunction其實(shí)就是一個(gè)與時(shí)間t有關(guān)的時(shí)間函數(shù)f(t).通過時(shí)間函數(shù)的處理,我們使動(dòng)畫過渡不要那么線性。
////// The public const string EasingFunctionPropertyName = "EasingFunction"; ///dependency property's name. /// /// Gets or sets the value of the public IEasingFunction EasingFunction { get { return (IEasingFunction)GetValue(EasingFunctionProperty); } set { SetValue(EasingFunctionProperty, value); } } ////// property. This is a dependency property. /// /// Identifies the public static readonly DependencyProperty EasingFunctionProperty = DependencyProperty.Register( EasingFunctionPropertyName, typeof(IEasingFunction), typeof(GridLengthAnimation), new UIPropertyMetadata(null));dependency property. ///
對(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); }
使用
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司的支持。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、網(wǎng)站設(shè)計(jì)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。